|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.dtstack.flink.sql.launcher.executor; |
| 20 | + |
| 21 | +import com.dtstack.flink.sql.enums.EPluginLoadMode; |
| 22 | +import com.dtstack.flink.sql.launcher.entity.JobParamsInfo; |
| 23 | +import com.dtstack.flink.sql.launcher.factory.StandaloneClientFactory; |
| 24 | +import com.dtstack.flink.sql.launcher.utils.JobGraphBuildUtil; |
| 25 | +import org.apache.commons.lang3.StringUtils; |
| 26 | +import org.apache.flink.api.common.JobExecutionResult; |
| 27 | +import org.apache.flink.client.ClientUtils; |
| 28 | +import org.apache.flink.client.deployment.ClusterDescriptor; |
| 29 | +import org.apache.flink.client.deployment.StandaloneClusterId; |
| 30 | +import org.apache.flink.client.program.ClusterClient; |
| 31 | +import org.apache.flink.client.program.ClusterClientProvider; |
| 32 | +import org.apache.flink.configuration.Configuration; |
| 33 | +import org.apache.flink.runtime.jobgraph.JobGraph; |
| 34 | +import org.apache.flink.util.Preconditions; |
| 35 | + |
| 36 | +/** |
| 37 | + * Date: 2020/3/6 |
| 38 | + * Company: www.dtstack.com |
| 39 | + * @author maqi |
| 40 | + */ |
| 41 | +public class StandaloneExecutor { |
| 42 | + |
| 43 | + StandaloneClientFactory standaloneClientFactory; |
| 44 | + JobParamsInfo jobParamsInfo; |
| 45 | + |
| 46 | + public StandaloneExecutor(JobParamsInfo jobParamsInfo) { |
| 47 | + this.jobParamsInfo = jobParamsInfo; |
| 48 | + standaloneClientFactory = new StandaloneClientFactory(); |
| 49 | + } |
| 50 | + |
| 51 | + public void exec() throws Exception { |
| 52 | + |
| 53 | + Preconditions.checkArgument(!StringUtils.equalsIgnoreCase(jobParamsInfo.getPluginLoadMode(), EPluginLoadMode.CLASSPATH.name()), |
| 54 | + "standalone only supports classpath mode"); |
| 55 | + |
| 56 | + JobGraph jobGraph = JobGraphBuildUtil.buildJobGraph(jobParamsInfo); |
| 57 | + Configuration flinkConfiguration = JobGraphBuildUtil.getFlinkConfiguration(jobParamsInfo.getFlinkConfDir(), jobParamsInfo.getConfProperties()); |
| 58 | + |
| 59 | + if (!StringUtils.isBlank(jobParamsInfo.getUdfJar())) { |
| 60 | + JobGraphBuildUtil.fillUserJarForJobGraph(jobParamsInfo.getUdfJar(), jobGraph); |
| 61 | + } |
| 62 | + |
| 63 | + JobGraphBuildUtil.fillJobGraphClassPath(jobGraph); |
| 64 | + |
| 65 | + ClusterDescriptor clusterDescriptor = standaloneClientFactory.createClusterDescriptor("", flinkConfiguration); |
| 66 | + ClusterClientProvider clusterClientProvider = clusterDescriptor.retrieve(StandaloneClusterId.getInstance()); |
| 67 | + ClusterClient clusterClient = clusterClientProvider.getClusterClient(); |
| 68 | + |
| 69 | + |
| 70 | + JobExecutionResult jobExecutionResult = ClientUtils.submitJob(clusterClient, jobGraph); |
| 71 | + String jobID = jobExecutionResult.getJobID().toString(); |
| 72 | + System.out.println("jobID:" + jobID); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | +} |
0 commit comments