解析文件入库方式
public void analysisAndSaveDat(String bussinessType) {
int count = 0;
// Auto-generated method stub
LOGGER.info("文件解析并落地到数据库开始.......");
String encoding = "UTF-8";
// DOTO 切割符
String decollator = "\\|@\\|";
// 通过路径与文件名拼接文件
String datFile = fileUrl(bussinessType,CodeConstant.FILE_TYPE_DAT)+"\\"+filName(bussinessType,CodeConstant.FILE_TYPE_DAT);
LOGGER.info("解析文件的文件路径为:"+datFile);
// 转换成File对象
File file = new File(datFile);
try {
// 将文件中的数据读取出来,并存放进集合中
while(count <3){
importFile(file, encoding, decollator);
count++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void importFile(File file, String encoding, String decollator)
throws IOException, ParseException {
LOGGER.info("解析文件入库开始");
// 链接数据库
Connection con = getCon();
try {
// IO 读取数据【按行读取】
FileInputStream fis = new FileInputStream(file);
Scanner scanner = new Scanner(fis, encoding);
int sum = 0;
// 获取sql脚本
String sql = "insert into T_FLTPSGINFO values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Long startTime = System.currentTimeMillis();
// 通过PreparedStatement 进行批量数据插入
PreparedStatement pst = (PreparedStatement) con
.prepareStatement(sql.toString());
// 批量将数据插入到数据库
while (scanner.hasNext()) {
String lineTxt = scanner.nextLine();
String[] split1 = lineTxt.split(decollator);
//TODO 将一条数据分割之后,分别赋值到pst中
pst.setString(1, split1[0]);
pst.setString(2, split1[1]);
pst.setString(3, split1[2]);
pst.setString(4, split1[3]);
pst.setString(5, split1[4]);
pst.setString(6, split1[5]);
pst.setString(7, split1[6]);
pst.setString(8, split1[7]);
pst.setString(9, split1[8]);
pst.setString(10, split1[9]);
pst.setString(11, split1[10]);
pst.setString(12, split1[11]);
pst.setString(13, split1[12]);
pst.setString(14, split1[13]);
pst.setString(15, split1[14]);
pst.setString(16, split1[15]);
pst.setString(17, split1[16]);
pst.setString(18, split1[17]);
pst.setString(19, split1[18]);
pst.setString(20, split1[19]);
pst.setString(21, split1[20]);
pst.setString(22, split1[21]);
pst.setString(23, split1[22]);
pst.setString(24, split1[23]);
pst.setString(25, split1[24]);
pst.setString(26, split1[25]);
pst.setString(27, split1[26]);
pst.setString(28, split1[27]);
pst.setString(29, split1[28]);
pst.setString(30, split1[29]);
pst.setString(31, split1[30]);
pst.setString(32, split1[31]);
pst.setString(33, split1[32]);
pst.setString(34, split1[33]);
pst.setString(35, split1[34]);
pst.setString(36, split1[35]);
pst.setString(37, split1[36]);
pst.setString(38, split1[37]);
pst.setString(39, split1[38]);
pst.setString(40, split1[39]);
pst.setString(41, split1[40]);
pst.setString(42, split1[41]);
pst.setString(43, split1[42]);
pst.setString(44, split1[43]);
pst.setString(45, split1[44]);
pst.setString(46, split1[45]);
pst.setString(47, split1[46]);
pst.setString(48, split1[47]);
pst.setString(49, split1[48]);
pst.setString(50, split1[49]);
pst.setString(51, split1[50]);
pst.setString(52, split1[51]);
pst.setString(53, split1[52]);
pst.setString(54, split1[53]);
// 添加到缓存中
pst.addBatch();
sum++;
// 如果数据量很大,不能一次性批量添加所以我们要分批次添加,这里就是1000条一次
if(sum%1000==0){
// 持久化
int []res=pst.executeBatch();
// 提交事务,持久化数据
con.commit();
pst.clearBatch();
LOGGER.info("1000整除插入结果: "+res.length);
}
}
// 小1000条的在这里持久化
int []ress= pst.executeBatch();
// 语句执行完毕,提交本事务
con.commit();
pst.clearBatch();
pst.close();
LOGGER.info("插入数据结果:"+ress.length);
pst.executeBatch();
Long endTime = System.currentTimeMillis();
System.out.println(sum + " 用时:" + (endTime - startTime) + "ms");
con.close();
} catch (SQLException e) {
LOGGER.error("数据插入失败"+e.getMessage());
e.printStackTrace();
}
}
这种方式采用分批处理数据,一次插入1000条,不足1000的一次性插入,用时较少

浙公网安备 33010602011771号