Unverified Commit 7950c5f9 authored by Licho's avatar Licho Committed by GitHub

refactor: asome trivial modified. (#2081)

* refactor: package uri wrong, and trivial optimization * refactor: a little trivial change * Spotless Apply * refactor Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * refactor: map null to empty Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply * fix: Mistakenly deleted Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * fix Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent e5070787
......@@ -116,8 +116,8 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
List<ModifyOperation> modifyOperations =
operations.stream()
.filter(operation -> operation instanceof ModifyOperation)
.map(operation -> (ModifyOperation) operation)
.filter(ModifyOperation.class::isInstance)
.map(ModifyOperation.class::cast)
.collect(Collectors.toList());
StreamGraph streamGraph = transOperatoinsToStreamGraph(modifyOperations);
......@@ -180,26 +180,26 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
}
Operation operation = operations.get(0);
SqlExplainResult record = new SqlExplainResult();
record.setParseTrue(true);
record.setExplainTrue(true);
SqlExplainResult data = new SqlExplainResult();
data.setParseTrue(true);
data.setExplainTrue(true);
if (operation instanceof ModifyOperation) {
record.setType("Modify DML");
data.setType("Modify DML");
} else if (operation instanceof ExplainOperation) {
record.setType("Explain DML");
data.setType("Explain DML");
} else if (operation instanceof QueryOperation) {
record.setType("Query DML");
data.setType("Query DML");
} else {
record.setExplain(operation.asSummaryString());
record.setType("DDL");
data.setExplain(operation.asSummaryString());
data.setType("DDL");
// record.setExplain("DDL statement needn't comment。");
return record;
// data.setExplain("DDL statement needn't comment。");
return data;
}
record.setExplain(getPlanner().explain(operations, ExplainFormat.TEXT, extraDetails));
return record;
data.setExplain(getPlanner().explain(operations, ExplainFormat.TEXT, extraDetails));
return data;
}
public boolean parseAndLoadConfiguration(
......
......@@ -34,6 +34,7 @@ import org.apache.flink.table.api.internal.TableEnvironmentInternal;
import org.apache.flink.table.delegation.Planner;
import org.apache.flink.types.Row;
import java.util.Collections;
import java.util.List;
import java.util.Map;
......@@ -67,7 +68,7 @@ public interface CustomTableEnvironment
Configuration getRootConfiguration();
default List<LineageRel> getLineage(String statement) {
return null;
return Collections.emptyList();
}
<T> void createTemporaryView(String s, DataStream<Row> dataStream, List<String> columnNameList);
......
......@@ -34,6 +34,8 @@ public class SqlUtil {
private static final String SEMICOLON = ";";
private SqlUtil() {}
public static String[] getStatements(String sql) {
return getStatements(sql, SystemConfiguration.getInstances().getSqlSeparator());
}
......
......@@ -53,6 +53,10 @@ import org.apache.flink.runtime.rest.messages.JobPlanInfo;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
......@@ -66,6 +70,7 @@ import cn.hutool.core.util.StrUtil;
* @since 2021/6/22
*/
public class Explainer {
private static final Logger logger = LoggerFactory.getLogger(Explainer.class);
private Executor executor;
private boolean useStatementSet;
......@@ -213,7 +218,7 @@ public class Explainer {
record.setIndex(index++);
sqlExplainRecords.add(record);
}
if (correct && jobParam.getTrans().size() > 0) {
if (correct && !jobParam.getTrans().isEmpty()) {
if (useStatementSet) {
SqlExplainResult record = new SqlExplainResult();
List<String> inserts = new ArrayList<>();
......@@ -222,7 +227,7 @@ public class Explainer {
inserts.add(item.getValue());
}
}
if (inserts.size() > 0) {
if (!inserts.isEmpty()) {
String sqlSet = String.join(";\r\n ", inserts);
try {
record.setExplain(executor.explainStatementSet(inserts));
......@@ -304,43 +309,39 @@ public class Explainer {
public ObjectNode getStreamGraph(String statement) {
JobParam jobParam = pretreatStatements(SqlUtil.getStatements(statement, sqlSeparator));
if (jobParam.getDdl().size() > 0) {
for (StatementParam statementParam : jobParam.getDdl()) {
executor.executeSql(statementParam.getValue());
}
}
if (jobParam.getTrans().size() > 0) {
jobParam.getDdl().forEach(statementParam -> executor.executeSql(statementParam.getValue()));
if (!jobParam.getTrans().isEmpty()) {
return executor.getStreamGraph(jobParam.getTransStatement());
} else if (jobParam.getExecute().size() > 0) {
List<String> datastreamPlans = new ArrayList<>();
for (StatementParam item : jobParam.getExecute()) {
datastreamPlans.add(item.getValue());
}
}
if (!jobParam.getExecute().isEmpty()) {
List<String> datastreamPlans =
jobParam.getExecute().stream()
.map(StatementParam::getValue)
.collect(Collectors.toList());
return executor.getStreamGraphFromDataStream(datastreamPlans);
} else {
return mapper.createObjectNode();
}
return mapper.createObjectNode();
}
public JobPlanInfo getJobPlanInfo(String statement) {
JobParam jobParam = pretreatStatements(SqlUtil.getStatements(statement, sqlSeparator));
if (jobParam.getDdl().size() > 0) {
for (StatementParam statementParam : jobParam.getDdl()) {
executor.executeSql(statementParam.getValue());
}
}
if (jobParam.getTrans().size() > 0) {
jobParam.getDdl().forEach(statementParam -> executor.executeSql(statementParam.getValue()));
if (!jobParam.getTrans().isEmpty()) {
return executor.getJobPlanInfo(jobParam.getTransStatement());
} else if (jobParam.getExecute().size() > 0) {
List<String> datastreamPlans = new ArrayList<>();
for (StatementParam item : jobParam.getExecute()) {
datastreamPlans.add(item.getValue());
}
}
if (!jobParam.getExecute().isEmpty()) {
List<String> datastreamPlans =
jobParam.getExecute().stream()
.map(StatementParam::getValue)
.collect(Collectors.toList());
return executor.getJobPlanInfoFromDataStream(datastreamPlans);
} else {
throw new RuntimeException(
"Creating job plan fails because this job doesn't contain an insert statement.");
}
throw new RuntimeException(
"Creating job plan fails because this job doesn't contain an insert statement.");
}
public List<LineageRel> getLineage(String statement) {
......@@ -353,15 +354,12 @@ public class Explainer {
useStatementSet,
1,
executor.getTableConfig().getConfiguration().toMap());
JobManager jm = JobManager.buildPlanMode(jobConfig);
this.initialize(jm, jobConfig, statement);
this.initialize(JobManager.buildPlanMode(jobConfig), jobConfig, statement);
String[] sqls = SqlUtil.getStatements(statement, sqlSeparator);
List<LineageRel> lineageRelList = new ArrayList<>();
for (String item : sqls) {
String sql = "";
for (String item : SqlUtil.getStatements(statement, sqlSeparator)) {
try {
sql = FlinkInterceptor.pretreatStatement(executor, item);
String sql = FlinkInterceptor.pretreatStatement(executor, item);
if (Asserts.isNullString(sql)) {
continue;
}
......@@ -373,8 +371,8 @@ public class Explainer {
executor.executeSql(sql);
}
} catch (Exception e) {
e.printStackTrace();
break;
logger.error(e.getMessage());
return lineageRelList;
}
}
return lineageRelList;
......
......@@ -21,8 +21,8 @@ package org.dinky.job;
import org.dinky.function.data.model.UDF;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* JobParam
......@@ -87,11 +87,7 @@ public class JobParam {
}
public List<String> getTransStatement() {
List<String> statementList = new ArrayList<>();
for (StatementParam statementParam : trans) {
statementList.add(statementParam.getValue());
}
return statementList;
return trans.stream().map(StatementParam::getValue).collect(Collectors.toList());
}
public void setTrans(List<StatementParam> trans) {
......
......@@ -238,9 +238,11 @@ public abstract class Executor {
reset.invoke(UserGroupInformation.class);
log.info("Reset kerberos authentication...");
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IOException e) {
} catch (NoSuchMethodException
| IllegalAccessException
| InvocationTargetException
| IOException e) {
logger.error("Reset kerberos authentication error.", e);
throw new RuntimeException(e);
}
}
......@@ -284,8 +286,7 @@ public abstract class Executor {
"Kerberos [{}] authentication success.",
UserGroupInformation.getLoginUser().getUserName());
} catch (IOException e) {
log.error("Kerberos authentication failed.");
e.printStackTrace();
log.error("Kerberos authentication failed. ", e);
}
}
......@@ -302,14 +303,7 @@ public abstract class Executor {
if (udfPyFilePath == null || udfPyFilePath.length == 0) {
return;
}
// if (MapUtil.isNotEmpty(setConfig)) {
// setConfig.put(PythonOptions.PYTHON_FILES.key(), String.join(",",
// udfPyFilePath));
// setConfig.put(PythonOptions.PYTHON_CLIENT_EXECUTABLE.key(), executable);
// Configuration configuration =
// Configuration.fromMap(executorSetting.getConfig());
// environment.getConfig().configure(configuration, null);
// }
Configuration configuration = tableEnvironment.getConfig().getConfiguration();
configuration.setString(PythonOptions.PYTHON_FILES, String.join(",", udfPyFilePath));
configuration.setString(PythonOptions.PYTHON_CLIENT_EXECUTABLE, executable);
......@@ -346,7 +340,7 @@ public abstract class Executor {
try {
objectNode = (ObjectNode) mapper.readTree(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
logger.error("Get stream graph json node error.", e);
}
return objectNode;
......@@ -357,11 +351,8 @@ public abstract class Executor {
}
public ObjectNode getStreamGraphFromDataStream(List<String> statements) {
for (String statement : statements) {
executeSql(statement);
}
StreamGraph streamGraph = getStreamGraph();
return getStreamGraphJsonNode(streamGraph);
statements.forEach(this::executeSql);
return getStreamGraphJsonNode(getStreamGraph());
}
public JobPlanInfo getJobPlanInfo(List<String> statements) {
......@@ -369,9 +360,7 @@ public abstract class Executor {
}
public JobPlanInfo getJobPlanInfoFromDataStream(List<String> statements) {
for (String statement : statements) {
executeSql(statement);
}
statements.forEach(this::executeSql);
StreamGraph streamGraph = getStreamGraph();
return new JobPlanInfo(JsonPlanGenerator.generatePlan(streamGraph.getJobGraph()));
}
......@@ -382,17 +371,13 @@ public abstract class Executor {
public TableResult executeStatementSet(List<String> statements) {
StatementSet statementSet = tableEnvironment.createStatementSet();
for (String item : statements) {
statementSet.addInsertSql(item);
}
statements.forEach(statementSet::addInsertSql);
return statementSet.execute();
}
public String explainStatementSet(List<String> statements) {
StatementSet statementSet = tableEnvironment.createStatementSet();
for (String item : statements) {
statementSet.addInsertSql(item);
}
statements.forEach(statementSet::addInsertSql);
return statementSet.explain();
}
......
......@@ -41,7 +41,6 @@ import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
......@@ -185,10 +184,6 @@ public final class SqlManager {
rows);
}
public Iterator getSqlFragmentsIterator() {
return sqlFragments.entrySet().iterator();
}
public Table getSqlFragmentsTable(CustomTableEnvironmentImpl environment) {
List<String> keys = new ArrayList<>(sqlFragments.keySet());
return environment.fromValues(keys);
......
......@@ -19,6 +19,7 @@
package org.dinky.parser;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
......@@ -33,6 +34,7 @@ import org.slf4j.LoggerFactory;
* @since 2021/6/14 16:49
*/
public class SingleSqlParserFactory {
private SingleSqlParserFactory() {}
protected static final Logger logger = LoggerFactory.getLogger(SingleSqlParserFactory.class);
......@@ -61,7 +63,7 @@ public class SingleSqlParserFactory {
if (tmp == null) {
logger.error("sql: {} illegal.", sql);
return null;
return Collections.emptyMap();
}
return tmp.splitSql2Segment();
......
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