Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6945133
  • 博文数量: 701
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12021
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

中科院架构师,专注企业数字化各个方面,MES/ERP/CRM/OA、物联网、传感器、大数据、ML、AI、云计算openstack、Linux、SpringCloud。

文章分类

全部博文(701)

分类: Oracle

2011-02-15 10:31:50

       sql loader可以把一些以文本格式存放的数据顺利地导入到中,它是一种在不同数据库之间进行数据迁移非常方便而且通用的工具。缺点就是速度比较慢,另外对blob等类型的数据就有点麻烦了。

一、用法SQLLDR keyword=value [,keyword=value,...]

 

二、有效的关键字:

   userid -- ORACLE username/password

   control – 控制文件

   log – 记录的日志文件

   bad – 坏数据文件

   data – 数据文件

   discard – 丢弃的数据文件

   discardmax – 允许丢弃数据的最大值        (全部默认)

   skip -- Number of logical records to skip  (默认0)

   load -- Number of logical records to load  (全部默认)

   errors – 允许的错误记录数          (默认50)

   rows -- Number of rows in conventional path bind array or between direct path data saves(每次提交的记录数,默认常规路径 64, 所有直接路径)

   bindsize -- Size of conventional path bind array in bytes(默认256000)

每次提交记录的缓冲区的大小(字节为单位,默认256000)

    silent --禁止输出信息 (header,feedback,errors,discards,partitions)

    direct – 使用直通路径方式导入                    (默认FALSE)

    parfile -- parameter file: name of file that contains parameter specifications

    parallel -- 并行导入                   (默认FALSE)

    file -- File to allocate extents from

    skip_unusable_indexes    -- disallow/allow unusable indexes or index partitions(默认FALSE)

    skip_index_maintenance   -- do not maintain indexes, mark affected indexes as unusable(默认FALSE)

    readsize -- Size of Read buffer                (默认1048576)

    bindsize成对使用,其中较小者会自动调整到较大者。sqlldr先计算单条记录长度,乘以rows,如小于bindsize,不会试图扩张rows以填充bindsize;如超出,则以bindsize为准。

 

     external_table       -- use external table for load; NOT_USEDGENERATE_ONLYEXECUTE(默认NOT_USED)

     columnarrayrows     -- Number of rows for direct path column array(默认5000)

     streamsize        -- Size of direct path stream buffer in bytes(默认256000)

     multithreading       -- use multithreading in direct path

     resumable -- enable or disable resumable for current session(默认FALSE)

     resumable_name       -- text string to help identify resumable statement

     resumable_timeout       -- wait time (in seconds) for RESUMABLE(默认7200)

     date_cache -- size (in entries) of date conversion cache(默认1000)

 

     注意:有两种方式可以指定命令行参数:通过位置或者通过关键字。

     1)通过位置指定命令行参数的例子:'sqlldr scott/tiger foo'

     2)通过关键字指定命令行参数的例子:'sqlldr control=foo userid=scott/tiger'

     不能前面使用关键字指定后面通过位置指定的混合方式;

     比如:'sqlldr scott/tiger control=foo logfile=log' 是允许的;

     'sqlldr scott/tiger control=foo log'不允许。

     通过位置指定命令行参数的时候,必须将位置放在user/passwd之前。

     为清楚起见最好所有命令行参数都用关键字指定。

三、控制文件:一个控制命令的脚本文件,通常以ctl结尾,内容如下:

LOAD DATA 

INFILE 't.dat'              //要导入的文件 

// INFILE 'tt.date'        //导入多个文件 

// INFILE *                  //表示要导入的内容就在control文件里 下面的BEGINDATA后面就是导入的内容 

INTO TABLE table_name   // 指定装入的表 

BADFILE 'c:\bad.txt'    //可选,指定坏文件地址,缺省在当前目录下生成与原文件名一致的.bad文件 

//************* 以下是4种装入表的方式 

APPEND           // 原先的表有数据 就加在后面 

// INSERT            //装载空表 如果原先的表有数据 sqlloader会停止 默认值 

// REPLACE       //  原先的表有数据 原先的数据会全部删除 

// TRUNCATE            //  指定的内容和replace的相同 会用truncate语句删除现存数据 

 

//************* 指定分隔符 

FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 

// TERMINATED BY WRITESPACE //以空白分割 

 

TRAILING NULLCOLS           // 表的字段没有对应的值时允许为空 

 

************* 下面是表的字段 

     col_1 , col_2 ,col_filler FILLER  // FILLER 关键字 此列的数值不会被装载 

     // : lg,lg,not 结果 lg lg 

)  

    如果没声明FIELDS TERMINATED BY ',' 时,可以用下面两种方式实现同样功能: 

1)、为每一列指定分隔符

 ( 

   col_1 [interger external] TERMINATED BY ',' , 

   col_2 [date "dd-mon-yyy"] TERMINATED BY ',' , 

   col_3 [char] TERMINATED BY ',' OPTIONALLY ENCLOSED BY 'lg' 

 ) 

2)、用位置告诉字段装载数据 

 ( 

    col_1 position(1:2), 

    col_2 position(3:10), 

    col_3 position(*:16), // 这个字段的开始位置在前一字段的结束位置 

    col_4 position(1:16), 

    col_5 position(3:10) char(8) // 指定字段的类型 

 ) 

 

BEGINDATA         //对应开始的 INFILE * 要导入的内容就在control文件里 

10what 

20lgshow

 

 

例子:

 

 [oracle@ZR230-117 ~]$ vi a.ctl

 

Esc i

 

options (bindsize=655360,direct=true) 

load data

infile '/usr/oracle/checkcode5.csv'

Append into table img_authcode

fields terminated by ';'

(pic_id,authcode,path)

 

wq

 

注:# bindsize每次提交记录的缓冲区direct使用直通方式,缺省为false

#读取'/usr/oracle/checkcode5.csv'这个excl文件

#添加到img_authcode这个表里

#每个字段间以“;”隔开

#分别对应pic_id,authcode,path这三个字段

#如果只导入一列,那么要注意用position标识一下字段长度:

options (bindsize=655360,direct=true)

load data

 

 

infile 'sex.txt'

 

 

insert into table id_temp

 

(id position(1:10))    //从第一个字节开始到第十个结束

 

 

 

导入控制文件

[oracle@ZR230-117 ~]$ sqlldr member/member control=a.ctl

 

提示完成:

SQL*Loader: Release10.2.0.2.0 - Production on Thu Mar 20 09:47:24

 

Copyright (c) 1982, 2005, Oracle. All rights reserved.

 

 

Load completed - logical record count 10000.

 

查看一下,有无错误:

[oracle@ZR230-117 ~]$ more a.log

 

SQL*Loader: Release10.2.0.2.0 - Production on Thu Mar 20 09:47:24 2008

 

Copyright (c) 1982, 2005, Oracle. All rights reserved.

 

Control File:  a.ctl

Data File:     /usr/oracle/checkcode5.csv

 Bad File:    checkcode5.bad

 Discard File: none specified

 

 (Allow all discards)

 

Number to load: ALL

Number to skip: 0

Errors allowed: 50

Continuation:   none specified

Path used:     Direct

 

Table IMG_AUTHCODE, loaded from every logical record.

Insert option in effect for this table: APPEND

 

  Column Name                 Position  Len Term Encl Datatype

------------------------------ ---------- ----- ---- ---- ---------------------

PIC_ID                             FIRST    *  ;      CHARACTER           

AUTHCODE                            NEXT    *  ;      CHARACTER           

PATH                                NEXT    *  ;      CHARACTER           

 

The following index(es) on table IMG_AUTHCODE were processed:

index MEMBER.SYS_C0020883 loaded successfully with 10000 keys

 

Table IMG_AUTHCODE:

 10000 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.

 

Bind array size not used in direct path.

Column array rows :   5000

Stream buffer bytes: 256000

Read  buffer bytes: 1048576

 

Total logical records skipped:         0

Total logical records read:        10000

Total logical records rejected:        0

Total logical records discarded:       0

Total stream buffers loaded by SQL*Loader main thread:       2

Total stream buffers loaded by SQL*Loader load thread:       0

 

Run began on Thu Mar 20 09:47:24 2008

Run ended on Thu Mar 20 09:47:25 2008

阅读(3414) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~