Unverified Commit 291da9dc authored by Licho's avatar Licho Committed by GitHub

refactor: format CustomTableEnvironmentImpl (#1549)

* refactor: format CustomTableEnvironmentImpl * chore: splotless * fix: compitable for jdk8 Signed-off-by: 's avatarLicho <chinabhsun@gmail.com> * refactor: compatible for jdk8 Signed-off-by: 's avatarLicho <chinabhsun@gmail.com> * refactor: remove used local variable Signed-off-by: 's avatarLicho <chinabhsun@gmail.com>
parent 6803fad5
...@@ -27,10 +27,8 @@ import org.dinky.result.SqlExplainResult; ...@@ -27,10 +27,8 @@ import org.dinky.result.SqlExplainResult;
import org.dinky.utils.FlinkStreamProgramWithoutPhysical; import org.dinky.utils.FlinkStreamProgramWithoutPhysical;
import org.dinky.utils.LineageContext; import org.dinky.utils.LineageContext;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.dag.Transformation; import org.apache.flink.api.dag.Transformation;
import org.apache.flink.configuration.Configuration; import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.ExecutionOptions;
import org.apache.flink.configuration.PipelineOptions; import org.apache.flink.configuration.PipelineOptions;
import org.apache.flink.runtime.jobgraph.JobGraph; import org.apache.flink.runtime.jobgraph.JobGraph;
import org.apache.flink.runtime.jobgraph.jsonplan.JsonPlanGenerator; import org.apache.flink.runtime.jobgraph.jsonplan.JsonPlanGenerator;
...@@ -64,9 +62,14 @@ import org.apache.flink.util.MutableURLClassLoader; ...@@ -64,9 +62,14 @@ import org.apache.flink.util.MutableURLClassLoader;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -80,7 +83,10 @@ import com.fasterxml.jackson.databind.node.ObjectNode; ...@@ -80,7 +83,10 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
*/ */
public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
private static final Logger log = LoggerFactory.getLogger(CustomTableEnvironmentImpl.class);
private final FlinkChainedProgram flinkChainedProgram; private final FlinkChainedProgram flinkChainedProgram;
private static final ObjectMapper mapper = new ObjectMapper();
public CustomTableEnvironmentImpl( public CustomTableEnvironmentImpl(
CatalogManager catalogManager, CatalogManager catalogManager,
...@@ -115,10 +121,6 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -115,10 +121,6 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
public static CustomTableEnvironmentImpl createBatch( public static CustomTableEnvironmentImpl createBatch(
StreamExecutionEnvironment executionEnvironment) { StreamExecutionEnvironment executionEnvironment) {
Configuration configuration = new Configuration();
configuration.set(ExecutionOptions.RUNTIME_MODE, RuntimeExecutionMode.BATCH);
TableConfig tableConfig = new TableConfig();
tableConfig.addConfiguration(configuration);
return create( return create(
executionEnvironment, EnvironmentSettings.newInstance().inBatchMode().build()); executionEnvironment, EnvironmentSettings.newInstance().inBatchMode().build());
} }
...@@ -134,10 +136,6 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -134,10 +136,6 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
tableConfig.setRootConfiguration(executor.getConfiguration()); tableConfig.setRootConfiguration(executor.getConfiguration());
tableConfig.addConfiguration(settings.getConfiguration()); tableConfig.addConfiguration(settings.getConfiguration());
final ResourceManager resourceManager =
new ResourceManager(settings.getConfiguration(), userClassLoader);
final ModuleManager moduleManager = new ModuleManager();
final CatalogManager catalogManager = final CatalogManager catalogManager =
CatalogManager.newBuilder() CatalogManager.newBuilder()
.classLoader(userClassLoader) .classLoader(userClassLoader)
...@@ -150,6 +148,9 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -150,6 +148,9 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
.executionConfig(executionEnvironment.getConfig()) .executionConfig(executionEnvironment.getConfig())
.build(); .build();
final ModuleManager moduleManager = new ModuleManager();
final ResourceManager resourceManager =
new ResourceManager(settings.getConfiguration(), userClassLoader);
final FunctionCatalog functionCatalog = final FunctionCatalog functionCatalog =
new FunctionCatalog(tableConfig, resourceManager, catalogManager, moduleManager); new FunctionCatalog(tableConfig, resourceManager, catalogManager, moduleManager);
...@@ -179,34 +180,35 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -179,34 +180,35 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
if (operations.size() != 1) { if (operations.size() != 1) {
throw new TableException( throw new TableException(
"Unsupported SQL query! explainSql() only accepts a single SQL query."); "Unsupported SQL query! explainSql() only accepts a single SQL query.");
} else {
List<ModifyOperation> modifyOperations = new ArrayList<>();
for (int i = 0; i < operations.size(); i++) {
if (operations.get(i) instanceof ModifyOperation) {
modifyOperations.add((ModifyOperation) operations.get(i));
}
}
List<Transformation<?>> trans = getPlanner().translate(modifyOperations);
for (Transformation<?> transformation : trans) {
getStreamExecutionEnvironment().addOperator(transformation);
}
StreamGraph streamGraph = getStreamExecutionEnvironment().getStreamGraph();
if (getConfig().getConfiguration().containsKey(PipelineOptions.NAME.key())) {
streamGraph.setJobName(
getConfig().getConfiguration().getString(PipelineOptions.NAME));
} }
List<ModifyOperation> modifyOperations =
operations.stream()
.filter(operation -> operation instanceof ModifyOperation)
.map(operation -> (ModifyOperation) operation)
.collect(Collectors.toList());
StreamGraph streamGraph = transOperatoinsToStreamGraph(modifyOperations);
JSONGenerator jsonGenerator = new JSONGenerator(streamGraph); JSONGenerator jsonGenerator = new JSONGenerator(streamGraph);
String json = jsonGenerator.getJSON();
ObjectMapper mapper = new ObjectMapper();
ObjectNode objectNode = mapper.createObjectNode();
try { try {
objectNode = (ObjectNode) mapper.readTree(json); return (ObjectNode) mapper.readTree(jsonGenerator.getJSON());
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); log.error("read streamGraph configure error: ", e);
} finally { return mapper.createObjectNode();
return objectNode; }
} }
private StreamGraph transOperatoinsToStreamGraph(List<ModifyOperation> modifyOperations) {
List<Transformation<?>> trans = getPlanner().translate(modifyOperations);
final StreamExecutionEnvironment environment = getStreamExecutionEnvironment();
trans.forEach(environment::addOperator);
StreamGraph streamGraph = environment.getStreamGraph();
final Configuration configuration = getConfig().getConfiguration();
if (configuration.containsKey(PipelineOptions.NAME.key())) {
streamGraph.setJobName(configuration.getString(PipelineOptions.NAME));
} }
return streamGraph;
} }
@Override @Override
...@@ -215,29 +217,23 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -215,29 +217,23 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
} }
public StreamGraph getStreamGraphFromInserts(List<String> statements) { public StreamGraph getStreamGraphFromInserts(List<String> statements) {
List<ModifyOperation> modifyOperations = new ArrayList(); List<ModifyOperation> modifyOperations = new ArrayList<>();
for (String statement : statements) { statements.stream()
List<Operation> operations = getParser().parse(statement); .map(statement -> getParser().parse(statement))
.forEach(
operations -> {
if (operations.size() != 1) { if (operations.size() != 1) {
throw new TableException("Only single statement is supported."); throw new TableException("Only single statement is supported.");
} else { }
Operation operation = operations.get(0); Operation operation = operations.get(0);
if (operation instanceof ModifyOperation) { if (operation instanceof ModifyOperation) {
modifyOperations.add((ModifyOperation) operation); modifyOperations.add((ModifyOperation) operation);
} else { } else {
throw new TableException("Only insert statement is supported now."); throw new TableException("Only insert statement is supported now.");
} }
} });
}
List<Transformation<?>> trans = getPlanner().translate(modifyOperations); return transOperatoinsToStreamGraph(modifyOperations);
for (Transformation<?> transformation : trans) {
getStreamExecutionEnvironment().addOperator(transformation);
}
StreamGraph streamGraph = getStreamExecutionEnvironment().getStreamGraph();
if (getConfig().getConfiguration().containsKey(PipelineOptions.NAME.key())) {
streamGraph.setJobName(getConfig().getConfiguration().getString(PipelineOptions.NAME));
}
return streamGraph;
} }
public JobGraph getJobGraphFromInserts(List<String> statements) { public JobGraph getJobGraphFromInserts(List<String> statements) {
...@@ -245,14 +241,17 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -245,14 +241,17 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
} }
public SqlExplainResult explainSqlRecord(String statement, ExplainDetail... extraDetails) { public SqlExplainResult explainSqlRecord(String statement, ExplainDetail... extraDetails) {
SqlExplainResult record = new SqlExplainResult();
List<Operation> operations = getParser().parse(statement); List<Operation> operations = getParser().parse(statement);
record.setParseTrue(true);
if (operations.size() != 1) { if (operations.size() != 1) {
throw new TableException( throw new TableException(
"Unsupported SQL query! explainSql() only accepts a single SQL query."); "Unsupported SQL query! explainSql() only accepts a single SQL query.");
} }
Operation operation = operations.get(0); Operation operation = operations.get(0);
SqlExplainResult record = new SqlExplainResult();
record.setParseTrue(true);
record.setExplainTrue(true);
if (operation instanceof ModifyOperation) { if (operation instanceof ModifyOperation) {
record.setType("Modify DML"); record.setType("Modify DML");
} else if (operation instanceof ExplainOperation) { } else if (operation instanceof ExplainOperation) {
...@@ -262,20 +261,18 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -262,20 +261,18 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
} else { } else {
record.setExplain(operation.asSummaryString()); record.setExplain(operation.asSummaryString());
record.setType("DDL"); record.setType("DDL");
}
record.setExplainTrue(true); // record.setExplain("DDL statement needn't comment。");
if ("DDL".equals(record.getType())) {
// record.setExplain("DDL语句不进行解释。");
return record; return record;
} }
record.setExplain(getPlanner().explain(operations, extraDetails)); record.setExplain(getPlanner().explain(operations, extraDetails));
return record; return record;
} }
public boolean parseAndLoadConfiguration( public boolean parseAndLoadConfiguration(
String statement, StreamExecutionEnvironment environment, Map<String, Object> setMap) { String statement, StreamExecutionEnvironment environment, Map<String, Object> setMap) {
List<Operation> operations = getParser().parse(statement); for (Operation operation : getParser().parse(statement)) {
for (Operation operation : operations) {
if (operation instanceof SetOperation) { if (operation instanceof SetOperation) {
callSet((SetOperation) operation, environment, setMap); callSet((SetOperation) operation, environment, setMap);
return true; return true;
...@@ -291,39 +288,44 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -291,39 +288,44 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
SetOperation setOperation, SetOperation setOperation,
StreamExecutionEnvironment environment, StreamExecutionEnvironment environment,
Map<String, Object> setMap) { Map<String, Object> setMap) {
if (setOperation.getKey().isPresent() && setOperation.getValue().isPresent()) { if (!setOperation.getKey().isPresent() || !setOperation.getValue().isPresent()) {
return;
}
String key = setOperation.getKey().get().trim(); String key = setOperation.getKey().get().trim();
String value = setOperation.getValue().get().trim(); String value = setOperation.getValue().get().trim();
if (Asserts.isNullString(key) || Asserts.isNullString(value)) { if (Asserts.isNullString(key) || Asserts.isNullString(value)) {
return; return;
} }
Map<String, String> confMap = new HashMap<>();
confMap.put(key, value);
setMap.put(key, value); setMap.put(key, value);
Configuration configuration = Configuration.fromMap(confMap);
environment.getConfig().configure(configuration, null); setConfiguration(environment, Collections.singletonMap(key, value));
getConfig().addConfiguration(configuration);
}
} }
private void callReset( private void callReset(
ResetOperation resetOperation, ResetOperation resetOperation,
StreamExecutionEnvironment environment, StreamExecutionEnvironment environment,
Map<String, Object> setMap) { Map<String, Object> setMap) {
if (resetOperation.getKey().isPresent()) { final Optional<String> keyOptional = resetOperation.getKey();
String key = resetOperation.getKey().get().trim(); if (!keyOptional.isPresent()) {
setMap.clear();
return;
}
String key = keyOptional.get().trim();
if (Asserts.isNullString(key)) { if (Asserts.isNullString(key)) {
return; return;
} }
Map<String, String> confMap = new HashMap<>();
confMap.put(key, null);
setMap.remove(key); setMap.remove(key);
Configuration configuration = Configuration.fromMap(confMap); setConfiguration(environment, Collections.singletonMap(key, null));
}
private void setConfiguration(
StreamExecutionEnvironment environment, Map<String, String> config) {
Configuration configuration = Configuration.fromMap(config);
environment.getConfig().configure(configuration, null); environment.getConfig().configure(configuration, null);
getConfig().addConfiguration(configuration); getConfig().addConfiguration(configuration);
} else {
setMap.clear();
}
} }
@Override @Override
......
/*
*
* 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;
/** */
public class LogicUtil {
public static <T> boolean IfThenRun(T object, Class<?> clazz, Runnable action) {
if (object.getClass() == clazz) {
action.run();
return true;
} else {
return false;
}
}
}
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