`
caohuan346
  • 浏览: 23155 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

ORACLE导入大数据的两种方式比较

阅读更多

1. 采用sqlplus导入*.sql文件

(以10W条数据测试)

1.1. 生成sql脚本

写个简单的JAVA程序如下:

    public class GenerateSQLFile {

    public static void main(String[] args) throws Exception {

// 要操作的文件

    File file = new File("d:" + File.separator + "demo.sql");  OutputStream out = null// 声明字节输出流

    out = new FileOutputStream(file, true); // 通过子类实例化

    for (int i = 1; i <= 100000; i++) {

        String str = "insert into t_test(id,name) values(" + i

        ",'hello world" + i + "');\r\n"// 要输出的信息

        byte b[] = str.getBytes(); // 将String变为byte数组

        out.write(b); // 写入数据

    }

out.close(); // 关闭

}

}

 

执行程序,生成的sql脚本如下所示:

insert into t_test(id,name) values(1,'hello world1');

insert into t_test(id,name) values(2,'hello world2');

insert into t_test(id,name) values(3,'hello world3');

... ...

 

1.2. 新建表,导入数据

scott/tiger登录sqlplus;

新建t_test表:create table t_test(id int,name varchar2(255));

创建成功后执行:@D:\demo.sql,逐行插入数据。

 

1.3. 测试结果

sqlplus执行sql脚本导入10W条数据的时间大约是5分钟。

 

2. sql loader 工具导入

(以100W条测试数据)

2.1. 新建test.csv数据文件

格式如下:

1,hello1

2,hello2

3,hello3

... ...

 

十万条数据的csv文件同样由java小程序生成:

    File file = new File("d:" + File.separator + "demo.txt"); // 要操作的文件

    OutputStream out = null// 声明字节输出流

    out = new FileOutputStream(file, true); // 通过子类实例化

    for (int i = 1; i <= 100000; i++) {

        String str = i+","+"hello"+i+"\r\n"// 要输出的信息

        byte b[] = str.getBytes(); // 将String变为byte数组

        out.write(b); // 写入数据

    }

out.close(); // 关闭

 

2.2. 建立表格

create table test(id varchar2(255),name varchar2(255));

 

2.3. 建立控制文件test.ctl

load data

infile demo.csv

into table test     

(

id char terminated by ',',

name char terminated by whitespace

)

 

PS: infile 指数据源文件 这里我们省略了默认的 discardfile   result.dsc   badfile   result.bad  
 
       into   table   test 默认是INSERT,也可以into   table   resultxt   APPEND为追加方式,或
REPLACE  
 
       terminated   by   ',' 指用逗号分隔
  
 
       terminated   by   whitespace 结尾以空白分隔
  

其它参数参考:d:\>sqlldr  

 

2.4. 导入数据

ctl文件和csv文件放置在D盘根目录下,命令行中运行:

 D:\>sqlldr   userid=scott/tiger  control=test.ctl  

2.5. 测试结果

急速,10W条数据转眼导入结束,100W条数据耗时10s

<!--EndFragment-->

分享到:
评论
1 楼 wangcl011 2014-09-18  
Oracle通过数据泵网络导入另一个数据库,不生成DMP文件,参考文章:http://www.itdatum.net/db/oracle/2014/09/7948.shtml
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics