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