将大量数据批量插入Oracle表的类,支持停止续传
之前用create table select * from XXTable无疑是创建庞大表的最快方案之一,但是数据重复率是个问题,且数据难以操控。
于是我在之前批量插数据的基础上更新了一个类,让它具有了Resume的能力,这样可以利用碎片时间能插一点是一点。
以后此类还可能改进,先留一个版本在这里。
数据库连接参数类:
class DBParam {
public final static String Driver = "oracle.jdbc.driver.OracleDriver";
public final static String DbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
public final static String User = "ufo";
public final static String Pswd = "1234";
}
HugeTbBatchInserter类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random; class TypeField{
String type;
String field;
} // Insert huge records to a table
public class HugeTbBatchInserter {
private final int BatchSize=250;// Batch insert size,可以根据机器性能提高
private final int Total_Record_Count=100000000;// 最好是BatchSize的整倍数 // 如果是多个表,扩充数组即可
// PK:主键 CH:文字 DT:Datetime,RND:百以内随机数 还可以根据需要扩充代号,在getInsertSql函数中则根据代号来设置值
private final String[][] tableArray= {
{"score:"+Total_Record_Count,"PK:ID","RND:stuid","RND:courseid","RND:score"},
}; /**
* 批量插值
*/
public void batchInsert() {
Connection conn = null;
Statement stmt = null; try{
Class.forName(DBParam.Driver).newInstance();
conn = DriverManager.getConnection(DBParam.DbUrl, DBParam.User, DBParam.Pswd);
stmt = conn.createStatement();
System.out.println("Begin to access "+DBParam.DbUrl+" as "+DBParam.User+"..."); for(String[] innerArr:tableArray) {
String tableName=innerArr[0].split(":")[0];
System.out.println("Table:"+tableName); int existCount=fetchExistCount(tableName,stmt);
System.out.println("Exist record count:"+existCount); int maxId=fetchMaxId(tableName,stmt);
System.out.println("Max id:"+maxId); int count=Integer.parseInt(innerArr[0].split(":")[1])-existCount;
System.out.println("准备向表"+tableName+"插入"+count+"条记录."); // 是否需要插值前先清空,自行判断再放开
//truncateTable(tableName,stmt); // 真正插入数据
insertTestDataTo(tableName,maxId+1,count,innerArr,stmt);
}
} catch (Exception e) {
System.out.print(e.getMessage());
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.print("Can't close stmt/conn because of " + e.getMessage());
}
}
} /**
* 以当前时间为基准减去数十秒
* @param n
* @return
*/
private static String getDatetimeBefore(int n) {
try {
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND,-n*10);//日期减去n*10秒 Date newDate=now.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String retval = sdf.format(newDate);
return retval;
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
} /**
* 清空一个表的数据,注意此功能有破坏性,不可恢复,注意备份好数据
* @param tableName
* @param conn
* @param stmt
* @throws SQLException
*/
private void truncateTable(String tableName,Statement stmt) throws SQLException{
String sql="truncate table "+tableName;
stmt.execute(sql);
System.out.println("truncated table:"+tableName);
} /**
* 得到表中已有的最大ID值
* @param tableName
* @param conn
* @param stmt
* @return
* @throws SQLException
*/
private int fetchMaxId(String tableName,Statement stmt) throws SQLException{
String sql="select max(id) as max from "+tableName+""; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) {
int max = rs.getInt("max");
return max;
} return 0;
} /**
* 得到表中现存数量
* @param tableName
* @param conn
* @param stmt
* @return
* @throws SQLException
*/
private int fetchExistCount(String tableName,Statement stmt) throws SQLException{
String sql="select count(*) as cnt from "+tableName+""; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) {
int cnt = rs.getInt("cnt");
return cnt;
} return 0;
} /**
* 向一个表插入数据
* @param tableName
* @param count
* @param innerArr
* @param conn
* @param stmt
* @throws SQLException
*/
private void insertTestDataTo(String tableName,int startId,int count,String[] innerArr,Statement stmt) throws SQLException{
// 得到字段名和字段类型
List<TypeField> typefields=new ArrayList<TypeField>();
for(int i=1;i<innerArr.length;i++) {
String temp=innerArr[i];
String[] arrTmp=temp.split(":"); TypeField tf=new TypeField();
tf.type=arrTmp[0];
tf.field=arrTmp[1];
typefields.add(tf);
} List<String> fields=new ArrayList<String>();
List<String> values=new ArrayList<String>();
int index=0;
for(TypeField tf:typefields) {
fields.add(tf.field);
values.add("''{"+index+"}''");
index++;
} index=0;
int times=count/BatchSize;
for(int i=0;i<times;i++) {
long startTime = System.currentTimeMillis();
StringBuilder sb=new StringBuilder();
sb.append("INSERT ALL "); for(int j=0;j<BatchSize;j++) {
index=i*BatchSize+j+startId;
sb.append(getInsertSql(tableName,typefields,index));
} sb.append(" select * from dual");
String sql = sb.toString();
stmt.executeUpdate(sql); long endTime = System.currentTimeMillis();
System.out.println("#"+i+"/"+times+" "+BatchSize+" records inserted to Table:'"+tableName+"',time elapsed:"+(endTime-startTime)+"ms.");
}
} /**
* 得到批量插入语句
* @param tableName
* @param typefields
* @param index
* @return
*/
private String getInsertSql(String tableName,List<TypeField> typefields,int index) {
String currTime=getDatetimeBefore(index); StringBuilder sb=new StringBuilder();
sb.append(" INTO "+tableName+"(");
List<String> fields=new ArrayList<String>();
for(TypeField tf:typefields) {
fields.add(tf.field);
}
sb.append(String.join(",",fields)); sb.append(") values(");
List<String> values=new ArrayList<String>();
for(TypeField tf:typefields) {
if(tf.type.equals("PK")) {
values.add("'"+String.valueOf(index)+"'");
}else if(tf.type.equals("CH")) {
values.add("'0'");
}else if(tf.type.equals("RND")) {
values.add("'"+getRND()+"'");
}else if(tf.type.equals("DT")) {
values.add("to_date('"+currTime+"','yyyy-MM-dd HH24:mi:ss')");
}
}
sb.append(String.join(",",values));
sb.append(")"); String insertSql=sb.toString();
return insertSql;
} private static String getRND() {
return getRandom(0,100);
} private static String getRandom(int min, int max){
Random random = new Random();
int s = random.nextInt(max) % (max - min + 1) + min;
return String.valueOf(s);
} /**
* 将秒转化为日时分秒
* @param secondCount
* @return
*/
private static String sec2DHMS(long secondCount) {
String retval = null; long days = secondCount / (60 * 60 * 24);
long hours = (secondCount % (60 * 60 * 24)) / (60 * 60);
long minutes = (secondCount % (60 * 60)) / 60;
long seconds = secondCount % 60; String strSeconds="";
if(seconds!=0) {
strSeconds=seconds + "s";
} if (days > 0) {
retval = days + "d" + hours + "h" + minutes + "m" + strSeconds;
} else if (hours > 0) {
retval = hours + "h" + minutes + "m" + strSeconds;
} else if (minutes > 0) {
retval = minutes + "m" + strSeconds;
} else {
retval = strSeconds;
} return retval;
} public static void main(String[] args) {
HugeTbBatchInserter mi=new HugeTbBatchInserter();
long startTime = System.currentTimeMillis();
mi.batchInsert();
long endTime = System.currentTimeMillis(); System.out.println("Time elapsed:" + sec2DHMS((endTime - startTime)/1000) );
}
}
这个类运行起来是这样的:
Begin to access jdbc:oracle:thin:@127.0.0.1:1521:orcl as ufo...
Table:score
Exist record count:3351500
Max id:3351499
准备向表score插入96648500条记录.
#0/386594 250 records inserted to Table:'score',time elapsed:284ms.
#1/386594 250 records inserted to Table:'score',time elapsed:282ms.
#2/386594 250 records inserted to Table:'score',time elapsed:324ms.
#3/386594 250 records inserted to Table:'score',time elapsed:284ms.
#4/386594 250 records inserted to Table:'score',time elapsed:302ms.
#5/386594 250 records inserted to Table:'score',time elapsed:330ms.
#6/386594 250 records inserted to Table:'score',time elapsed:291ms.
#7/386594 250 records inserted to Table:'score',time elapsed:335ms.
#8/386594 250 records inserted to Table:'score',time elapsed:372ms.
#9/386594 250 records inserted to Table:'score',time elapsed:374ms.
下面这个类虽然更快些,但插入总量有限,需要改进,也留一个版本在这里吧:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat; class DBParam {
public final static String Driver = "oracle.jdbc.driver.OracleDriver";
public final static String DbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
public final static String User = "ufo";
public final static String Pswd = "1234";
}
// Insert records to srcore table
public class ScoreInserter {
private final String Table="score";
private final int Total=1000000; public boolean fillTable() {
Connection conn = null;
Statement stmt = null; try{
Class.forName(DBParam.Driver).newInstance();
conn = DriverManager.getConnection(DBParam.DbUrl, DBParam.User, DBParam.Pswd);
conn.setAutoCommit(false);
stmt = conn.createStatement(); long startMs = System.currentTimeMillis();
clearTable(stmt,conn);
fillDataInTable(stmt,conn); long endMs = System.currentTimeMillis();
System.out.println("It takes "+ms2DHMS(startMs,endMs)+" to fill "+toEastNumFormat(Total)+" records to table:'"+Table+"'.");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.print("Can't close stmt/conn because of " + e.getMessage());
}
} return false;
} private void clearTable(Statement stmt,Connection conn) throws SQLException {
stmt.executeUpdate("truncate table "+Table);
conn.commit();
System.out.println("Cleared table:'"+Table+"'.");
} private void fillDataInTable(Statement stmt,Connection conn) throws SQLException {
StringBuilder sb=new StringBuilder();
sb.append(" Insert into "+Table);
sb.append(" select dbms_random.value(0,200),dbms_random.value(1,10),dbms_random.value(0,101) from dual ");
sb.append(" connect by level<="+Total);
sb.append(" order by dbms_random.random"); String sql=sb.toString();
stmt.executeUpdate(sql);
conn.commit(); } // 将整数在万分位以逗号分隔表示
public static String toEastNumFormat(long number) {
DecimalFormat df = new DecimalFormat("#,####");
return df.format(number);
} // change seconds to DayHourMinuteSecond format
private static String ms2DHMS(long startMs, long endMs) {
String retval = null;
long secondCount = (endMs - startMs) / 1000;
String ms = (endMs - startMs) % 1000 + "ms"; long days = secondCount / (60 * 60 * 24);
long hours = (secondCount % (60 * 60 * 24)) / (60 * 60);
long minutes = (secondCount % (60 * 60)) / 60;
long seconds = secondCount % 60; if (days > 0) {
retval = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
} else if (hours > 0) {
retval = hours + "h" + minutes + "m" + seconds + "s";
} else if (minutes > 0) {
retval = minutes + "m" + seconds + "s";
} else {
retval = seconds + "s";
} return retval + ms;
} public static void main(String[] args) {
ScoreInserter si=new ScoreInserter();
si.fillTable();
}
}
--END-- 2020年1月4日16点57分
将大量数据批量插入Oracle表的类,支持停止续传的更多相关文章
- c#几种数据库的大数据批量插入(SqlServer、Oracle、SQLite和MySql)
这篇文章主要介绍了c#几种数据库的大数据批量插入(SqlServer.Oracle.SQLite和MySql),需要的朋友可以了解一下. 在之前只知道SqlServer支持数据批量插入,殊不知道Ora ...
- C#中几种数据库的大数据批量插入
C#语言中对SqlServer.Oracle.SQLite和MySql中的数据批量插入是支持的,不过Oracle需要使用Orace.DataAccess驱动. IProvider里有一个用于实现批量插 ...
- C#:几种数据库的大数据批量插入
在之前只知道SqlServer支持数据批量插入,殊不知道Oracle.SQLite和MySql也是支持的,不过Oracle需要使用Orace.DataAccess驱动,今天就贴出几种数据库的批量插入解 ...
- C#:几种数据库的大数据批量插入(转)
在之前只知道SqlServer支持数据批量插入,殊不知道Oracle.SQLite和MySql也是支持的,不过Oracle需要使用Orace.DataAccess驱动,今天就贴出几种数据库的批量插入解 ...
- C#:几种数据库的大数据批量插入 - faib
在之前只知道SqlServer支持数据批量插入,殊不知道Oracle.SQLite和MySql也是支持的,不过Oracle需要使用Orace.DataAccess驱动,今天就贴出几种数据库的批量插入解 ...
- SQL SERVER 使用BULK Insert将txt文件中的数据批量插入表中(1)
1/首先建立数据表 CREATE TABLE BasicMsg( RecvTime FLOAT NOT NULL , --接收时间,不存在时间相同的数据 AA INT NOT NULL, --24位地 ...
- c#数据批量插入
由于之前面试中经常被问到有关EF的数据批量插入问题,今天以Sqlserver数据库为例,对.net中处理数据批量处理的方案进行了测试对比. 1.四种测试方案 (1)普通的EF数据批量插入:即调用DbS ...
- PHP如何将多维数组中的数据批量插入数据库?
PHP将多维数组中的数据批量插入到数据库中,顾名思义,需要用循环来插入. 1.循环insert into 语句,逐渐查询 <?php /* www.qSyz.net */ @mysql_conn ...
- mybatis批量插入oracle时报错:unique constraint (table name) violated
mybatis批量插入oracle时报错:unique constraint (table name) violated,是因为插入的集合中有两条相同唯一约束的数据.
随机推荐
- Vuex mapGetter的基本使用
getter相当于Vuex中的计算属性 对 state 做处理再返回 mapGetters 把 Store 中的 getters 映射到组件中的计算属性中 Store文件 import Vue fro ...
- Flutter 容器(5) - SizedBox
SizedBox: 两种用法:一是可用来设置两个widget之间的间距,二是可以用来限制子组件的大小. import 'package:flutter/material.dart'; class Au ...
- Vue 页面导出成PDF文件
注意事项 如果导出的页面中设计到图片或者其他文件跨域文件,需要后端服务配合 安装依赖 npm install html2Canvas --save npm install jspdf--save 封装 ...
- 快速入门Mybatis
框架概述 什么是框架 它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题.使用框架的好处:框架封装了很多的细节,使开发者可以使用极简的方式实现功能.大大提高开发效率 三层架构 UI(表现层 ...
- 5G从小就梦想着自己要迎娶:高速率、低时延、大容量三个老婆
摘要:2020年7月9日,ITU正式把NB-IoT纳入5G标准体系! 高速率.低时延与5G是青梅竹马的关系,在大容量的选择上,5G与NB-IoT不断传出着绯闻,终于:2020年7月9日,ITU正式把N ...
- MSF常用命令备忘录
msf下的命令 set session x:设置要攻击的session #监听端口反弹PHP shell use exploit/multi/handler set payload php/meter ...
- 封装react antd的upload上传组件
上传文件也是我们在实际开发中常遇到的功能,比如上传产品图片以供更好地宣传我们的产品,上传excel文档以便于更好地展示更多的产品信息,上传zip文件以便于更好地收集一些资料信息等等.至于为何要把上传组 ...
- 微信小程序setData局部刷新列表
利用setData局部刷新列表 当列表管理加载到第几页时,这个list的数据有十几条的,如果重新setData的话就要重新刷新和渲染列表, 这是个比较麻烦的事,当数据量大时,就会造成白屏, 这时就要局 ...
- 真正从底层理解 Synchronized 实现原理
实现synchronized的基础有两个:Java 对象头和 Monitor. 在虚拟机规范中定义了对象在内存中的布局,主要由以下 3 部分组成: 对象头 实例数据 对齐填充 而synchronize ...
- JavaScript学习系列博客_35_JavaScript 正则表达式的使用
正则表达式的使用 先说RegExp对象的一个方法 test() - 使用这个方法可以用来检查一个字符串是否符合正则表达式的规则,如果符合则返回true,否则返回false. 1.用正则表达式来检查一个 ...