读取本地文件,每行为一条记录,文件大小550M,200万条数据。先将文件读取的内存中,再开启6个线程连接postgresql不同coordinator端口导入数据。代码如下:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; public class InsertThread implements Runnable { public static void main(String[] args) {
// String file = "f://weibo.200w";
// String tb = "tb2";
// String ip = "102";
String file = args[0];
String tb = args[1];
String ip = args[2];
String[] port = { "2341", "2342", "2343", "2344", "2345", "2346" };
List<String> list = null;
try {
// 获得源数据
list = getContent(file);
System.out.println(list.size());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} InsertThread myThread1 = new InsertThread();
myThread1.setPramater(tb, ip, port[0], list);
Thread thread1 = new Thread(myThread1); InsertThread myThread2 = new InsertThread();
myThread2.setPramater(tb, ip, port[1], list);
Thread thread2 = new Thread(myThread2); InsertThread myThread3 = new InsertThread();
myThread3.setPramater(tb, ip, port[2], list);
Thread thread3 = new Thread(myThread3); InsertThread myThread4 = new InsertThread();
myThread4.setPramater(tb, ip, port[3], list);
Thread thread4 = new Thread(myThread4); InsertThread myThread5 = new InsertThread();
myThread5.setPramater(tb, ip, port[4], list);
Thread thread5 = new Thread(myThread5); InsertThread myThread6 = new InsertThread();
myThread6.setPramater(tb, ip, port[5], list);
Thread thread6 = new Thread(myThread6); thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
} private String tb;
private String ip;
private String port;
private List<String> list; public void setPramater(String tb, String ip, String port, List<String> list) {
this.tb = tb;
this.ip = ip;
this.port = port;
this.list = list;
} public void run() {
PreparedStatement ps = null;
String sql = null;
Connection conn = null; try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://192.168.8." + ip + ":" + port
+ "/postgres";
try {
conn = DriverManager.getConnection(url, "postgres", "postgres");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
int count = 0;
try {
conn.setAutoCommit(false);
sql = "insert into "
+ tb
+ " values(?,?,?,?,?,?,?::timestamptz,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
Long beginTime = System.currentTimeMillis();
Long begin = System.currentTimeMillis(); for (int i = 0; i < list.size(); i++) {
String[] con = list.get(i).split(",", -1);
if (con.length != 38) {
continue;
}
count++;
try {
for (int j = 0; j < con.length; j++) {
if (con[j] == null) {
ps.setString(j + 1, "NULL");
} else {
ps.setString(j + 1, con[j].trim());
}
}
ps.addBatch();
if (count > 0 && count % 10000 == 0) {// 可以设置不同的大小;如50,100,500,1000等等
ps.executeBatch();
conn.commit();
ps.clearBatch();
Long midTime = System.currentTimeMillis();
System.out.println("-----------------" + count);
System.out.println("导入1万条数据性能" + (10000 * 1000)
/ (midTime - begin));
begin = midTime;
}
} catch (SQLException e) {
e.printStackTrace();
continue;
}
}
try {
ps.executeBatch();
conn.commit();
ps.clearBatch();
} catch (SQLException e) {
e.printStackTrace();
} long sum = 1000 * count;
long endTime = System.currentTimeMillis(); System.out.println("pst+batch:" + count + "条");
System.out.println("pst+batch:" + (endTime - beginTime) / 1000 + "秒");
System.out.println("pst+batch:" + sum / (endTime - beginTime) + "条/秒"); if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static List<String> getContent(String file) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "UTF-8"));
String line = br.readLine();
List<String> list = new ArrayList<String>();
while (line != null) {
list.add(line);
line = br.readLine();
}
br.close();
return list;
}
}

  

java多线程向数据库中加载数据的更多相关文章

  1. ArcGIS Engine中加载数据

    ArcGIS Engine中加载数据 http://blog.csdn.net/gisstar/article/details/4206822   分类: AE开发积累2009-05-21 16:49 ...

  2. Flexigrid从对象中加载数据

    (有问题,在找…………) Flexigrid是用来动态加载数据的一种比较好(老)的Jquery表插件,然后有些时候,我们需要其从本地或者jQuery对象中加载数据,比如有这么个需求,页面显示中有两个表 ...

  3. ML.NET Cookbook --- 1.如何从文本文件中加载数据?

    使用ML.NET中的TextLoader扩展方法从文本文件中加载数据.你需要知道在文本文件中数据列在那里,它们的类型是什么,在文本文件中什么位置可以找到它们. 请注意:对于ML.NET只读取文件的某些 ...

  4. 机器学习:从sklearn中加载数据

    一.sklearn模块 sklearn模块下有很多子模块,常用的数据集在:sklearn.datasets模块下: 通过数据集中DESCR来查看数据集的文档: 从datasets中加载数据: impo ...

  5. OGG初始化之使用数据库实用程序加载数据

    Loading Data with a Database Utility 要使用数据库复制实用程序建立目标数据,您需要启动更改同步提取组,以便在数据库实用程序创建并应用数据的静态副本时提取正在进行的数 ...

  6. Java读取oracle数据库中blob字段数据文件保存到本地文件(转载)

    转自:https://www.cnblogs.com/forever2698/p/4747349.html package com.bo.test; import java.io.FileOutput ...

  7. python数据可视化-matplotlib入门(6)-从文件中加载数据

    前几篇都是手动录入或随机函数产生的数据.实际有许多类型的文件,以及许多方法,用它们从文件中提取数据来图形化. 比如之前python基础(12)介绍打开文件的方式,可直接读取文件中的数据,扩大了我们的数 ...

  8. 向treeview中加载数据

    1.获取树节点的值,用事件AfterSelect加载(id值的获取,用name来获取) 2.双击treeview控件得到 private void treeView1_AfterSelect(obje ...

  9. Vue完成页面切换中加载数据

    created() { // 拿到路由传递来的car主键 let pk = this.$route.query.pk || this.$route.params.pk; // 主键不存在,就直接结束方 ...

随机推荐

  1. Hyper-V和Virtual PC的不同

    微软在2003年收购了推出了Virtual PC软件的Connectix公司,并在其后推出了Virtual Server服务器虚拟化软件 Hyper-V跟微软自家的Virtual PC.Virtual ...

  2. Flume数据传输事务分析[转]

    本文基于ThriftSource,MemoryChannel,HdfsSink三个组件,对Flume数据传输的事务进行分析,如果使用的是其他组件,Flume事务具体的处理方式将会不同.一般情况下,用M ...

  3. String.resize()

    void resize (size_t n); void resize (size_t n, char c); 测试代码: // resizing string #include <iostre ...

  4. bzoj 2428: [HAOI2006]均分数据

    #include<cstdio> #include<iostream> #include<cstdlib> #include<ctime> #inclu ...

  5. Windows7下U盘安装Ubuntu14.04双系统

    1.准备工作 (1)下载Ubuntu14.04系统镜像文件,Ultraiso,EasyBcd,分区助手 Ubuntu14.04地址:http://www.ubuntu.com/download/des ...

  6. js打印数组查看

    alert() 是不能查看数组,对象的console.log(数组变量); 然后你用火狐的friebug 在控制台查看

  7. 纯JS省市区三级联动

    代码下载

  8. C++质因式分解

    分解质因数是将一个数差分成为几个质数相乘,本函数n初始取2 void prim(int m, int n) { if (m > n) { while (m%n) n++; m/=n; prim( ...

  9. mybatis写mapper文件注意事项(转)

    原文链接:http://wksandy.iteye.com/blog/1443133 xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响 < < > & ...

  10. public protected default private

    简单来说,如果让一个变量或者方法,只想让自己类中的访问,那么就将它们设置成private 如果你想让一个变量或者方法,本包中的类可以访问,而且子类也可访问,但是包外的缺不想让他访问.就设置成prote ...