MySQL 轉doris
Mysql 表換成對應的dorid表插入語句:
package sql; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.Charset; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Mysql2doris2 { public static void main(String[] args) throws IOException { genFile(); convert(); } private static void genFile() throws IOException { // 指定要建立或開啟的檔案路徑及名稱 String filePath2 = "E:\newtime.sql"; // 指定要讀取的檔案路徑 BufferedReader reader = new BufferedReader(new FileReader(filePath2,Charset.forName("utf-8"))); File file = new File("E:\\\\\\\\\\\\\\\doc\\\\\\\\temp.sql"); if (file.exists()) { // 判斷檔案是否存在 file.delete(); // 刪除檔案 } else { // 檔案不存在 System.out.println(file.createNewFile()); // 建立新檔案 } String line; while ((line = reader.readLine()) != null) { System.out.println(line); // 輸出每一行內容 // 在這裡進行其他操作或邏輯判斷等 // Pattern pattern = Pattern.compile("\\((.*?)\\)"); Pattern pattern = Pattern.compile("\\((.*?)\\)"); Matcher matcher = pattern.matcher(line); String key = null; // timestamp 改為 datetime if (line.contains("datetime(0)")) { // line = line.replace("timestamp", "datetime"); line = line.replace("datetime(0)", "datetime"); } // numeric(->decimal( if (line.contains("numeric")) { line = line.replace("numeric", "decimal"); } // bpchar->char if (line.contains("bpchar")) { line = line.replace("bpchar", "char"); } if (line.contains("ENGINE")) { while (matcher.find()) { System.out.println("獲取數值=" + matcher.group().replace("(", "").replace(")", "")); key = matcher.group().replace("(", "").replace(")", ""); } // 1.最後追加 line = ""; line = " "; line += ")\n"; line += "unique key(" + key + ")" + " DISTRIBUTED BY HASH(" + key + ") BUCKETS 16" + " PROPERTIES(\"replication_num\" = \"3\");"; } else { //普通行*3 取出括號的數值乘以3 // 獲取數值*3 while (matcher.find()) { System.out.println("獲取數值=" + matcher.group().replace("(", "").replace(")", "")); key = matcher.group().replace("(", "").replace(")", ""); if (isNumber(key)) { int keyInt = Integer.parseInt(key) * 3; line = line.replace(key, "" + keyInt); } else { } } } if (line.startsWith(")")) { line = ""; } if (line.startsWith("COMMENT")) { line = ""; } //包含索引 if(line.startsWith("CREATE INDEX")) { line=" "; } //包含Mysq set 關鍵字去掉 if(line.startsWith("SET NAMES")) { line=""; } if(line.startsWith("SET FOREIGN_KEY_CHECKS")) { line=""; } if(line.startsWith("INSERT INTO")) { line=""; } //刪除CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci if(line.contains("CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci")) { line=line.replace("CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci", ""); } try (BufferedWriter writer = new BufferedWriter(new FileWriter(file,true))) { if (!(line.isBlank() || line.isEmpty())) { // 向檔案寫入資料 // 過濾 if (line != null) { writer.write(line); writer.write("\n"); } } } catch (IOException e) { e.printStackTrace(); } } } public static boolean isNumber(String str) { return str.matches("\\d+"); } public static void convert() throws IOException { // 指定要建立或開啟的檔案路徑及名稱 String filePath = "E:\\\\doc\\temp.sql"; BufferedReader reader = new BufferedReader(new FileReader(filePath)); File file = new File("E:\\doc\\certificate_create_newtime"+"-create.sql"); if (file.exists()) { // 判斷檔案是否存在 file.delete(); // 刪除檔案 } else { // 檔案不存在 System.out.println(file.createNewFile()); // 建立新檔案 } String line; while ((line = reader.readLine()) != null) { System.out.println(line); // 輸出每一行內容 int lastCommaIndex = line.lastIndexOf(","); // 獲取最後一個逗號的索引位置 // 建立 StringBuilder 物件並將原始字串賦值給它 StringBuilder sb = new StringBuilder(line); if (lastCommaIndex >= 0 && lastCommaIndex < sb.length()) { // 呼叫 deleteCharAt() 方法刪除指定索引位置的字元 sb.deleteCharAt(lastCommaIndex); System.out.println("刪除後的字串為:" + sb.toString()); String result = sb.toString().replace("&", "\n"); try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, Charset.forName("UTF-8"), true))) { if (!(line.isBlank() || line.isEmpty())) { // 向檔案寫入資料 // 過濾 if (line != null) { writer.write(result); } } } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("無效的索引"); } } } //記得最後要更改為varchar }
來自 “ ITPUB部落格 ” ,連結:https://blog.itpub.net/69949806/viewspace-3005034/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Doris 的 一鍵 mysql 同步,靠譜不?MySql
- Doris JDBC 外表JDBC
- Flink CDC 系列 - 實現 MySQL 資料實時寫入 Apache DorisMySqlApache
- streampark+flink一鍵整庫或多表同步mysql到doris實戰MySql
- 一文教你玩轉 Apache Doris 分割槽分桶新功能Apache
- Doris安裝部署
- Oracle 轉MySqlOracleMySql
- springboot 使用 doris-streamloader 到doris 防止批次更新 事務卡主Spring Boot
- doris匯入匯出
- Doris開發手記1:解決蛋疼的MySQL 8.0連線問題MySql
- Mysql - 行轉列、列轉行MySql
- 轉MySQL--mysql常用函式打全MySql函式
- Apache Doris 入門 10 問Apache
- [Apache Doris] Apache Doris 後設資料設計及DDL操作原始碼閱讀Apache原始碼
- mysql 轉義問題MySql
- Doris開發手記3:利用CoreDump檔案快速定位Doris的查詢問題
- Apache Doris 2.0.4 版本正式釋出Apache
- Apache Doris 2.0.5 版本正式釋出Apache
- Apache Doris 2.0.5 版本正式釋出!Apache
- Apache Doris 2.0.3 版本正式釋出Apache
- 大資料技術 - Apache Doris大資料Apache
- [玩轉MySQL之四]MySQL快取機制MySql快取
- mysql動態行轉列MySql
- mysql left join轉inner joinMySql
- mysql行列轉換詳解MySql
- MySQL鎖詳解!(轉載)MySql
- mysql基礎 行轉列MySql
- mysql 的Escape轉義字串MySql字串
- [玩轉MySQL之六]MySQL查詢優化器MySql優化
- 使用SeaTunnel從InfluxDB同步資料到DorisUX
- 與創新者同行,Apache Doris in 2023Apache
- Apache Doris 1.2.2 Release 版本正式釋出Apache
- 尚矽谷Doris視訊教程釋出
- 基於Apache Doris的湖倉分析Apache
- Mysql MHA部署-05故障轉移MySql
- 帶你玩玩轉 MySQL 查詢MySql
- mysql隱式轉換問題MySql
- 轉:阿里雲上面部署mysql阿里MySql