提供本人写的一个工具:Oracle unload tool,
用perl写的,可把oracle的数据表导出成文本,参数很灵活,效率也不错,虽然比不上用c写的(例如sqluldr),实测大致有sqluldr的60%左右,但也可以接受了,每秒能卸出10000至50000行不等,5-10MB/s,视实际环境而定。
优点:直接以源码发布,可移植性强,linux/unix/windows下都能用,只要配置好oracle客户端环境及perl环境(DBI+DBD::Oracle)即可。并且,稍加修改也可用于其它数据库,只要提供了DBD驱动的都行。
缺点:暂时不支持BLOB、CLOB等字段的导出,以后有时间的话再说。
源码如下(用GBK编码保存成ora_unload_gbk.pl,如果要启用UTF8,则存成ora_unload_utf8.pl,并把代码中的
my $UTF8 = 0改成 my $UTF8 =1, no utf8改成use utf8即可):
#!/usr/bin/perl
########################################################################################
#
#模块: oracle unload tool
#
#描述: 卸载ORACLE数据库中表内容到文本文件
#运行环境要求:
# perl 5.x.x on unix/linux
(http://www.perl.org/)
# or
# ActivePerl 5.x.x on windows
(http://www.activestate.com/activeperl)
# perl DBI module
(http://search.cpan.org/~timb/DBI/)
# perl DBD::Oracle module
(http://search.cpan.org/~timb/DBD-Oracle/)
# perl Archive::Zip module
(http://search.cpan.org/~timb/Archive-Zip/)
# oracle client
(http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html)
#
#环境变量要求:
# 1. 必须设置 ORACLE_HOME
环境变量
# 2. 如果操作系统为32位LINUX, 必须设置
LD_LIBRARY_PATH=$ORACLE_HOME/lib32:$LD_LIBRARY_PATH
#
如果操作系统为64位LINUX, 必须设置
LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
# 3. 如果操作系统为AIX, 必须设置
LIBPATH=$ORACLE_HOME/lib:$LIBPATH
#
(因DBD::Oracle模块只能在64位模式下成功编译)
########################################################################################
use strict;
my $version = '2.1.3';
my $devDate = '2013-04-22';
$0 = "ora_unload.pl";
my $UTF8 = 0;
no utf8;
if ( $UTF8 == 1 ) {
# UTF8
环境设定
binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');
$ENV{'NLS_LANG'} = "SIMPLIFIED CHINESE_CHINA.AL32UTF8";
} else {
$ENV{'NLS_LANG'} = "SIMPLIFIED CHINESE_CHINA.ZHS16GBK";
}
# 命令行参数
use Getopt::Long qw(:config no_ignore_case);
my $opt_db_user;
my $opt_db_password;
my $opt_db_instance=$ENV{'ORACLE_SID'};
my $opt_connect_url;
my $opt_table_name;
my $opt_table_rows_limit = -1;
my $opt_sql_file;
my $opt_sql_string;
my $opt_output_file = "unload.out";
my $opt_delimiter_str = "|";
my $opt_dos_endline;
my $opt_date_format = 'YYYY-MM-DD';
my $opt_time_format = 'HH24:MI:SS.FF';
my $opt_timestamp_format = 'YYYY-MM-DD HH24:MI:SS.FF';
my $opt_quiet_mode;
my $opt_enclosed_str;
my $opt_want_trim_char;
my $opt_want_format_number;
my $opt_want_head;
my $opt_want_bzip2;
my $opt_want_gzip;
my $opt_want_zip;
my $opt_want_help;
my $opt_batch_size=10000;
¬ify if $#ARGV == -1;
GetOptions(
'user=s'
=> $opt_db_user,
'password=s'
=> $opt_db_password,
'instance=s'
=> $opt_db_instance,
'connecturl=s'
=> $opt_connect_url,
'table=s'
=> $opt_table_name,
'rows=i'
=> $opt_table_rows_limit,
'filequery=s'
=> $opt_sql_file,
'stringquery=s'
=> $opt_sql_string,
'output=s'
=> $opt_output_file,
'delimiter=s'
=> $opt_delimiter_str,