diff --git a/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/AbstractJdbcDriver.java b/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/AbstractJdbcDriver.java index 54073d4966..a67a43ec6d 100644 --- a/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/AbstractJdbcDriver.java +++ b/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/AbstractJdbcDriver.java @@ -385,13 +385,6 @@ public List listColumns(String schemaName, String tableName) { return columns; } - @Override - public List listColumnsSortByPK(String schemaName, String tableName) { - List columnList = listColumns(schemaName, tableName); - columnList.sort(Comparator.comparing(Column::isKeyFlag).reversed()); - return columnList; - } - @Override public boolean createTable(Table table) throws Exception { String sql = getCreateTableSql(table).replaceAll("\r\n", " "); diff --git a/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/Driver.java b/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/Driver.java index b297a7f143..d43b67ac02 100644 --- a/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/Driver.java +++ b/dlink-metadata/dlink-metadata-base/src/main/java/com/dlink/metadata/driver/Driver.java @@ -153,8 +153,6 @@ static Driver buildNewConnection(String connector, String url, String username, List listColumns(String schemaName, String tableName); - List listColumnsSortByPK(String schemaName, String tableName); - List getSchemasAndTables(); List getTablesAndColumns(String schemaName); diff --git a/dlink-metadata/dlink-metadata-doris/src/main/java/com/dlink/metadata/driver/DorisDriver.java b/dlink-metadata/dlink-metadata-doris/src/main/java/com/dlink/metadata/driver/DorisDriver.java index 4d7f872ce5..bb0299862e 100644 --- a/dlink-metadata/dlink-metadata-doris/src/main/java/com/dlink/metadata/driver/DorisDriver.java +++ b/dlink-metadata/dlink-metadata-doris/src/main/java/com/dlink/metadata/driver/DorisDriver.java @@ -19,13 +19,6 @@ package com.dlink.metadata.driver; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import cn.hutool.core.text.CharSequenceUtil; import com.dlink.metadata.convert.DorisTypeConvert; import com.dlink.metadata.convert.ITypeConvert; import com.dlink.metadata.query.DorisQuery; @@ -38,6 +31,15 @@ import com.dlink.utils.LogUtil; import com.dlink.utils.SqlUtil; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import cn.hutool.core.text.CharSequenceUtil; + public class DorisDriver extends AbstractJdbcDriver { @Override @@ -133,11 +135,13 @@ public String generateCreateTableSql(Table table) { @Override public String getCreateTableSql(Table table) { - List dorisTypes = Arrays.asList("BOOLEAN", "TINYINT", "SMALLINT", "SMALLINT", "INT", "BIGINT", "LARGEINT", "FLOAT", "DOUBLE", "DECIMAL", "DATE", "DATETIME", "CHAR", "VARCHAR", "TEXT", "TIMESTAMP", "STRING"); + List dorisTypes = Arrays.asList("BOOLEAN", "TINYINT", "SMALLINT", "SMALLINT", "INT", "BIGINT", + "LARGEINT", "FLOAT", "DOUBLE", "DECIMAL", "DATE", "DATETIME", "CHAR", "VARCHAR", "TEXT", "TIMESTAMP", + "STRING"); StringBuilder keyBuffer = new StringBuilder(); StringBuilder ddlBuffer = new StringBuilder(); ddlBuffer.append("CREATE TABLE IF NOT EXISTS ").append(table.getSchema()).append(".").append(table.getName()) - .append(" (").append(System.lineSeparator()); + .append(" (").append(System.lineSeparator()); for (int i = 0; i < table.getColumns().size(); i++) { Column columnInfo = table.getColumns().get(i); String cType = columnInfo.getType().split(" ")[0].toUpperCase(); @@ -146,27 +150,29 @@ public String getCreateTableSql(Table table) { cType = columnInfo.getJavaType().getFlinkType().toUpperCase(); if (!dorisTypes.contains(cType)) { logger.error("doris does not support {} type", columnInfo.getType()); - return ""; + return ""; } } if (cType.equalsIgnoreCase("TIMESTAMP")) { ddlBuffer.append("DATETIME"); } else if (columnInfo.getType().equalsIgnoreCase("TEXT")) { ddlBuffer.append("STRING"); - }else{ + } else { ddlBuffer.append(cType); } - if(columnInfo.getLength()!=null &&columnInfo.getLength()>0 ) { - ddlBuffer.append("(").append(cType.equalsIgnoreCase("VARCHAR") ?columnInfo.getLength() * 3 : columnInfo.getLength()).append(")"); + if (columnInfo.getLength() != null && columnInfo.getLength() > 0) { + ddlBuffer.append("(") + .append(cType.equalsIgnoreCase("VARCHAR") ? columnInfo.getLength() * 3 : columnInfo.getLength()) + .append(")"); } - if (columnInfo.getComment()!=null) { + if (columnInfo.getComment() != null) { ddlBuffer.append(" COMMENT '").append(columnInfo.getComment()).append("'"); } if (i < table.getColumns().size() - 1) { ddlBuffer.append(","); } ddlBuffer.append(System.lineSeparator()); - if(columnInfo.isKeyFlag()){ + if (columnInfo.isKeyFlag()) { keyBuffer.append(columnInfo.getName()).append(","); } @@ -175,7 +181,8 @@ public String getCreateTableSql(Table table) { String primaryKeys = keyBuffer.substring(0, keyBuffer.length() - 1); ddlBuffer.append(") UNIQUE KEY (").append(primaryKeys).append(")").append(System.lineSeparator()); ddlBuffer.append("COMMENT '").append(table.getComment()).append("'"); - ddlBuffer.append(" DISTRIBUTED BY HASH (").append(primaryKeys).append(") BUCKETS AUTO").append(System.lineSeparator()); + ddlBuffer.append(" DISTRIBUTED BY HASH (").append(primaryKeys).append(") BUCKETS AUTO") + .append(System.lineSeparator()); // ddlBuffer.append(" PROPERTIES ( \"replication_allocation\" = \"tag.location.default: 3\")"); return ddlBuffer.toString(); @@ -184,6 +191,8 @@ public String getCreateTableSql(Table table) { @Override public List listColumns(String schemaName, String tableName) { // Doris 中声明为 Key 的列(可能是多个)必须顺序声明在建表语句头部,因此按 Key 对列重新排序 - return listColumnsSortByPK(schemaName, tableName); + List columnList = super.listColumns(schemaName, tableName); + columnList.sort(Comparator.comparing(Column::isKeyFlag).reversed()); + return columnList; } } diff --git a/dlink-metadata/dlink-metadata-starrocks/src/main/java/com/dlink/metadata/driver/StarRocksDriver.java b/dlink-metadata/dlink-metadata-starrocks/src/main/java/com/dlink/metadata/driver/StarRocksDriver.java index a7ce82860e..d079582263 100644 --- a/dlink-metadata/dlink-metadata-starrocks/src/main/java/com/dlink/metadata/driver/StarRocksDriver.java +++ b/dlink-metadata/dlink-metadata-starrocks/src/main/java/com/dlink/metadata/driver/StarRocksDriver.java @@ -29,6 +29,7 @@ import com.dlink.utils.SqlUtil; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -112,6 +113,8 @@ public Map getFlinkColumnTypeConversion() { @Override public List listColumns(String schemaName, String tableName) { // StarRocks 中声明为 Key 的列(可能是多个)必须顺序声明在建表语句头部,因此按 Key 对列重新排序 - return listColumnsSortByPK(schemaName, tableName); + List columnList = super.listColumns(schemaName, tableName); + columnList.sort(Comparator.comparing(Column::isKeyFlag).reversed()); + return columnList; } }