|
@@ -7,17 +7,16 @@ import cn.hutool.core.io.FileUtil;
|
|
|
import com.example.bigsql.entity.UploadBigsqlFileRecordBaseVO;
|
|
|
import com.example.bigsql.util.ExecuteShellUtil;
|
|
|
import com.example.bigsql.util.JyDbUtil;
|
|
|
-import com.example.bigsql.util.UnZipUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.*;
|
|
|
-import java.nio.file.Files;
|
|
|
-import java.nio.file.Paths;
|
|
|
+import java.nio.charset.Charset;
|
|
|
import java.sql.*;
|
|
|
import java.util.Date;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
|
|
|
/**
|
|
|
* 解析v3版大sql文件
|
|
@@ -215,82 +214,76 @@ public class CreateDatabasesByV3File {
|
|
|
//场站编号
|
|
|
String name = f.getName().substring(0, 6);
|
|
|
String csvFilePath = "/home/bigsql/csvdir/" + name + File.separator;
|
|
|
- String sqlPath = "/home/bigsql/sqldir/";
|
|
|
- //String sqlPath = "/Users/xiaowang/个人/sqldir/";
|
|
|
//String csvFilePath = "/Users/xiaowang/个人/csvdir/" + name + File.separator;
|
|
|
//判断csvFilePath这个目录是否存在 如果不存在则创建目录
|
|
|
if (!new File(csvFilePath).exists()) {
|
|
|
new File(csvFilePath).mkdirs();
|
|
|
}
|
|
|
- if (!new File(sqlPath).exists()) {
|
|
|
- new File(sqlPath).mkdirs();
|
|
|
- }
|
|
|
String databasesName = "";
|
|
|
//获取文件输入流
|
|
|
- BufferedInputStream input = new BufferedInputStream(Files.newInputStream(Paths.get(f.getPath())));
|
|
|
- //扫描解压目录下的sql文件 正常没有数据或只有一条数据
|
|
|
- UnZipUtil.decompressZip(f.getPath(), sqlPath);
|
|
|
- File sFile = new File(sqlPath);
|
|
|
- File[] sFiles = searchFile(sFile, ".sql");
|
|
|
- if (sFiles.length == 0) {
|
|
|
- log.error(f.getName() + "压缩文件为空");
|
|
|
- break;
|
|
|
- }
|
|
|
- //循环遍历
|
|
|
- try {
|
|
|
- try (BufferedReader reader = new BufferedReader(new FileReader(sFiles[0]))) {
|
|
|
- List<String> sqlStatements = new ArrayList<>();
|
|
|
- StringBuilder statementBuilder = new StringBuilder();
|
|
|
- String line;
|
|
|
- int lineSize = 0;
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
- if (line.startsWith("Navicat") || line.startsWith("/")
|
|
|
- || line.startsWith("INSERT") || line.startsWith("--") || line.startsWith("#")
|
|
|
- || line.startsWith("//")) {
|
|
|
- lineSize++;
|
|
|
- //sql内的数据转换成csv文件
|
|
|
- spliteFileToCsv(line, fileWriterMap, csvFilePath);
|
|
|
- continue; // Skip comments
|
|
|
- }
|
|
|
- if (line.endsWith(";")) {
|
|
|
- statementBuilder.append(line); // Remove semicolon
|
|
|
- sqlStatements.add(statementBuilder.toString().trim()); // Add to list
|
|
|
- statementBuilder.setLength(0); // Reset statement buffer
|
|
|
- } else {
|
|
|
- statementBuilder.append(line).append(" ");
|
|
|
- }
|
|
|
- }
|
|
|
- sqlStatements = sqlStatements.stream().filter(s -> s.startsWith("CREATE") || s.startsWith("DROP")).collect(Collectors.toList());
|
|
|
- databasesName = "ipfcst_" + name + "_" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_FORMAT);
|
|
|
- Connection conn = JyDbUtil.conn;
|
|
|
+ //BufferedInputStream input = new BufferedInputStream(Files.newInputStream(Paths.get(f.getPath())));
|
|
|
|
|
|
- int num = 0;
|
|
|
- while (!flag) {
|
|
|
- if (createDatabase(databasesName, conn)) {
|
|
|
- if (createTable(databasesName, sqlStatements, conn)) {
|
|
|
- flag = true;
|
|
|
- log.info("执行数据库和数据表创建成功,其中数据共:{}", lineSize);
|
|
|
+ FileInputStream input = new FileInputStream(f.getPath());
|
|
|
+ //获取ZIP输入流(一定要指定字符集Charset.forName("GBK")否则会报java.lang.IllegalArgumentException: MALFORMED)
|
|
|
+ ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));
|
|
|
+ if (zipInputStream.getNextEntry() != null) {
|
|
|
+ //循环遍历
|
|
|
+ try {
|
|
|
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(zipInputStream))) {
|
|
|
+ List<String> sqlStatements = new ArrayList<>();
|
|
|
+ StringBuilder statementBuilder = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ int lineSize = 0;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ if (line.startsWith("Navicat") || line.startsWith("/")
|
|
|
+ || line.startsWith("INSERT") || line.startsWith("--") || line.startsWith("#")
|
|
|
+ || line.startsWith("//")) {
|
|
|
+ lineSize++;
|
|
|
+ //sql内的数据转换成csv文件
|
|
|
+ spliteFileToCsv(line, fileWriterMap, csvFilePath);
|
|
|
+ continue; // Skip comments
|
|
|
+ }
|
|
|
+ if (line.endsWith(";")) {
|
|
|
+ statementBuilder.append(line); // Remove semicolon
|
|
|
+ sqlStatements.add(statementBuilder.toString().trim()); // Add to list
|
|
|
+ statementBuilder.setLength(0); // Reset statement buffer
|
|
|
+ } else {
|
|
|
+ statementBuilder.append(line).append(" ");
|
|
|
}
|
|
|
- log.info("执行数据库和数据表创建失败,第{}次重复执行", lineSize + 1);
|
|
|
- num++;
|
|
|
}
|
|
|
- if (num == 2) {
|
|
|
- break;
|
|
|
+ sqlStatements = sqlStatements.stream().filter(s -> s.startsWith("CREATE") || s.startsWith("DROP")).collect(Collectors.toList());
|
|
|
+ databasesName = "ipfcst_" + name + "_" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_FORMAT);
|
|
|
+ Connection conn = JyDbUtil.conn;
|
|
|
+
|
|
|
+ int num = 0;
|
|
|
+ while (!flag) {
|
|
|
+ if (createDatabase(databasesName, conn)) {
|
|
|
+ if (createTable(databasesName, sqlStatements, conn)) {
|
|
|
+ flag = true;
|
|
|
+ log.info("执行数据库和数据表创建成功,其中数据共:{}", lineSize);
|
|
|
+ }
|
|
|
+ log.info("执行数据库和数据表创建失败,第{}次重复执行", lineSize + 1);
|
|
|
+ num++;
|
|
|
+ }
|
|
|
+ if (num == 2) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
+ } catch (IOException e) {
|
|
|
+ updateRecords(list.get(0).getId(), "2", databasesName);
|
|
|
+ throw new RuntimeException("读取Sql文件失败: " + f.getName(), e);
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- updateRecords(list.get(0).getId(), "2", databasesName);
|
|
|
- throw new RuntimeException("读取Sql文件失败: " + f.getName(), e);
|
|
|
- }
|
|
|
- input.close();
|
|
|
- //修改cloud数据库表里的状态
|
|
|
- if (flag) {
|
|
|
- updateRecords(list.get(0).getId(), "1", databasesName);
|
|
|
+ //修改cloud数据库表里的状态
|
|
|
+ if (flag) {
|
|
|
+ updateRecords(list.get(0).getId(), "1", databasesName);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("读取zip文件时异常" + e);
|
|
|
+ } finally {
|
|
|
+ //一定记得关闭流
|
|
|
+ zipInputStream.close();
|
|
|
+ input.close();
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
- log.info("读取zip文件时异常" + e);
|
|
|
- } finally {
|
|
|
- input.close();
|
|
|
}
|
|
|
if (flag) {
|
|
|
log.info("表结构导入完毕,开始进行表数据导入");
|
|
@@ -302,7 +295,7 @@ public class CreateDatabasesByV3File {
|
|
|
}
|
|
|
}
|
|
|
log.info("数据导入完毕!!!");
|
|
|
- FileUtil.del(sqlPath);
|
|
|
+ //FileUtil.del(sqlPath);
|
|
|
FileUtil.del(csvFilePath);
|
|
|
FileUtil.move(f, new File(path + File.separator + "backUps" + DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN) + File.separator + f.getName()), true);
|
|
|
}
|