|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START dataproc_instantiate_inline_workflow] |
| 18 | +import com.google.api.gax.longrunning.OperationFuture; |
| 19 | +import com.google.cloud.dataproc.v1.ClusterConfig; |
| 20 | +import com.google.cloud.dataproc.v1.GceClusterConfig; |
| 21 | +import com.google.cloud.dataproc.v1.HadoopJob; |
| 22 | +import com.google.cloud.dataproc.v1.ManagedCluster; |
| 23 | +import com.google.cloud.dataproc.v1.OrderedJob; |
| 24 | +import com.google.cloud.dataproc.v1.RegionName; |
| 25 | +import com.google.cloud.dataproc.v1.WorkflowMetadata; |
| 26 | +import com.google.cloud.dataproc.v1.WorkflowTemplate; |
| 27 | +import com.google.cloud.dataproc.v1.WorkflowTemplatePlacement; |
| 28 | +import com.google.cloud.dataproc.v1.WorkflowTemplateServiceClient; |
| 29 | +import com.google.cloud.dataproc.v1.WorkflowTemplateServiceSettings; |
| 30 | +import com.google.protobuf.Empty; |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.concurrent.ExecutionException; |
| 33 | + |
| 34 | +public class InstantiateInlineWorkflow { |
| 35 | + |
| 36 | + public static void InstantiateInlineWorkflow() throws IOException, InterruptedException { |
| 37 | + // TODO(developer): Replace these variables before running the sample. |
| 38 | + String projectId = "your-project-id"; |
| 39 | + String region = "your-project-region"; |
| 40 | + instantiateInlineWorkflow(projectId, region); |
| 41 | + } |
| 42 | + |
| 43 | + public static void instantiateInlineWorkflow(String projectId, String region) |
| 44 | + throws IOException, InterruptedException { |
| 45 | + String myEndpoint = String.format("%s-dataproc.googleapis.com:443", region); |
| 46 | + |
| 47 | + // Configure the settings for the workflow template service client. |
| 48 | + WorkflowTemplateServiceSettings workflowTemplateServiceSettings = |
| 49 | + WorkflowTemplateServiceSettings.newBuilder().setEndpoint(myEndpoint).build(); |
| 50 | + |
| 51 | + // Create a workflow template service client with the configured settings. The client only |
| 52 | + // needs to be created once and can be reused for multiple requests. Using a try-with-resources |
| 53 | + // closes the client, but this can also be done manually with the .close() method. |
| 54 | + try (WorkflowTemplateServiceClient workflowTemplateServiceClient = |
| 55 | + WorkflowTemplateServiceClient.create(workflowTemplateServiceSettings)) { |
| 56 | + |
| 57 | + // Configure the jobs within the workflow. |
| 58 | + HadoopJob teragenHadoopJob = |
| 59 | + HadoopJob.newBuilder() |
| 60 | + .setMainJarFileUri("file:///usr/lib/hadoop-mapreduce/hadoop-mapreduce-examples.jar") |
| 61 | + .addArgs("teragen") |
| 62 | + .addArgs("1000") |
| 63 | + .addArgs("hdfs:///gen/") |
| 64 | + .build(); |
| 65 | + OrderedJob teragen = |
| 66 | + OrderedJob.newBuilder().setHadoopJob(teragenHadoopJob).setStepId("teragen").build(); |
| 67 | + |
| 68 | + HadoopJob terasortHadoopJob = |
| 69 | + HadoopJob.newBuilder() |
| 70 | + .setMainJarFileUri("file:///usr/lib/hadoop-mapreduce/hadoop-mapreduce-examples.jar") |
| 71 | + .addArgs("terasort") |
| 72 | + .addArgs("hdfs:///gen/") |
| 73 | + .addArgs("hdfs:///sort/") |
| 74 | + .build(); |
| 75 | + OrderedJob terasort = |
| 76 | + OrderedJob.newBuilder() |
| 77 | + .setHadoopJob(terasortHadoopJob) |
| 78 | + .addPrerequisiteStepIds("teragen") |
| 79 | + .setStepId("terasort") |
| 80 | + .build(); |
| 81 | + |
| 82 | + // Configure the cluster placement for the workflow. |
| 83 | + GceClusterConfig gceClusterConfig = |
| 84 | + GceClusterConfig.newBuilder().setZoneUri("us-central1-a").build(); |
| 85 | + ClusterConfig clusterConfig = |
| 86 | + ClusterConfig.newBuilder().setGceClusterConfig(gceClusterConfig).build(); |
| 87 | + ManagedCluster managedCluster = |
| 88 | + ManagedCluster.newBuilder() |
| 89 | + .setClusterName("my-managed-cluster") |
| 90 | + .setConfig(clusterConfig) |
| 91 | + .build(); |
| 92 | + WorkflowTemplatePlacement workflowTemplatePlacement = |
| 93 | + WorkflowTemplatePlacement.newBuilder().setManagedCluster(managedCluster).build(); |
| 94 | + |
| 95 | + // Create the inline workflow template. |
| 96 | + WorkflowTemplate workflowTemplate = |
| 97 | + WorkflowTemplate.newBuilder() |
| 98 | + .addJobs(teragen) |
| 99 | + .addJobs(terasort) |
| 100 | + .setPlacement(workflowTemplatePlacement) |
| 101 | + .build(); |
| 102 | + |
| 103 | + // Submit the instantiated inline workflow template request. |
| 104 | + String parent = RegionName.format(projectId, region); |
| 105 | + OperationFuture<Empty, WorkflowMetadata> instantiateInlineWorkflowTemplateAsync = |
| 106 | + workflowTemplateServiceClient.instantiateInlineWorkflowTemplateAsync( |
| 107 | + parent, workflowTemplate); |
| 108 | + instantiateInlineWorkflowTemplateAsync.get(); |
| 109 | + |
| 110 | + // Print out a success message. |
| 111 | + System.out.printf("Workflow ran successfully."); |
| 112 | + |
| 113 | + } catch (ExecutionException e) { |
| 114 | + System.err.println(String.format("Error running workflow: %s ", e.getMessage())); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +// [END dataproc_instantiate_inline_workflow] |
0 commit comments