IO流讀取資料檔案,將資料寫入資料庫,並記錄資料匯入日誌
阿新 • • 發佈:2018-11-02
流程分析:
資料型別:
ROUTE_ID,LXBM,ROAD_NAME,SRC_LON,SRC_LAT,DEST_LON,DEST_LAT
10000,G50,滬渝高速,115.8605349,30.08934467,115.5437817,30.08898601
10001,G50,滬渝高速,115.5437817,30.08898601,115.2825297,30.28938191
需求分析:資料檔名就是資料庫表名,資料型別大概就是第一行是欄位名(但是裡面的欄位名不一定跟資料庫中名字完全匹配,可能多了,可能少),第二行以及後面都是對應的資料。
需求設計:
- 第一步:找到檔案存放的指定資料夾
- 第二步:迴圈讀取這些檔案,獲取檔名並且去掉字尾
- 第三步:將檔名去掉_並且全部轉為小寫。
- 第四步:匹配對應的資料庫
- 第五步:讀取檔案資料第一行,並用map儲存欄位名對應的位置index
- 第六步:繼續讀取下面的資料,並將資料通過逗號分隔,獲取list,根據欄位所在位置獲取資料
routeZonesTest.setDestLat(Double.valueOf(split.get(map.get("destlat"))));
- 第七步:將所有需要插入資料庫的資料放在一個list中,只到一個檔案中的資料讀完。(這裡採用批量插入效率會高很多)
- 第八步:將list資料插入對應的資料
- 第九步:將已經讀取並且插入到資料庫的檔案移動到別的資料夾。
- 第十步:記錄資料插入情況
下面是程式碼:
public Result<?> importDB() { List<String> filesPath = new ArrayList<String>(); File files = new File(BaseConst.file_data_path); File[] tempList = files.listFiles(); continueOut: for (int i = 0; i < tempList.length; i++) { // 如果目標檔案 是檔案 int line = 1; String fileName = tempList[i].getName(); if (tempList[i].isFile()) { System.out.println("文 件:" + tempList[i].getName().substring(0, tempList[i].getName().lastIndexOf("."))); filesPath.add(tempList[i].toString()); File file = new File(tempList[i].toString()); try { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK")); String tempString = null; Map<String, Integer> map = new HashMap<>(); // 記錄下每個欄位對應的位置 while ((tempString = reader.readLine()) != null) { List<String> split = Arrays.asList(tempString.split(",")); if (line == 1) { System.out.println(tempString); for (int s = 0; s < split.size(); s++) { map.put(split.get(s).replace("_", "").replace(" ", "").toLowerCase(), s); } } break; } // 匹配該檔案屬於哪個資料庫 String fileNameNew = fileName.substring(0, tempList[i].getName().lastIndexOf(".")).replace("_", "") .replace(" ", "").toLowerCase(); if (fileNameNew.equals("routezonestest")) { List<RouteZonesTest> routeZonesTestList = new ArrayList<>(); while ((tempString = reader.readLine()) != null) { try { RouteZonesTest routeZonesTest = new RouteZonesTest(); List<String> split = Arrays.asList(tempString.split(",")); routeZonesTest.setDestLat(Double.valueOf(split.get(map.get("destlat")))); routeZonesTest.setDestLon(Double.valueOf(split.get(map.get("destlon")))); routeZonesTest.setRoadName(split.get(map.get("roadname"))); if (map.get("routeid") == null) { dataImportLog(0, 0, "Missing primary key columns", fileName); continue continueOut; } if (split.get(map.get("routeid")) == null) { dataImportLog(0, line, "The " + line + " row primary key does not exist.", fileName); continue continueOut; } routeZonesTest.setRouteid(Integer.valueOf((split.get(map.get("routeid"))))); routeZonesTest.setSrcLat(Double.valueOf(split.get(map.get("srclat")))); routeZonesTest.setSrcLon(Double.valueOf(split.get(map.get("srclon")))); routeZonesTestList.add(routeZonesTest); line++; } catch (Exception e) { dataImportLog(0, 0, "The "+line+" row data has problems.", fileName); continue continueOut; } } // 批量插入資料庫 try { routezonestest.insertDataBatch(routeZonesTestList); } catch (Exception e) { dataImportLog(0, line+1, "Data insertion failed. ", fileName); continue; } } else if (fileNameNew.equals("routezonestest2")) { List<RouteZonesTest2> routeZonesTest2List = new ArrayList<>(); while ((tempString = reader.readLine()) != null) { try { RouteZonesTest2 routeZonesTest2 = new RouteZonesTest2(); List<String> split = Arrays.asList(tempString.split(",")); routeZonesTest2.setDestLat(Double.valueOf(split.get(map.get("destlat")))); routeZonesTest2.setDestLon(Double.valueOf(split.get(map.get("destlon")))); routeZonesTest2.setRoadName(split.get(map.get("roadname"))); if (map.get("routeid") == null) { dataImportLog(0, 0, "Missing primary key columns", fileName); continue continueOut; } if (split.get(map.get("routeid")) == null) { dataImportLog(0, line, "The " + line + " row primary key does not exist.", fileName); continue continueOut; } routeZonesTest2.setRouteid(Integer.valueOf((split.get(map.get("routeid"))))); routeZonesTest2.setSrcLat(Double.valueOf(split.get(map.get("srclat")))); routeZonesTest2.setSrcLon(Double.valueOf(split.get(map.get("srclon")))); routeZonesTest2List.add(routeZonesTest2); line++; } catch (ArrayIndexOutOfBoundsException e) { dataImportLog(0, 0, "The "+line+" row data has problems.", fileName); continue continueOut; } } reader.close(); // 批量插入資料庫 try { routezonestest2.insertDataBatch(routeZonesTest2List); } catch (Exception e) { dataImportLog(0, line, "Data insertion failed. ", fileName); continue; } } else {// 沒有找到對應的資料庫 dataImportLog(0, line, "The filename does not correspond to the database name.", fileName); continue; } } catch (IOException e) { e.printStackTrace(); } // 移動檔案 Result<?> removeFile = RemoveFile(file, BaseConst.file_data_used_path); if (removeFile.getCode() != 200) { dataImportLog(0, line, removeFile.getMsg(), fileName); } else { dataImportLog(1, line, null, fileName); } } // 如果目標檔案是資料夾 if (tempList[i].isDirectory()) { try { dataImportLog(0, 0, "The target is a folder, not a file", fileName); } catch (Exception e) { dataImportLog(0, 0, "The target is a folder, not a file, and the mobile file fails.", fileName); } } } return Result.returnResult(); } private void dataImportLog(int status, int line, String reason, String fileName) { ImportDataLog importDataLog = new ImportDataLog(); importDataLog.setStatus(0); importDataLog.setDataNumber(line); importDataLog.setReason(reason); importDataLog.setId(UUIDUtil.getUUID22()); importDataLog.setFileName(fileName); importDataLog.setFileUsedName(fileName + "_" + DateUtil.formatDateTime(new Date())); importDataLog.setCreateTime(new Date()); importDataLogMapper.insertSelective(importDataLog); } private Result<?> RemoveFile(File file, String destinationFloderUrl) { File destFloder = new File(destinationFloderUrl); // 檢查目標路徑是否合法 if (destFloder.exists()) { if (destFloder.isFile()) { return Result.returnErrorResult("The target path is a file. Please check the target path!"); } } else { if (!destFloder.mkdirs()) { return Result.returnErrorResult("Target folder does not exist, creation failed!"); } } // 檢查原始檔是否合法 if (file.isFile() && file.exists()) { String destinationFile = destinationFloderUrl + "\\" + file.getName(); if (!file.renameTo(new File(destinationFile))) { return Result.returnErrorResult("Failed to move files!"); } } else { return Result.returnErrorResult("The backup file path is incorrect, and the migration fails."); } return Result.returnResult(); }
日誌記錄:
由於插入資料肯定會因為資料存在問題,或者檔案型別以及檔名存在問題而導致插入不成功,所以將這幾種異常情況需要處理並且記錄到日誌檔案中
- 情況1:資料檔案中,資料庫需要的主鍵列沒有,則這個資料插入肯定失敗,然後將插入日誌寫入資料庫,檔案不轉移,並且繼續執行下一個檔案的資料插入
if (map.get("routeid") == null) { dataImportLog(0, 0, "Missing primary key columns", fileName); continue continueOut; }
- 情況1:資料檔案中,某一條資料沒有資料,或者資料庫不夠,沒有找到主鍵對應的資料,則這個資料插入失敗,然後將插入日誌寫入資料庫(同時記錄在哪一行失敗的),檔案不轉移,並且繼續執行下一個檔案的資料插入
if (split.get(map.get("routeid")) == null) { dataImportLog(0, line, "The " + line + " row primary key does not exist.", fileName); continue continueOut; }
- 情況3:儘管在講資料放入list時做了異常情況處理,但是還是多加一個catch來補貨異常
} catch (Exception e) { dataImportLog(0, 0, "The "+line+" row data has problems.", fileName); continue continueOut; }
- 情況4:在將已經封裝好的list資料插入資料庫中也可能存在異常
try { routezonestest.insertDataBatch(routeZonesTestList); } catch (Exception e) { dataImportLog(0, line+1, "Data insertion failed. ", fileName); continue; }
- 情況5:沒有找到對應的資料庫
dataImportLog(0, line, "The filename does not correspond to the database name.", fileName); continue;
- 情況6:目標不是檔案,而是資料夾
dataImportLog(0, 0, "The target is a folder, not a file", fileName);
- 情況7::移動檔案失敗(失敗的原因有很多,具體見RemoveFile方法)
dataImportLog(0, line, removeFile.getMsg(), fileName);
批量插入sql
<insert id="insertDataBatch" parameterType="java.util.List" >
insert into ROUTE_ZONES_TEST2 (ROUTEID, ROAD_NAME, SRC_LAT, SRC_LON, DEST_LAT, DEST_LON )
values
<foreach collection="list" item="bean" separator=",">
(#{bean.routeid},#{bean.roadName},#{bean.srcLat},#{bean.srcLon},#{bean.destLat},#{bean.destLon})
</foreach>
</insert>