Unverified Commit 98deaffd authored by Wink's avatar Wink Committed by GitHub

[Feature-3189][client] Optimize cdcsource and lineage (#3190)

parent 81689c18
...@@ -228,7 +228,7 @@ public class JobRefreshHandler { ...@@ -228,7 +228,7 @@ public class JobRefreshHandler {
.config(jobConfigInfo) .config(jobConfigInfo)
.build(); .build();
} catch (Exception e) { } catch (Exception e) {
log.error("Connect {} failed,{}", jobManagerHost, e.getMessage()); log.warn("Connect {} failed,{}", jobManagerHost, e.getMessage());
return builder.id(id).error(true).errorMsg(e.getMessage()).build(); return builder.id(id).error(true).errorMsg(e.getMessage()).build();
} }
} }
......
...@@ -38,13 +38,12 @@ import org.apache.flink.types.Row; ...@@ -38,13 +38,12 @@ import org.apache.flink.types.Row;
import java.io.Serializable; import java.io.Serializable;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import cn.hutool.core.collection.CollUtil;
public class SQLSinkBuilder extends AbstractSqlSinkBuilder implements Serializable { public class SQLSinkBuilder extends AbstractSqlSinkBuilder implements Serializable {
public static final String KEY_WORD = "sql"; public static final String KEY_WORD = "sql";
...@@ -58,7 +57,7 @@ public class SQLSinkBuilder extends AbstractSqlSinkBuilder implements Serializab ...@@ -58,7 +57,7 @@ public class SQLSinkBuilder extends AbstractSqlSinkBuilder implements Serializab
@Override @Override
protected void initTypeConverterList() { protected void initTypeConverterList() {
typeConverterList = CollUtil.newArrayList( typeConverterList = Arrays.asList(
this::convertDateType, this::convertDateType,
this::convertTimestampType, this::convertTimestampType,
this::convertFloatType, this::convertFloatType,
......
...@@ -34,12 +34,11 @@ import org.apache.flink.types.Row; ...@@ -34,12 +34,11 @@ import org.apache.flink.types.Row;
import java.io.Serializable; import java.io.Serializable;
import java.time.Instant; import java.time.Instant;
import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import com.google.common.collect.Lists;
public class SQLCatalogSinkBuilder extends AbstractSqlSinkBuilder implements Serializable { public class SQLCatalogSinkBuilder extends AbstractSqlSinkBuilder implements Serializable {
public static final String KEY_WORD = "sql-catalog"; public static final String KEY_WORD = "sql-catalog";
...@@ -52,7 +51,7 @@ public class SQLCatalogSinkBuilder extends AbstractSqlSinkBuilder implements Ser ...@@ -52,7 +51,7 @@ public class SQLCatalogSinkBuilder extends AbstractSqlSinkBuilder implements Ser
@Override @Override
protected void initTypeConverterList() { protected void initTypeConverterList() {
typeConverterList = Lists.newArrayList( typeConverterList = Arrays.asList(
this::convertDateType, this::convertDateType,
this::convertTimestampType, this::convertTimestampType,
this::convertDecimalType, this::convertDecimalType,
......
...@@ -130,6 +130,9 @@ public class FlinkStatementUtil { ...@@ -130,6 +130,9 @@ public class FlinkStatementUtil {
if (config.getSink().get("connector").equals("hudi") && (type.equals("TIMESTAMP"))) { if (config.getSink().get("connector").equals("hudi") && (type.equals("TIMESTAMP"))) {
return "TIMESTAMP(3)"; return "TIMESTAMP(3)";
} }
if (config.getSink().get("connector").equals("doris") && (type.equals("TIME"))) {
return "STRING";
}
return type; return type;
} }
......
...@@ -31,7 +31,6 @@ import org.apache.flink.table.api.TableEnvironment; ...@@ -31,7 +31,6 @@ import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.TableException; import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.bridge.java.internal.StreamTableEnvironmentImpl; import org.apache.flink.table.api.bridge.java.internal.StreamTableEnvironmentImpl;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import org.apache.flink.table.delegation.Planner; import org.apache.flink.table.delegation.Planner;
import org.apache.flink.table.operations.ExplainOperation; import org.apache.flink.table.operations.ExplainOperation;
import org.apache.flink.table.operations.ModifyOperation; import org.apache.flink.table.operations.ModifyOperation;
...@@ -118,7 +117,7 @@ public abstract class AbstractCustomTableEnvironment ...@@ -118,7 +117,7 @@ public abstract class AbstractCustomTableEnvironment
@Override @Override
public List<LineageRel> getLineage(String statement) { public List<LineageRel> getLineage(String statement) {
LineageContext lineageContext = new LineageContext((TableEnvironmentImpl) streamTableEnvironment); LineageContext lineageContext = new LineageContext(this);
return lineageContext.getLineage(statement); return lineageContext.analyzeLineage(statement);
} }
} }
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.utils;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.util.SqlBasicVisitor;
import org.apache.flink.table.catalog.UnresolvedIdentifier;
import java.util.ArrayList;
import java.util.List;
public class FunctionVisitor extends SqlBasicVisitor<Void> {
private final List<UnresolvedIdentifier> functionList = new ArrayList<>();
@Override
public Void visit(SqlCall call) {
if (call instanceof SqlBasicCall && call.getOperator() instanceof SqlFunction) {
SqlFunction function = (SqlFunction) call.getOperator();
SqlIdentifier opName = function.getNameAsId();
functionList.add(UnresolvedIdentifier.of(opName.names));
}
return super.visit(call);
}
public List<UnresolvedIdentifier> getFunctionList() {
return functionList;
}
}
...@@ -19,26 +19,38 @@ ...@@ -19,26 +19,38 @@
package org.dinky.utils; package org.dinky.utils;
import org.dinky.data.model.FunctionResult;
import org.dinky.data.model.LineageRel; import org.dinky.data.model.LineageRel;
import org.dinky.executor.CustomParser;
import org.dinky.executor.CustomTableEnvironment;
import org.apache.calcite.plan.RelOptTable; import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.rel.RelNode; import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.metadata.RelColumnOrigin; import org.apache.calcite.rel.metadata.RelColumnOrigin;
import org.apache.calcite.rel.metadata.RelMetadataQuery; import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.calcite.sql.SqlNode;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.flink.api.java.tuple.Tuple2; import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.table.api.TableException; import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.ValidationException; import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.api.internal.TableEnvironmentImpl; import org.apache.flink.table.catalog.ContextResolvedFunction;
import org.apache.flink.table.catalog.FunctionCatalog;
import org.apache.flink.table.catalog.UnresolvedIdentifier;
import org.apache.flink.table.functions.FunctionIdentifier;
import org.apache.flink.table.operations.Operation; import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.operations.SinkModifyOperation; import org.apache.flink.table.operations.SinkModifyOperation;
import org.apache.flink.table.planner.delegation.PlannerBase;
import org.apache.flink.table.planner.operations.PlannerQueryOperation; import org.apache.flink.table.planner.operations.PlannerQueryOperation;
import org.apache.flink.table.planner.plan.schema.TableSourceTable; import org.apache.flink.table.planner.plan.schema.TableSourceTable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* LineageContext * LineageContext
* *
...@@ -46,13 +58,15 @@ import java.util.Set; ...@@ -46,13 +58,15 @@ import java.util.Set;
*/ */
public class LineageContext { public class LineageContext {
private final TableEnvironmentImpl tableEnv; private static final Logger LOG = LoggerFactory.getLogger(LineageContext.class);
private final CustomTableEnvironment tableEnv;
public LineageContext(TableEnvironmentImpl tableEnv) { public LineageContext(CustomTableEnvironment tableEnv) {
this.tableEnv = tableEnv; this.tableEnv = tableEnv;
} }
public List<LineageRel> getLineage(String statement) { public List<LineageRel> analyzeLineage(String statement) {
// 1. Generate original relNode tree // 1. Generate original relNode tree
Tuple2<String, RelNode> parsed = parseStatement(statement); Tuple2<String, RelNode> parsed = parseStatement(statement);
String sinkTable = parsed.getField(0); String sinkTable = parsed.getField(0);
...@@ -131,4 +145,50 @@ public class LineageContext { ...@@ -131,4 +145,50 @@ public class LineageContext {
} }
return resultList; return resultList;
} }
/**
* Analyze custom functions from SQL, does not contain system functions.
*
* @param singleSql the SQL statement to analyze
* @return custom functions set
*/
public Set<FunctionResult> analyzeFunction(String singleSql) {
LOG.info("Analyze function Sql: \n {}", singleSql);
CustomParser parser = (CustomParser) tableEnv.getParser();
// parsing sql and return the abstract syntax tree
SqlNode sqlNode = parser.parseSql(singleSql);
// validate the query
SqlNode validated = parser.validate(sqlNode);
// look for all functions
FunctionVisitor visitor = new FunctionVisitor();
validated.accept(visitor);
List<UnresolvedIdentifier> fullFunctionList = visitor.getFunctionList();
// filter custom functions
Set<FunctionResult> resultSet = new HashSet<>();
for (UnresolvedIdentifier unresolvedIdentifier : fullFunctionList) {
getFunctionCatalog()
.lookupFunction(unresolvedIdentifier)
.flatMap(ContextResolvedFunction::getIdentifier)
// the objectIdentifier of the built-in function is null
.flatMap(FunctionIdentifier::getIdentifier)
.ifPresent(identifier -> {
FunctionResult functionResult = new FunctionResult()
.setCatalogName(identifier.getCatalogName())
.setDatabase(identifier.getDatabaseName())
.setFunctionName(identifier.getObjectName());
LOG.debug("analyzed function: {}", functionResult);
resultSet.add(functionResult);
});
}
return resultSet;
}
private FunctionCatalog getFunctionCatalog() {
PlannerBase planner = (PlannerBase) tableEnv.getPlanner();
return planner.getFlinkContext().getFunctionCatalog();
}
} }
...@@ -22,12 +22,11 @@ package org.dinky.utils; ...@@ -22,12 +22,11 @@ package org.dinky.utils;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.dinky.data.model.LineageRel; import org.dinky.data.model.LineageRel;
import org.dinky.executor.CustomTableEnvironmentImpl;
import org.apache.flink.configuration.Configuration; import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.EnvironmentSettings; import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -43,7 +42,7 @@ import org.junit.Test; ...@@ -43,7 +42,7 @@ import org.junit.Test;
*/ */
public class LineageContextTest { public class LineageContextTest {
private static TableEnvironmentImpl tableEnv; private static CustomTableEnvironmentImpl tableEnv;
private static LineageContext context; private static LineageContext context;
@BeforeClass @BeforeClass
...@@ -52,8 +51,7 @@ public class LineageContextTest { ...@@ -52,8 +51,7 @@ public class LineageContextTest {
EnvironmentSettings settings = EnvironmentSettings settings =
EnvironmentSettings.newInstance().inStreamingMode().build(); EnvironmentSettings.newInstance().inStreamingMode().build();
tableEnv = (TableEnvironmentImpl) StreamTableEnvironment.create(env, settings); tableEnv = CustomTableEnvironmentImpl.create(env, settings);
context = new LineageContext(tableEnv); context = new LineageContext(tableEnv);
} }
...@@ -80,7 +78,7 @@ public class LineageContextTest { ...@@ -80,7 +78,7 @@ public class LineageContextTest {
@Test @Test
public void testGetLineage() { public void testGetLineage() {
List<LineageRel> actualList = context.getLineage("INSERT INTO TT select a||c A ,b||c B from ST"); List<LineageRel> actualList = context.analyzeLineage("INSERT INTO TT select a||c A ,b||c B from ST");
String[][] expectedArray = { String[][] expectedArray = {
{"ST", "a", "TT", "A", "||(a, c)"}, {"ST", "a", "TT", "A", "||(a, c)"},
{"ST", "c", "TT", "A", "||(a, c)"}, {"ST", "c", "TT", "A", "||(a, c)"},
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package org.dinky.data.model; package org.dinky.data.model;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -135,6 +136,10 @@ public class FlinkCDCConfig { ...@@ -135,6 +136,10 @@ public class FlinkCDCConfig {
} }
private boolean isSkip(String key) { private boolean isSkip(String key) {
if (key.equals("url")) {
return !(sink.containsKey("connector")
&& Arrays.asList("jdbc", "clickhouse").contains(sink.get("connector")));
}
switch (key) { switch (key) {
case SINK_DB: case SINK_DB:
case AUTO_CREATE: case AUTO_CREATE:
......
...@@ -169,7 +169,7 @@ public class DorisDriver extends AbstractJdbcDriver { ...@@ -169,7 +169,7 @@ public class DorisDriver extends AbstractJdbcDriver {
String propertiesStr = "\n PROPERTIES ( \"light_schema_change\" = \"true\" )"; String propertiesStr = "\n PROPERTIES ( \"light_schema_change\" = \"true\" )";
String commentStr = String commentStr =
Asserts.isNullString(table.getComment()) ? "" : String.format("\n COMMENT '%s'", table.getComment()); Asserts.isNullString(table.getComment()) ? "" : String.format("\n COMMENT \"%s\"", table.getComment());
return MessageFormat.format( return MessageFormat.format(
"CREATE TABLE IF NOT EXISTS `{0}`.`{1}` (\n{2}\n) ENGINE=OLAP{3}{4}{5}{6}", "CREATE TABLE IF NOT EXISTS `{0}`.`{1}` (\n{2}\n) ENGINE=OLAP{3}{4}{5}{6}",
...@@ -214,11 +214,10 @@ public class DorisDriver extends AbstractJdbcDriver { ...@@ -214,11 +214,10 @@ public class DorisDriver extends AbstractJdbcDriver {
: String.format("%s NULL ", !column.isNullable() ? " NOT " : ""); : String.format("%s NULL ", !column.isNullable() ? " NOT " : "");
return String.format( return String.format(
" `%s` %s%s%s%s", " `%s` %s%s%s",
column.getName(), column.getName(),
columnType, columnType,
defaultValue, defaultValue,
column.isAutoIncrement() ? " AUTO_INCREMENT " : "",
Asserts.isNotNullString(column.getComment()) Asserts.isNotNullString(column.getComment())
? String.format(" COMMENT '%s'", column.getComment()) ? String.format(" COMMENT '%s'", column.getComment())
: ""); : "");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment