使用說明:
本程序的原來是利用SQL Plus的spool命令將從數據導出爲文本文件。
程序有三種模式:默認模式,定製模式,並行模式sql
默認模式:
舉例:shell
sh exp_spool_table.sh -connect=test/123@test
說明: 程序默認導出到當前路徑下的data目錄下,若是沒有此目錄程序會自動建立。
文件名相似 20161204-0.data,若文件已經存在,程序會報錯推出,需手動刪除後再運行。
默認的導出sql爲當前目錄的exp_table.sql.
-connect參數爲sqlplus鏈接數據庫信息,格式爲:用戶名/密碼@TNS名或鏈接串。
例如: -connect=test/123@test 若採用TNS名的方式,須要在TNS_ADMIN(環境變量)目錄下的tnsnames.ora中配置對應的tns信息
-connect=test/123@192.168.1.101:1521/orcl數據庫
定製模式:
舉例:bash
sh exp_spool_table.sh -connect=test/123@test -sql=/path/to/your.sql
說明:你能夠用-sql參數指定導出的sql文件
導出sql舉例,導出的子段以逗號分割,日期格式或者包含換行的字段須要進行轉換。 ui
select ID||','||to_char('yyyymmddhh24miss',date)||','||replace(text,chr(10),'') from TABLE_NAME;
導出的格式爲 10001,20161204191800,備註說明this
並行模式:
舉例:.net
sh exp_spool_table.sh -connect=test/123@test -sql=my_sql_1.sql -parallel=true -id=table1 sh exp_spool_table.sh -connect=test/123@test -sql=my_sql_2.sql -parallel=true -id=table2
說明:對於大量的數據導出的場景,程序提供了並行模式,能夠啓動多個進程同時進行導出。
經過-parallel=true來啓動並行模式,同時,並行模式必須指定-sql和-id參數。
-id參數用來區分導出的文件。文件名以"日期-ID.data"的方式命名,
已啓動2個進程爲例:
-id=table1的進程導出的數據文件爲 20161204-table1.data,同理,-id=table2的數據文件爲 20161204-table2.data
注意:若多個進程同時導出同一個表的數據,須要在sql文件中經過取模的方式來防止重複導出數據。
my_sql_1.sql中的內容:code
select ID||','||to_char('yyyymmddhh24miss',date)||','||replace(text,chr(10),'') from TABLE_NAME where (ID mod 2) = 0;
my_sql_2.sql中的內容:three
select ID||','||to_char('yyyymmddhh24miss',date)||','||replace(text,chr(10),'') from TABLE_NAME where (ID mod 2) = 1;
如下爲shell腳本的源代碼:進程
#!/bin/bash usages="Usages: you can use this shell in three mode listed below: \n #easy ,default using exp_table.sql \n sh exp_spool_table.sh -connect=test/123@test \n sh exp_spool_table.sh -connect=test/123@192.168.1.1:1521/orcl \n \n #customed your export sql \n sh exp_spool_table.sh -connect=test/123@test -sql=/path/to/your.sql \n \n #parallel \n sh exp_spool_table.sh -connect=test/123@test -sql=my_table_1.sql -parallel=true -id=1 \n sh exp_spool_table.sh -connect=test/123@test -sql=my_table_2.sql -parallel=true -id=2" if [ $# -eq 0 ] ; then echo -e $usages exit 1 fi #unique id for parallism ,default:0 ID=0 #export data to this directory,you can change it to your own target directory export_dir=./data #default sql file,you can change it by passing the first argument to this process #example :sh exp_spool_table.sh sql=my_table.sql export_sql=exp_table.sql #connection info using by sqlplus #example: # user/passwd@tnsname # user/passwd@192.168.1.1:1521/orcl CONNECTION="" PARALLEL="false" #parse arguments ARGS=`getopt -o "" -al connect:,sql:,parallel:,id: -- "$@"` if [ $? -ne 0 ] ;then echo 'invalid arguments' echo -e $usages exit 1; fi ARG_NUM=$# eval set -- "$ARGS" while true; do case "$1" in --connect) CONNECTION=$2; shift 2;; --sql) export_sql=$2; shift 2;; --parallel) PARALLEL=$2; shift 2;; --id) ID=$2; shift 2;; --) break ;; *) echo $1,$2; break ;; esac done if [ $PARALLEL = "true" ] && [ $ARG_NUM -ne 4 ];then echo "when you using parallel mode ,you must set all arguments explicit ,do not use default value" echo "example: sh exp_spool_table.sh -connect=test/123@test -sql=my_table_1.sql -parallel=true -id=1 sh exp_spool_table.sh -connect=test/123@test -sql=my_table_2.sql -parallel=true -id=2" exit 1 fi if [ ! -f $export_sql ] ; then echo "sql file '$export_sql' doesn't exists " exit 1 fi #export data into the file named like '20161225-pid.data' export_file=`date '+%Y%m%d'`-$ID.data echo "current dir is '`pwd`' ,running shell '$0' ,pid '$$'" echo "export data to dir '$export_dir' , file name is '$export_file'" #if the directory exists , remove it automaticly if [ ! -d $export_dir ] ;then echo -n "$export_dir is not exists " mkdir -p $export_dir if [ $? -eq 0 ]; then echo ",create it successfully " else echo "failed to create it ,you can create it manually ,then retry." echo "process exits with failure" exit 1 fi fi #if the file exists ,remove it automaticly if [ -f $export_dir/$export_file ] ;then echo "failed , $export_dir/$export_file is exists ,please remove it and try again" exit 1 fi echo -e "\n===============spooling data using SQLPLUS ==========================" sqlplus -S $CONNECTION<<EOF set trimspool on set linesize 2000 set pagesize 1000 set newpage none set heading off set term off set feedback off; spool $export_dir/$export_file @"$export_sql" spool off quit EOF if [ $? -ne 0 ] ; then echo "export data failed" exit 1 fi echo `cat $export_dir/$export_file | wc -l` records exported sucessfullly