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;
import org.dinky.utils.FlinkStreamProgramWithoutPhysical;
import org.dinky.utils.LineageContext;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.dag.Transformation;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.ExecutionOptions;
import org.apache.flink.configuration.PipelineOptions;
import org.apache.flink.runtime.jobgraph.JobGraph;
import org.apache.flink.runtime.jobgraph.jsonplan.JsonPlanGenerator;
......@@ -64,9 +62,14 @@ import org.apache.flink.util.MutableURLClassLoader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;
import java.util.List;
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.databind.ObjectMapper;
......@@ -80,7 +83,10 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
*/
public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
private static final Logger log = LoggerFactory.getLogger(CustomTableEnvironmentImpl.class);
private final FlinkChainedProgram flinkChainedProgram;
private static final ObjectMapper mapper = new ObjectMapper();
public CustomTableEnvironmentImpl(
CatalogManager catalogManager,
......@@ -115,10 +121,6 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
public static CustomTableEnvironmentImpl createBatch(
StreamExecutionEnvironment executionEnvironment) {
Configuration configuration = new Configuration();
configuration.set(ExecutionOptions.RUNTIME_MODE, RuntimeExecutionMode.BATCH);
TableConfig tableConfig = new TableConfig();
tableConfig.addConfiguration(configuration);
return create(
executionEnvironment, EnvironmentSettings.newInstance().inBatchMode().build());
}
......@@ -134,10 +136,6 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
tableConfig.setRootConfiguration(executor.getConfiguration());
tableConfig.addConfiguration(settings.getConfiguration());
final ResourceManager resourceManager =
new ResourceManager(settings.getConfiguration(), userClassLoader);
final ModuleManager moduleManager = new ModuleManager();
final CatalogManager catalogManager =
CatalogManager.newBuilder()
.classLoader(userClassLoader)
......@@ -150,6 +148,9 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
.executionConfig(executionEnvironment.getConfig())
.build();
final ModuleManager moduleManager = new ModuleManager();
final ResourceManager resourceManager =
new ResourceManager(settings.getConfiguration(), userClassLoader);
final FunctionCatalog functionCatalog =
new FunctionCatalog(tableConfig, resourceManager, catalogManager, moduleManager);
......@@ -179,65 +180,60 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
if (operations.size() != 1) {
throw new TableException(
"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));
}
JSONGenerator jsonGenerator = new JSONGenerator(streamGraph);
String json = jsonGenerator.getJSON();
ObjectMapper mapper = new ObjectMapper();
ObjectNode objectNode = mapper.createObjectNode();
try {
objectNode = (ObjectNode) mapper.readTree(json);
} catch (JsonProcessingException e) {
e.printStackTrace();
} finally {
return objectNode;
}
}
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);
try {
return (ObjectNode) mapper.readTree(jsonGenerator.getJSON());
} catch (JsonProcessingException e) {
log.error("read streamGraph configure error: ", e);
return mapper.createObjectNode();
}
}
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
public JobPlanInfo getJobPlanInfo(List<String> statements) {
return new JobPlanInfo(JsonPlanGenerator.generatePlan(getJobGraphFromInserts(statements)));
}
public StreamGraph getStreamGraphFromInserts(List<String> statements) {
List<ModifyOperation> modifyOperations = new ArrayList();
for (String statement : statements) {
List<Operation> operations = getParser().parse(statement);
if (operations.size() != 1) {
throw new TableException("Only single statement is supported.");
} else {
Operation operation = operations.get(0);
if (operation instanceof ModifyOperation) {
modifyOperations.add((ModifyOperation) operation);
} else {
throw new TableException("Only insert statement is supported now.");
}
}
}
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));
}
return streamGraph;
List<ModifyOperation> modifyOperations = new ArrayList<>();
statements.stream()
.map(statement -> getParser().parse(statement))
.forEach(
operations -> {
if (operations.size() != 1) {
throw new TableException("Only single statement is supported.");
}
Operation operation = operations.get(0);
if (operation instanceof ModifyOperation) {
modifyOperations.add((ModifyOperation) operation);
} else {
throw new TableException("Only insert statement is supported now.");
}
});
return transOperatoinsToStreamGraph(modifyOperations);
}
public JobGraph getJobGraphFromInserts(List<String> statements) {
......@@ -245,14 +241,17 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
}
public SqlExplainResult explainSqlRecord(String statement, ExplainDetail... extraDetails) {
SqlExplainResult record = new SqlExplainResult();
List<Operation> operations = getParser().parse(statement);
record.setParseTrue(true);
if (operations.size() != 1) {
throw new TableException(
"Unsupported SQL query! explainSql() only accepts a single SQL query.");
}
Operation operation = operations.get(0);
SqlExplainResult record = new SqlExplainResult();
record.setParseTrue(true);
record.setExplainTrue(true);
if (operation instanceof ModifyOperation) {
record.setType("Modify DML");
} else if (operation instanceof ExplainOperation) {
......@@ -262,20 +261,18 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
} else {
record.setExplain(operation.asSummaryString());
record.setType("DDL");
}
record.setExplainTrue(true);
if ("DDL".equals(record.getType())) {
// record.setExplain("DDL语句不进行解释。");
// record.setExplain("DDL statement needn't comment。");
return record;
}
record.setExplain(getPlanner().explain(operations, extraDetails));
return record;
}
public boolean parseAndLoadConfiguration(
String statement, StreamExecutionEnvironment environment, Map<String, Object> setMap) {
List<Operation> operations = getParser().parse(statement);
for (Operation operation : operations) {
for (Operation operation : getParser().parse(statement)) {
if (operation instanceof SetOperation) {
callSet((SetOperation) operation, environment, setMap);
return true;
......@@ -291,39 +288,44 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
SetOperation setOperation,
StreamExecutionEnvironment environment,
Map<String, Object> setMap) {
if (setOperation.getKey().isPresent() && setOperation.getValue().isPresent()) {
String key = setOperation.getKey().get().trim();
String value = setOperation.getValue().get().trim();
if (Asserts.isNullString(key) || Asserts.isNullString(value)) {
return;
}
Map<String, String> confMap = new HashMap<>();
confMap.put(key, value);
setMap.put(key, value);
Configuration configuration = Configuration.fromMap(confMap);
environment.getConfig().configure(configuration, null);
getConfig().addConfiguration(configuration);
if (!setOperation.getKey().isPresent() || !setOperation.getValue().isPresent()) {
return;
}
String key = setOperation.getKey().get().trim();
String value = setOperation.getValue().get().trim();
if (Asserts.isNullString(key) || Asserts.isNullString(value)) {
return;
}
setMap.put(key, value);
setConfiguration(environment, Collections.singletonMap(key, value));
}
private void callReset(
ResetOperation resetOperation,
StreamExecutionEnvironment environment,
Map<String, Object> setMap) {
if (resetOperation.getKey().isPresent()) {
String key = resetOperation.getKey().get().trim();
if (Asserts.isNullString(key)) {
return;
}
Map<String, String> confMap = new HashMap<>();
confMap.put(key, null);
setMap.remove(key);
Configuration configuration = Configuration.fromMap(confMap);
environment.getConfig().configure(configuration, null);
getConfig().addConfiguration(configuration);
} else {
final Optional<String> keyOptional = resetOperation.getKey();
if (!keyOptional.isPresent()) {
setMap.clear();
return;
}
String key = keyOptional.get().trim();
if (Asserts.isNullString(key)) {
return;
}
setMap.remove(key);
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);
getConfig().addConfiguration(configuration);
}
@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