zoukankan      html  css  js  c++  java
  • ORACLE 使用sqluldr2和sqlldr进行导入导出

    oracle数据导出工具sqluldr2可以将数据以csv、txt等格式导出,适用于大批量数据的导出,导出速度非常快。导出后可以使用oracle loader工具将数据导入。

    简介:

    Sqluldr2:专业用于大数据量导出工具之一,效率比普通导出快70%。 ( Sqlldr:专业用于导入的工具之一,请注意两个工具的区别。),在使用时,最好用磁盘写入速度快,网络好,网速快的做。

    工具列表:

    针对不同平台用不同的导出工具:

    Windows:     sqluldr2.exe

    Linux(32位): sqluldr2_linux32_10204.bin

    Linux(64位):   sqluldr2_linux64_10204.bin

    使用说明 (Windows平台):

    使用sqluldr2的步骤:

    1.打开运行àcmd进入到sqluldr2.exe的当前目录

     

    2.参数介绍

    User=用户/密码@tns

    Query=”查询语句”

    File= 导出的路径

    Head= 输出信息时,yes表示要表头,no表示不要表头

     

    注意:想查看更多参数,请输入 sqluldr2 help=yes

     

    1. C:UsersmengDesktopsqluldr2>sqluldr2.exe help=yes  
    2.   
    3.   
    4. SQL*UnLoader: Fast Oracle Text Unloader (GZIP, Parallel), Release 4.0.1  
    5. (@) Copyright Lou Fangxin (AnySQL.net) 2004 - 2010, all rights reserved.  
    6.   
    7.   
    8. License: Free for non-commercial useage, else 100 USD per server.  
    9.   
    10.   
    11. Usage: SQLULDR2 keyword=value [,keyword=value,...]  
    12.   
    13.   
    14. Valid Keywords:  
    15.    user    = username/password@tnsname  
    16.    sql     = SQL file name  
    17.    query   = select statement  
    18.    field   = separator string between fields  
    19.    record  = separator string between records  
    20.    rows    = print progress for every given rows (default, 1000000)  
    21.    file    = output file name(default: uldrdata.txt)  
    22.    log     = log file name, prefix with + to append mode  
    23.    fast    = auto tuning the session level parameters(YES)  
    24.    text    = output type (MYSQL, CSV, MYSQLINS, ORACLEINS, FORM, SEARCH).  
    25.    charset = character set name of the target database.  
    26.    ncharset= national character set name of the target database.  
    27.    parfile = read command option from parameter file  
    28.    read    = set DB_FILE_MULTIBLOCK_READ_COUNT at session level  
    29.    sort    = set SORT_AREA_SIZE at session level (UNIT:MB)  
    30.    hash    = set HASH_AREA_SIZE at session level (UNIT:MB)  
    31.    array   = array fetch size  
    32.    head    = print row header(Yes|No)  
    33.    batch   = save to new file for every rows batch (Yes/No)  
    34.    size    = maximum output file piece size (UNIB:MB)  
    35.    serial  = set _serial_direct_read to TRUE at session level  
    36.    trace   = set event 10046 to given level at session level  
    37.    table   = table name in the sqlldr control file  
    38.    control = sqlldr control file and path.  
    39.    mode    = sqlldr option, INSERT or APPEND or REPLACE or TRUNCATE  
    40.    buffer  = sqlldr READSIZE and BINDSIZE, default 16 (MB)  
    41.    long    = maximum long field size  
    42.    width   = customized max column width (w1:w2:...)  
    43.    quote   = optional quote string  
    44.    data    = disable real data unload (NO, OFF)  
    45.    alter   = alter session SQLs to be execute before unload  
    46.    safe    = use large buffer to avoid ORA-24345 error (Yes|No)  
    47.    crypt   = encrypted user information only (Yes|No)  
    48.    sedf/t  = enable character translation function  
    49.    null    = replace null with given value  
    50.    escape  = escape character for special characters  
    51.    escf/t  = escape from/to characters list  
    52.    format  = MYSQL: MySQL Insert SQLs, SQL: Insert SQLs.  
    53.    exec    = the command to execute the SQLs.  
    54.    prehead = column name prefix for head line.  
    55.    rowpre  = row prefix string for each line.  
    56.    rowsuf  = row sufix string for each line.  
    57.    colsep  = separator string between column name and value.  
    58.    presql  = SQL or scripts to be executed before data unload.  
    59.    postsql = SQL or scripts to be executed after data unload.  
    60.    lob     = extract lob values to single file (FILE).  
    61.    lobdir  = subdirectory count to store lob files .  
    62.    split   = table name for automatically parallelization.  
    63.    degree  = parallelize data copy degree (2-128).  
    64.    hint    = MySQL SQL hint for the Insert, for example IGNORE.  
    65.    unique  = Unique Column List for the MySQL target table.  
    66.    update  = Enable MySQL ON DUPLICATE SQL statement(YES/NO).  
    67.   
    68.   
    69.   for field and record, you can use '0x' to specify hex character code,  
    70.    =0x0d  =0x0a |=0x7c ,=0x2c,  =0x09, :=0x3a, #=0x23, "=0x22 '=0x27  
    71.   
    72. C:UsersmengDesktopsqluldr2>  
    73.    

     

    代码例子1:

    sqluldr2.exe USER=用户/密码@tnsQUERY="select   /*+ parallel(8)  */  *from cs_XXX dt,cfg_XXX devdim105 wheredt.starttime>=to_date('2012-06-27 00:00:00','yyyy-mm-dd hh24:mi:ss') and dt.starttime<=to_date('2012-06-2700:01:59','yyyy-mm-dd hh24:mi:ss') AND dt.msc= devdim105.mapvalue(+) ANDdevdim105.deviceid=15 " head=yes FILE=F:cs_XXX_test.csv

     

    代码例子2(这种方式用于导出的查询sql很长,那么就把sql写在123.sql文件里 ):

    sqluldr2.exe USER=用户/密码@tns  sql=123.sqlhead=yes FILE=F:cs_XXX_test.csv

     

     

    3.在cmd里,直接把步骤2的代码例子1 ,贴进去执行。

     

    注:如果执行报报错,就有可能是环境变量path的问题,还有就是plsql需要的oci.dll文件等多个dll文件,请放在sqluldr2的目录下。

     

    4.查看结果F:cs_XXX_test.csv

     

    5.打开csv里面的内容,就可以用sqlldr进行 入库。

     

     

     

     

     

    --如果是linux版本

     

    1. [root@localhost export_xdr]# ./sqluldr2_linux64_10204.bin   
    2.   
    3. SQL*UnLoader: Fast Oracle Text Unloader (GZIP, Parallel), Release 4.0.1  
    4. (@) Copyright Lou Fangxin (AnySQL.net) 2004 - 2010, all rights reserved.  
    5.   
    6. Usage: SQLULDR2 keyword=value [,keyword=value,...]  
    7.   
    8. Valid Keywords:  
    9.    user    = username/password@tnsname  
    10.    sql     = SQL file name  
    11.    query   = select statement  
    12.    field   = separator string between fields  
    13.    record  = separator string between records  
    14.    rows    = print progress for every given rows (default, 1000000)   
    15.    file    = output file name(default: uldrdata.txt)  
    16.    log     = log file name, prefix with + to append mode  
    17.    fast    = auto tuning the session level parameters(YES)  
    18.    text    = output type (MYSQL, CSV, MYSQLINS, ORACLEINS, FORM, SEARCH).  
    19.    charset = character set name of the target database.  
    20.    ncharset= national character set name of the target database.  
    21.    parfile = read command option from parameter file   
    22.   
    23.   for field and record, you can use '0x' to specify hex character code,  
    24.   r=0x0d n=0x0a |=0x7c ,=0x2c, t=0x09, :=0x3a, #=0x23, "=0x22 '=0x27  
     
    1. [root@localhost export_xdr]# cd /usr/lib/oracle/11.2/client64/lib/  
    2. [root@localhost lib]# ls  
    3. glogin.sql    libclntsh.so.10.1  libnnz11.so  libocci.so.11.1  libocijdbc11.so  libsqlplus.so  ojdbc6.jar      xstreams.jar  
    4. libclntsh.so  libclntsh.so.11.1  libocci.so   libociei.so      libsqlplusic.so  ojdbc5.jar     ottclasses.zip  
    5. [root@localhost lib]# ls -l  
    6. total 185012  
    7. -rw-r--r-- 1 root root       368 Sep 17  2011 glogin.sql  
    8. lrwxrwxrwx 1 root root        17 Jan 26 17:08 libclntsh.so -> libclntsh.so.11.1  
    9. lrwxrwxrwx 1 root root        12 Jan 26 17:31 libclntsh.so.10.1 -> libclntsh.so  
    10. -rw-r--r-- 1 root root  52761218 Sep 17  2011 libclntsh.so.11.1  
    11. -rw-r--r-- 1 root root   7955322 Sep 17  2011 libnnz11.so  
    12. lrwxrwxrwx 1 root root        15 Jan 26 17:08 libocci.so -> libocci.so.11.1  
    1. [root@localhost export_xdr]# history |grep ln  
    2.    94  find / -name libclntsh.so.10.1  
    3.    95  find / -name libclntsh.so  
    4.   116  find / -name libclntsh.so  
    5.   127  ln libclntsh.so.10.1 libocci.so  
    6.   128  ln libocci.so libclntsh.so.10.1  
    7.   130  rm libclntsh.so.10.1 -f  
    8.   132  ln libclntsh.so libclntsh.so.10.1  
    9.   134  rm libclntsh.so.10.1 -f  
    10.   135  ln libclntsh.so.10.1 libclntsh.so  
     

    1. ./sqluldr2_linux64_10204.bin user=unxx/密码@192.168.x.x:1521/unxx query="select/*+ parallel(2) */STARTTIME, ENDTIME,phone,imei,rantype,HCITY from v_ps_xxx dt,mv_terminal cc  where endtime_par>=trunc(sysdate)-1 and endtime_par<trunc(sysdate)-1+1/24 and dt.TACID=cc.TACID(+) " head=yes field=0x09 text=txt file='/BigData/export_xdr/exportting.txt.tmp';  
     

    补充:

    兄弟们,在安装的时候,是不是经常遇到报错?

    比如:

    1. [root@FCJ-2F-21 ~]# ./sqluldr2_linux64_10204.bin   
    2. ./sqluldr2_linux64_10204.bin: error while loading shared libraries: libclntsh.so.10.1: cannot open shared object file: No such file or directory  
    3. [root@FCJ-2F-21 ~]# more /etc/profile  

    方法一:

    这个so文件,在安装oracle后,肯定是有的,如果没有可以用软连接 ln -s xxxx  libclntsh.so.10.1

    1.下面只需配置 LD_LIBRARY_PATH

    export ORACLE_HOME=/u01/app/Oracle/product/11.2.0/dbhome_1
    export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
    export PATH=$PATH:$ORACLE_HOME:$ORACLE_HOME/bin

    1. [oracle@FCJ-2F-21 lib]$ more /etc/profile  
    2. # /etc/profile  
    3.   
    4. # System wide environment and startup programs, for login setup  
    5. # Functions and aliases go in /etc/bashrc  
    6.   
    7. pathmunge () {  
    8.     if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then  
    9.        if [ "$2" = "after" ] ; then  
    10.           PATH=$PATH:$1  
    11.        else  
    12.           PATH=$1:$PATH  
    13.        fi  
    14.     fi  
    15. }  
    16.   
    17. # ksh workaround  
    18. if [ -z "$EUID" -a -x /usr/bin/id ]; then   
    19.     EUID=`id -u`  
    20.     UID=`id -ru`  
    21. fi  
    22.   
    23. # Path manipulation  
    24. if [ "$EUID" = "0" ]; then  
    25.     pathmunge /sbin  
    26.     pathmunge /usr/sbin  
    27.     pathmunge /usr/local/sbin  
    28. fi  
    29.   
    30. # No core files by default  
    31. ulimit -S -c 0 > /dev/null 2>&1  
    32.   
    33. if [ -x /usr/bin/id ]; then  
    34.     USER="`id -un`"  
    35.     LOGNAME=$USER  
    36.     MAIL="/var/spool/mail/$USER"  
    37. fi  
    38.   
    39. HOSTNAME=`/bin/hostname`  
    40. HISTSIZE=1000  
    41.   
    42. if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc" ]; then  
    43.     INPUTRC=/etc/inputrc  
    44. fi  
    45.   
    46. export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC  
    47.   
    48. # By default, we want umask to get set. This sets it for login shell  
    49. # Current threshold for system reserved uid/gids is 200  
    50. # You could check uidgid reservation validity in  
    51. # /usr/share/doc/setup-*/uidgid file  
    52. if [ $UID -gt 99 ] && [ "`id -gn`" = "`id -un`" ]; then  
    53.     umask 002  
    54. else  
    55.     umask 022  
    56. fi  
    57.   
    58. for i in /etc/profile.d/*.sh ; do  
    59.     if [ -r "$i" ]; then  
    60.         if [ "${-#*i}" != "$-" ]; then  
    61.             . $i  
    62.         else  
    63.             . $i >/dev/null 2>&1  
    64.         fi  
    65.     fi  
    66. done  
    67.   
    68. unset i  
    69. unset pathmunge  
    70.   
    71. export LANG=en_US.UTF-8  
    72. export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1  
    73. export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH  
    74. export TNS_ADMIN=/usr/lib/oracle/11.1/client64/  
    75. export PATH=$PATH:$ORACLE_HOME:$ORACLE_HOME/bin  
     

    2.source /etc/profile

    成功。

    方法二:(来自其他网友)

      1.  如果共享库文件安装到了/usr/local/lib(很多开源的共享库都会安装到该目录下)或其它"非/lib或/usr/lib"目录下, 那么在执行ldconfig命令前, 还要把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 如下:  
      2.   
      3. # cat /etc/ld.so.conf  
      4. include ld.so.conf.d/*.conf  
      5. # echo "/usr/local/lib" >> /etc/ld.so.conf  
      6. # ldconfig 
    ----实例演示
    下载完sqluldr2,文件夹内容如下:
    sqluldr2_linux32_10204.bin和sqluldr2_linux64_10204.bin分别适用于与linux32位和linux64位操作系统;
    sqluldr2.exe用于windows平台。


    以下是导出导入过程:
    1、首先将sqluldr2.exe复制到到$ORACLE_HOME的bin目录,即可开始使用:

    2、查看help帮助:


    3、执行导出数据命令:
    sqluldr2.exe USER=hh/hh@tiod QUERY="select /*+ parallel(2) */  *from hh.ent_person" table=ent_person head=yes FILE=C:ent_person.txt
    p.s. head=yes表示第一行为表头;并且query也可以写入一个文件然后使用sql选项。
    默认分隔字符为逗号,如果列中有特殊字符,可使用field选项指定新的分隔字符。


    4、数据已经导入到C:ent_person.txt,几十万数据秒速。

    5、使用sqlldr进行导入,首先找到$ORACLE_HOME的bin目录生成的ctl文件,当sqluldr2有table选项会默认生成ctl文件,以用于导入。


    6、将数据加载到数据库中:
    sqlldr jms/jms@tiod control=ent_person_sqlldr.ctl log=ent_person_sqlldr.log bad=ent_person_sqlldr_bad.log skip=1  errors=5000 rows=5000 bindsize=335542
    p.s. skip=1表示跳过第一行,从第二行开始导入。


    7、最后查看log是否有数据没有导入:
  • 相关阅读:
    校验函数
    声明
    主程序(开始检查)
    活代码LINQ——09
    活代码LINQ——08
    活代码LINQ——07
    活代码LINQ——06
    活代码LINQ——05
    活代码LINQ——04
    活代码LINQ——03
  • 原文地址:https://www.cnblogs.com/gaochsh/p/9427576.html
Copyright © 2011-2022 走看看