首页 > 数据库 > 数据库开发技术 > 关系表转dooris 的java 脚本
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 Main {
public static void main(String[] args) throws IOException {
genFile();
convert();
}
private static void genFile() throws IOException {
// 指定要创建或打开的文件路径及名称
String filePath2 = "E:\\XXXX\\ct.sql"; // 指定要读取的文件路径
BufferedReader reader = new BufferedReader(new FileReader(filePath2));
File file = new File("E:\\\\\\\\XXXX\\\\\\\\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;
if (line.startsWith("CREATE")) {
String[] data1 = line.split(" ");
String name = data1[2];
if (name.contains(".")) {
String[] names = name.split("\\.");
String tableName = names[1];
String tableNames = "`" + tableName.trim() + "`";
line = line.replace(names[0] + ".", ""); //去除表名
line=line.replace(tableName, tableNames);
}
}
// 加引号
String[] data = line.split(" ");
String filed = data[0];
if (filed.length() > 0 && !filed.startsWith("--") && !filed.startsWith("CREATE")
&& !filed.startsWith(")") && !filed.endsWith("COMMENT")) {
String filednew = "`" + filed.trim() + "`";
line = line.replace(filed, filednew);
}
// 判断varchar(转换varchar2(
if (line.contains("varchar(")) {
line = line.replace("varchar(", "varchar2(");
}
if(line.contains("int4")) {
line = line.replace("int4", "int(4)");
}
//byte
if(line.contains("bytea")) {
line = line.replace("bytea", "text");
}
// timestamp 改为 datetime
if (line.contains("timestamp")) {
// line = line.replace("timestamp", "datetime");
String[] result = line.split(" ");
if(result[1].contains("timestamp")) {
line = line.replace(result[1], "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("PRIMARY")) {
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
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=" ";
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {
if (!(line.isBlank() || line.isEmpty())) {
// 向文件写入数据
// 过滤
if (line != null) {
writer.write(line);
writer.write("&");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static boolean isNumber(String str) {
return str.matches("\\d+");
}
public static void convert() throws IOException {
// 指定要创建或打开的文件路径及名称
String filePath = "E:\\01XXXX\\doc\\temp.sql";
BufferedReader reader = new BufferedReader(new FileReader(filePath));
File file = new File("E:\\0XXXXXXX\\doc\\doris.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
}
以上很容易将对应的普通的表结构转化为dris 可以识别的表插入数据。
参考资料:
https://cloud.tencent.com/developer/article/2055323
https://blog.csdn.net/be_racle/article/details/133877785
https://blog.csdn.net/be_racle/article/details/133877785
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69949806/viewspace-3004521/ ,如需转载,请注明出处,否则将追究法律责任。