ITPub博客

首页 > 数据库 > 数据库开发技术 > MySQL 转doris

MySQL 转doris

原创 数据库开发技术 作者:mcxiaoracle 时间:2024-01-24 23:19:11 0 删除 编辑

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/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
数据方向,了解oracle ,IBM DB2 730

注册时间:2019-09-30

  • 博文量
    286
  • 访问量
    226692