使用SQL*Loader导入CLOB和BLOB数据使用案例


        在之前的四篇文章中,介绍了SQL*Loader的使用方法及使用案例,本篇使用SQL*Loader导入CLOBBLOB数据
      Oracle SQL*Loader使用案例(一)
      Oracle SQL*Loader使用案例(二)
      Oracle SQL*Loader使用案例(三)
      Oracle SQL*Loader使用案例(四)


      在使用SQL*Loader往数据库中导入blob或clob数据总令人有些头疼。本文详细记录了用Oracle的SQL*Loader实现该功能的一个实验案例,希望能对大家有帮助。

 

 

首先创建一张用于实验的测试表

 

SCOTT@seiang11g>create table tb_clob(new_id number(20),new_to varchar2(100),new_subject varchar2(100),new_date date,new_content clob);
Table created.

 

下面是数据文件中的内容:

[oracle@wjq SQL*Loader]$ vim wjq_clob.csv
1,wjq123@qq.com,"Greeting from Mars",2017-11-02 10:59:43,\/u01\/app\/oracle\/SQL*Loader\/new_clob001.dat
2,seiang@126.com,"Special discount",2017-11-02 11:28:55,\/u01\/app\/oracle\/SQL*Loader\/new_clob002.dat
(特别注意:一定要注意数据文件的格式,不能有多余的空格存在,作者在这里栽过好多次的坑,否则导入的时候会报错,所以执行导入之后最好查看一下日志信息)

 

new_content是消息正文,用clob数据类型表示。在数据文件中该字段仅保存每条记录的消息正文的clob文件名。new_clob001.dat,,new_clob002.dat文件中保存new_content的真正内容。

 

new_clob001.dat文件中的内容:

[oracle@wjq SQL*Loader]$ cat new_clob001.dat
China's top judicial authorities presented long-anticipated reports on Wednesday to national legislators on their progress in upholding the law and preventing wrongful convictions in the wake of important judicial reforms in 2013.

The Supreme People's Court and Supreme People's Procuratorate both submitted reports to the bimonthly session of the Standing Committee of the National People's Congress on Wednesday.The top court said it had overturned 37 wrongful convictions since November 2012, including in the high-profile case of Nie Shubin, who was exonerated on Dec 2, 2016, more than two decades after he was wrongly executed for rape and murder.

 

new_clob001.dat文件中的内容:

[oracle@wjq SQL*Loader]$ cat new_clob002.dat  
Thanks to these efforts, courts acquitted 4,032 defendants in accordance with the law between 2013 and September this year, the report said.

Courts have also been ordered to strictly exclude evidence obtained illegally, including evidence gained by torture, "and not to force anyone to plead guilty", Zhou said.In Shanghai, for example, between July 2016 and September this year, the city's courts received 24 applications from defense attorneys to strike evidence suspected to have been obtained illegally, leading to 15 reviews on the legality of evidence, he said.

 

导入上述的数据需要写如下的控制文件:

[oracle@wjq SQL*Loader]$ cat wjq_clob.ctl
LOAD DATA  
INFILE '/u01/app/oracle/SQL*Loader/wjq_clob.csv' 
TRUNCATE INTO TABLE tb_clob 
FIELDS TERMINATED BY ',' 
( new_id       CHAR(20), 
  new_to       CHAR(100),   
  new_subject  CHAR(100), 
  new_date     DATE "YYYY-MM-DD HH24:MI:SS" ":new_date",
  clob_filename FILLER CHAR(1000000), 
  new_content  LOBFILE(clob_filename) TERMINATED BY EOF 
)

上述控制文件的关键在于定义一个clob_filename的伪字段(在SQL*Loader中就是filler)以获取clob文件名,紧接着用lobfile从该文件导入消息正文。如果需要导入blob类型的数据,其方法完全一样。

 

下面就开始执行导入操作:

[oracle@wjq ~]$ sqlldr scott/tiger control=/u01/app/oracle/SQL*Loader/wjq_clob.ctl log=/u01/app/oracle/SQL*Loader/wjq_clob.log

SQL*Loader: Release 11.2.0.4.0 - Production on Thu Nov 2 11:43:54 2017

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

Commit point reached - logical record count 1
Commit point reached - logical record count 2

 

下面查看一下日志信息:

Table TB_CLOB:
  2 Rows 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.

Space allocated for bind array:                1000488 bytes(1 rows)
Read   buffer bytes: 1048576

Total logical records skipped:          0
Total logical records read:             2
Total logical records rejected:         0
Total logical records discarded:        0

Run began on Thu Nov 02 11:43:54 2017
Run ended on Thu Nov 02 11:43:54 2017

Elapsed time was:     00:00:00.05
CPU time was:         00:00:00.00

 

通过日志可以看到,导入成功,下面通过查询表中的内容来进行验证





作者:SEian.G(苦练七十二变,笑对八十一难)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31015730/viewspace-2147266/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31015730/viewspace-2147266/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了你的问题。以下是一个简单的示例,展示如何使用PL/SQL导出和导入CLOBBLOB数据。 先看导出CLOB数据的示例: ``` DECLARE v_clob CLOB; v_file UTL_FILE.FILE_TYPE; BEGIN SELECT clob_column INTO v_clob FROM my_table WHERE id = 1; v_file := UTL_FILE.FOPEN('MY_DIR', 'clob_data.txt', 'w', 32767); UTL_FILE.PUT(v_file, v_clob); UTL_FILE.FCLOSE(v_file); END; ``` 这个示例从`my_table`表中查询ID为1的行的`clob_column`列的值,并将其写入到名为`clob_data.txt`的文件中。其中,`MY_DIR`是一个已经存在的目录对象,它指向一个文件系统目录。 接下来看导入CLOB数据的示例: ``` DECLARE v_clob CLOB; v_file UTL_FILE.FILE_TYPE; BEGIN v_file := UTL_FILE.FOPEN('MY_DIR', 'clob_data.txt', 'r', 32767); UTL_FILE.GET_LINE(v_file, v_clob); UTL_FILE.FCLOSE(v_file); INSERT INTO my_table (id, clob_column) VALUES (2, v_clob); END; ``` 这个示例从名为`clob_data.txt`的文件中读取第一行,并将其插入到`my_table`表的ID为2的行的`clob_column`列中。 接下来是导出BLOB数据的示例: ``` DECLARE v_blob BLOB; v_file UTL_FILE.FILE_TYPE; BEGIN SELECT blob_column INTO v_blob FROM my_table WHERE id = 1; v_file := UTL_FILE.FOPEN('MY_DIR', 'blob_data.txt', 'wb', 32767); UTL_FILE.PUT_RAW(v_file, v_blob); UTL_FILE.FCLOSE(v_file); END; ``` 这个示例从`my_table`表中查询ID为1的行的`blob_column`列的值,并将其写入到名为`blob_data.txt`的文件中。注意,这里使用了`UTL_FILE.PUT_RAW`函数来写入原始的二进制数据。 最后是导入BLOB数据的示例: ``` DECLARE v_blob BLOB; v_file UTL_FILE.FILE_TYPE; BEGIN v_file := UTL_FILE.FOPEN('MY_DIR', 'blob_data.txt', 'rb', 32767); UTL_FILE.GET_RAW(v_file, v_blob, 200); UTL_FILE.FCLOSE(v_file); INSERT INTO my_table (id, blob_column) VALUES (2, v_blob); END; ``` 这个示例从名为`blob_data.txt`的文件中读取前200个字节,并将其插入到`my_table`表的ID为2的行的`blob_column`列中。 需要注意的是,在以上示例中,我使用了`UTL_FILE`包来读写文件。在使用`UTL_FILE`包前,你需要创建一个目录对象,指向你想要导入/导出数据的文件夹。另外,如果你想要导出/导入大量的数据,你可能需要对`UTL_FILE`的读写方法进行优化,以避免出现性能问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值