|
| 1 | +/* |
| 2 | + * Copyright 2019 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_quickstart] |
| 18 | +import com.google.api.gax.longrunning.OperationFuture; |
| 19 | +import com.google.cloud.dataproc.v1.Cluster; |
| 20 | +import com.google.cloud.dataproc.v1.ClusterConfig; |
| 21 | +import com.google.cloud.dataproc.v1.ClusterControllerClient; |
| 22 | +import com.google.cloud.dataproc.v1.ClusterControllerSettings; |
| 23 | +import com.google.cloud.dataproc.v1.ClusterOperationMetadata; |
| 24 | +import com.google.cloud.dataproc.v1.InstanceGroupConfig; |
| 25 | +import com.google.cloud.dataproc.v1.Job; |
| 26 | +import com.google.cloud.dataproc.v1.JobControllerClient; |
| 27 | +import com.google.cloud.dataproc.v1.JobControllerSettings; |
| 28 | +import com.google.cloud.dataproc.v1.JobPlacement; |
| 29 | +import com.google.cloud.dataproc.v1.PySparkJob; |
| 30 | +import com.google.cloud.storage.Blob; |
| 31 | +import com.google.cloud.storage.Storage; |
| 32 | +import com.google.cloud.storage.StorageOptions; |
| 33 | +import com.google.protobuf.Empty; |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.concurrent.CompletableFuture; |
| 36 | +import java.util.concurrent.ExecutionException; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | +import java.util.concurrent.TimeoutException; |
| 39 | + |
| 40 | +public class Quickstart { |
| 41 | + |
| 42 | + public static Job waitForJobCompletion( |
| 43 | + JobControllerClient jobControllerClient, String projectId, String region, String jobId) { |
| 44 | + while (true) { |
| 45 | + // Poll the service periodically until the Job is in a finished state. |
| 46 | + Job jobInfo = jobControllerClient.getJob(projectId, region, jobId); |
| 47 | + switch (jobInfo.getStatus().getState()) { |
| 48 | + case DONE: |
| 49 | + case CANCELLED: |
| 50 | + case ERROR: |
| 51 | + return jobInfo; |
| 52 | + default: |
| 53 | + try { |
| 54 | + // Wait a second in between polling attempts. |
| 55 | + TimeUnit.SECONDS.sleep(1); |
| 56 | + } catch (InterruptedException e) { |
| 57 | + throw new RuntimeException(e); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static void quickstart() throws IOException, InterruptedException { |
| 64 | + // TODO(developer): Replace these variables before running the sample. |
| 65 | + String projectId = "your-project-id"; |
| 66 | + String region = "your-project-region"; |
| 67 | + String clusterName = "your-cluster-name"; |
| 68 | + String jobFilePath = "your-job-file-path"; |
| 69 | + quickstart(projectId, region, clusterName, jobFilePath); |
| 70 | + } |
| 71 | + |
| 72 | + public static void quickstart( |
| 73 | + String projectId, String region, String clusterName, String jobFilePath) |
| 74 | + throws IOException, InterruptedException { |
| 75 | + String myEndpoint = String.format("%s-dataproc.googleapis.com:443", region); |
| 76 | + |
| 77 | + // Configure the settings for the cluster controller client. |
| 78 | + ClusterControllerSettings clusterControllerSettings = |
| 79 | + ClusterControllerSettings.newBuilder().setEndpoint(myEndpoint).build(); |
| 80 | + |
| 81 | + // Configure the settings for the job controller client. |
| 82 | + JobControllerSettings jobControllerSettings = |
| 83 | + JobControllerSettings.newBuilder().setEndpoint(myEndpoint).build(); |
| 84 | + |
| 85 | + // Create both a cluster controller client and job controller client with the configured |
| 86 | + // settings. The client only needs to be created once and can be reused for multiple requests. |
| 87 | + // Using a try-with-resources closes the client, but this can also be done manually with |
| 88 | + // the .close() method. |
| 89 | + try (ClusterControllerClient clusterControllerClient = |
| 90 | + ClusterControllerClient.create(clusterControllerSettings); |
| 91 | + JobControllerClient jobControllerClient = |
| 92 | + JobControllerClient.create(jobControllerSettings)) { |
| 93 | + // Configure the settings for our cluster. |
| 94 | + InstanceGroupConfig masterConfig = |
| 95 | + InstanceGroupConfig.newBuilder() |
| 96 | + .setMachineTypeUri("n1-standard-1") |
| 97 | + .setNumInstances(1) |
| 98 | + .build(); |
| 99 | + InstanceGroupConfig workerConfig = |
| 100 | + InstanceGroupConfig.newBuilder() |
| 101 | + .setMachineTypeUri("n1-standard-1") |
| 102 | + .setNumInstances(2) |
| 103 | + .build(); |
| 104 | + ClusterConfig clusterConfig = |
| 105 | + ClusterConfig.newBuilder() |
| 106 | + .setMasterConfig(masterConfig) |
| 107 | + .setWorkerConfig(workerConfig) |
| 108 | + .build(); |
| 109 | + // Create the cluster object with the desired cluster config. |
| 110 | + Cluster cluster = |
| 111 | + Cluster.newBuilder().setClusterName(clusterName).setConfig(clusterConfig).build(); |
| 112 | + |
| 113 | + // Create the Cloud Dataproc cluster. |
| 114 | + OperationFuture<Cluster, ClusterOperationMetadata> createClusterAsyncRequest = |
| 115 | + clusterControllerClient.createClusterAsync(projectId, region, cluster); |
| 116 | + Cluster response = createClusterAsyncRequest.get(); |
| 117 | + System.out.printf("Cluster created successfully: %s", response.getClusterName()); |
| 118 | + |
| 119 | + // Configure the settings for our job. |
| 120 | + JobPlacement jobPlacement = JobPlacement.newBuilder().setClusterName(clusterName).build(); |
| 121 | + PySparkJob pySparkJob = PySparkJob.newBuilder().setMainPythonFileUri(jobFilePath).build(); |
| 122 | + Job job = Job.newBuilder().setPlacement(jobPlacement).setPysparkJob(pySparkJob).build(); |
| 123 | + |
| 124 | + // Submit an asynchronous request to execute the job. |
| 125 | + Job request = jobControllerClient.submitJob(projectId, region, job); |
| 126 | + String jobId = request.getReference().getJobId(); |
| 127 | + System.out.println(String.format("Submitted job \"%s\"", jobId)); |
| 128 | + |
| 129 | + // Wait for the job to finish. |
| 130 | + CompletableFuture<Job> finishedJobFuture = |
| 131 | + CompletableFuture.supplyAsync( |
| 132 | + () -> waitForJobCompletion(jobControllerClient, projectId, region, jobId)); |
| 133 | + int timeout = 10; |
| 134 | + try { |
| 135 | + Job jobInfo = finishedJobFuture.get(timeout, TimeUnit.MINUTES); |
| 136 | + System.out.printf("Job %s finished successfully.", jobId); |
| 137 | + |
| 138 | + // Cloud Dataproc job output gets saved to a GCS bucket allocated to it. |
| 139 | + Cluster clusterInfo = clusterControllerClient.getCluster(projectId, region, clusterName); |
| 140 | + Storage storage = StorageOptions.getDefaultInstance().getService(); |
| 141 | + Blob blob = |
| 142 | + storage.get( |
| 143 | + clusterInfo.getConfig().getConfigBucket(), |
| 144 | + String.format( |
| 145 | + "google-cloud-dataproc-metainfo/%s/jobs/%s/driveroutput.000000000", |
| 146 | + clusterInfo.getClusterUuid(), jobId)); |
| 147 | + System.out.println( |
| 148 | + String.format( |
| 149 | + "Job \"%s\" finished with state %s:\n%s", |
| 150 | + jobId, jobInfo.getStatus().getState(), new String(blob.getContent()))); |
| 151 | + } catch (TimeoutException e) { |
| 152 | + System.err.println( |
| 153 | + String.format("Job timed out after %d minutes: %s", timeout, e.getMessage())); |
| 154 | + } |
| 155 | + |
| 156 | + // Delete the cluster. |
| 157 | + OperationFuture<Empty, ClusterOperationMetadata> deleteClusterAsyncRequest = |
| 158 | + clusterControllerClient.deleteClusterAsync(projectId, region, clusterName); |
| 159 | + deleteClusterAsyncRequest.get(); |
| 160 | + System.out.println(String.format("Cluster \"%s\" successfully deleted.", clusterName)); |
| 161 | + |
| 162 | + } catch (ExecutionException e) { |
| 163 | + System.err.println(String.format("Error executing quickstart: %s ", e.getMessage())); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | +// [END dataproc_quickstart] |
0 commit comments