main
黄海 3 years ago
parent 7f06fce230
commit 85a6ef7ef4

@ -0,0 +1,32 @@
package UnitTest;
import com.dsideal.FengHuang.Util.ClickHouseUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.druid.DruidPlugin;
public class StructMysqlToClickHouse {
public static void main(String[] args) {
//告之配置文件位置
PropKit.use("application.properties");
//安装地区
String user = PropKit.get("user");
String password = PropKit.get("password");
String jdbcUrl = PropKit.get("jdbcUrl");
DruidPlugin hp = new DruidPlugin(jdbcUrl, user, password);
hp.start();
// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin(hp);
//配置默认小写
arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
arp.start();
//转换的表名称
String dbName = "fenghuang_db";
String tableName = "t_base_organization";
String s = ClickHouseUtil.getSql(dbName, tableName);
System.out.println(s);
}
}

@ -1,44 +1,31 @@
package UnitTest;
package com.dsideal.FengHuang.Util;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;
public class MysqlToClickHouse {
public static void main(String[] args) {
//告之配置文件位置
PropKit.use("application.properties");
//安装地区
String user = PropKit.get("user");
String password = PropKit.get("password");
String jdbcUrl = PropKit.get("jdbcUrl");
DruidPlugin hp = new DruidPlugin(jdbcUrl, user, password);
hp.start();
import java.util.List;
// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin(hp);
//配置默认小写
arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
arp.start();
public class ClickHouseUtil {
public static String getSql(String dbName, String tableName) {
String sql = "show create table " + tableName + " ;";
Record record = Db.findFirst(sql);
String createTable = record.getStr("create table");
//转换的表名称
String tableName = "t_base_term";
String sql = "show create table " + tableName+" ;";
Record record=Db.findFirst(sql);
String createTable =record.getStr("create table");
String res = changeMysqlTableToClickHouse(createTable);
System.out.println("转换后的建表语句为:");
System.out.println(res);
sql = "select column_name from information_schema.columns where table_name = ? and table_schema = ?";
List<Record> list = Db.find(sql, tableName, dbName);
for (Record r1 : list) {
String column_name = r1.getStr("column_name");
createTable = createTable.replaceAll(column_name, column_name.toUpperCase());
}
String res = change(createTable);
return res;
}
/*
MysqlClickHouse
*/
private static String changeMysqlTableToClickHouse(String tableName) {
String tables = tableName;
private static String change(String createSql) {
String tables = createSql;
String primaryKey = "`id`";//默认id
String[] rows = tables.split("\n");
StringBuilder replaceTables = new StringBuilder();
@ -77,7 +64,6 @@ public class MysqlToClickHouse {
// 为空,字符串
changeRow = changeRow.replaceAll("(` ).*(char).*(DEFAULT NULL)", "` String NULL");
changeRow = changeRow.replaceAll("(` ).*(char).*(DEFAULT '')", "` String");
// changeRow = changeRow.replaceAll("(DEFAULT '')", "NULL");
// 非空,字符串
changeRow = changeRow.replaceAll("(` ).*(char).*(NOT NULL)", "` String");
changeRow = changeRow.replaceAll("text", "String");
@ -86,7 +72,6 @@ public class MysqlToClickHouse {
// 以空格分割
String[] changeColumns = changeRow.split("[ ]");
// System.out.println(changeRow);
// 含有int的替换规则
if (changeColumns[3].contains("int") || changeColumns[3].contains("bigint")
|| changeColumns[3].contains("INT")) {
@ -135,7 +120,7 @@ public class MysqlToClickHouse {
String temp = replaceTables.substring(0, replaceTables.indexOf(",) ENGINE = Memory"));
replaceTables = new StringBuilder(temp + ") ENGINE = Memory ");
}
replaceTables.toString().replaceAll("CREATE TABLE `" + tableName + "`", tableName + "_local");
replaceTables.toString().replaceAll("CREATE TABLE `" + createSql + "`", createSql + "_local");
if (haveKey) {
replaceTables.append("PRIMARY KEY " + primaryKey);
}
@ -151,6 +136,12 @@ public class MysqlToClickHouse {
break;
}
}
//修正Timestamp默认值
res = res.replace("DEFAULT current_timestamp() ON UPDATE current_timestamp()", "");
//删除空行
res=res.replaceAll("\n\n","\n");
res=res.replaceAll(" ","");
return res;
}
}
}
Loading…
Cancel
Save