SQL 从单个文件加载到 BLOB 列的加载器

SQL Loader to load into BLOB column from single file

我想将 CSV 文件加载到 Oracle table。该文件有一个列可以包含一个非常长的字符串,该字符串将超过 VARCHAR2 的最大长度。因此,我正在加载的 table 将此列指定为 BLOB.

create table MY_TABLE(
COL1 VARCHAR2(100),
COL2 VARCHAR2(100),
COL3 BLOB
);

要加载的 CSV 文件如下所示:

COL1,COL2,COL3
1,qwerty,<very long block of text>
2,asdfgh,<very long block of text>
3,zxcvbn,<very long block of text>
...

如何使用 SQL 加载程序加载它?我能找到的所有网站都描述了如何使用 BLOB 来加载单独的文件,即:

LOAD DATA 
INFILE 'lob_test_data.txt'
   INTO TABLE lob_tab
   FIELDS TERMINATED BY ','
   (number_content    CHAR(10),
    varchar2_content  CHAR(100),
    date_content      DATE "DD-MON-YYYY" ":date_content",
    clob_filename     FILLER CHAR(100),
    clob_content      LOBFILE(clob_filename) TERMINATED BY EOF,
    blob_filename     FILLER CHAR(100),
    blob_content      LOBFILE(blob_filename) TERMINATED BY EOF)
    
    Source: https://oracle-base.com/articles/10g/load-lob-data-using-sql-loader

如何从我的单个 CSV 文件加载到 BLOB?我希望下面的代码可以工作,但会引发语法错误:

load data
inflie '/path/to/file.csv'
into table MY_TABLE
fields terminated by ','
(
COL1 CHAR(100),
COL2 CHAR(100),
COL3 BLOB
)

为了加载很长的文本,您应该使用 CLOB 而不是 BLOB。后者用于存储二进制对象,例如视频文件、图片或声音文件。

加载方式取决于 CLOB 内容是存储在您的 CSV 文件中还是您使用的是外部文件定位器。

CSV 文件中的 CLOB

举个例子。我创建了一个包含很长文本的 csv 示例文件:

$ cat testclob.txt
1;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

我的控制文件看起来像

load data
infile '/home/ftpcpl/testclob.txt'
into table test_perf.t
append  
fields terminated by ';'
(
  c1 integer ,
  c2 char(10000)
)

然后我加载文件

$ sqlldr test_perf/Oracle_1 control=/home/ftpcpl/testclob.ctl log=/home/ftpcpl/testclob.log

SQL*Loader: Release 12.2.0.1.0 - Production on Tue Aug 11 14:23:21 2020

Copyright (c) 1982, 2017, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 1

Table TEST_PERF.T:
  1 Row successfully loaded.

Check the log file:
  /home/ftpcpl/testclob.log
for more information about the load.

日志文件看起来像

  Table TEST_PERF.T:
  1 Row successfully loaded.
  0 Rows not loaded due to data errors.
  0 Rows not loaded because all WHEN clauses were failed.
  0 Rows not loaded because all fields were null

.

CLOB 作为外部文件定位器

如果 CLOB 在另一个文件中,则应将文件加载为:

load data
infile *
replace
into table t
fields terminated by ',' optionally enclosed by '"' trailing nullcols
(
id integer external,
fname filler,
c LOBFILE(fname) TERMINATED BY EOF
)
begindata
1,"c:\temp\demo.out"
2,"c:\temp\load_emoji.sql"

SQL> create table t ( id int, c clob );

Table created.

$ sqlldr userid=scott/tigercontrol=c:\temp\c.ctl

SQL*Loader: Release 12.2.0.1.0 - Production on Fri Sep 8 15:49:55 2017

Copyright (c) 1982, 2017, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 1
Commit point reached - logical record count 2

Table T:
  2 Rows successfully loaded.

Check the log file:
  c.log
for more information about the load.

SQL> select id, length(c) from t;

        ID  LENGTH(C)
---------- ----------
         1      17640
         2       1418