Hutool读取CSV的方法
Hutool读取CSV的方法
public static void finallyCount(String path) throws IOException {
String filepath = "/home/wuliu/import_data/raw_data";
String outpath = "/home/wuliu/import_data/input";
String filename = path; // 假设path已经定义并赋值
// 构建完整文件路径
String filePathWithFilename = String.join(File.separator, filepath, filename);
System.out.println(filePathWithFilename);
// 提取日期标记
String dateMark = filename.replace(".csv", "").split("_")[1];
System.out.println(dateMark); // 输出20221212日期
List<String[]> dataRows = new ArrayList<>();
CsvReader reader = CsvUtil.getReader();
CsvData data = reader.read(FileUtil.file(filePathWithFilename), CharsetUtil.CHARSET_UTF_8);
for (CsvRow row : data) {
//System.out.println(row.get(10).toString());
if (row.size() > 1) { // 假设至少有两列以上,去掉最后一列
String[] newRow = new String[row.size() - 1];
for (int i = 0; i < row.size() - 1; i++) {
newRow[i] = row.get(i);
}
dataRows.add(newRow);
}
}
String url = "jdbc:mysql://ip:port/xita";
String username = "username";
String password = "pass";
try (Connection connection = DriverManager.getConnection(url, username, password)) {
//String insertStatement = "INSERT INTO your_table_name (waybill, waybill_new, number, freight, collection, goodsname, goodsname_new, goodscode, goodsname_level1, goodsname_level2, startpoint, startpoint_new, startpoint_city, startpoint_province, endpoint, endpoint_new, endpoint_city, endpoint_province, receivedate, outbounddate, logisticscom, reserve1, reserve2, reserve3, reserve4, reserve5, reserve6,logisticsvip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?)";
String insertStatement = "INSERT INTO your_table_name (waybill, waybill_new, number, freight, collection, goodsname, goodsname_new, goodscode, goodsname_level1, goodsname_level2, startpoint, startpoint_new, startpoint_city, startpoint_province, endpoint, endpoint_new, endpoint_city, endpoint_province, receivedate, outbounddate, logisticscom, reserve1, reserve2, reserve3, reserve4, reserve5, reserve6, logisticsvip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertStatement);
int batchSize = 5000;
int count = 0;
for (String[] newRow : dataRows) {
System.out.println(newRow.length);
//27
for (int i = 0; i < newRow.length-1; i++) { // 将条件改为 i < newRow.length - 1
//System.out.println(i+1);
preparedStatement.setString(i+1, newRow[i]);
}
preparedStatement.addBatch();
count++;
if (count % batchSize == 0) {
preparedStatement.executeBatch(); // 当记录数达到5000时执行批处理
}
}
// 处理剩余不足5000条的数据
if (count % batchSize != 0) {
preparedStatement.executeBatch();
}
/* for (String[] newRow : dataRows) {
System.out.println(newRow.length);
//27
for (int i = 0; i < newRow.length-1; i++) { // 将条件改为 i < newRow.length - 1
preparedStatement.setString(i+1, newRow[i]);
}
//preparedStatement.setString(newRow.length, newRow[newRow.length - 1]); // 单独处理最后一个参数
preparedStatement.executeUpdate();
}*/
} catch (SQLException e) {
e.printStackTrace();
}
// System.out.println(dataRows.toString());
}