Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dataproc quickstart and createcluster #1908

Merged
merged 29 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8b74842
Added dataproc quickstart samples
bradmiro Dec 2, 2019
0c132cd
Fixed linting, string formatting, copyrights
bradmiro Dec 2, 2019
c242a0c
added overloaded functions to all samples
bradmiro Dec 2, 2019
aa03238
Formatting changes
bradmiro Dec 2, 2019
e2b74e5
small bug fixes
bradmiro Dec 2, 2019
f4bb1cc
Merge branch 'master' into dataproc-samples
bradmiro Dec 2, 2019
84810dc
Merge branch 'master' into dataproc-samples
bradmiro Dec 3, 2019
ee02b72
Fixed CreateCluster sample and added Quickstart
bradmiro Dec 20, 2019
400617c
Added quickstart sample
bradmiro Dec 20, 2019
d0efbbc
Added dataproc quickstart samples
bradmiro Dec 2, 2019
2970060
Fixed linting, string formatting, copyrights
bradmiro Dec 2, 2019
ebe059d
added overloaded functions to all samples
bradmiro Dec 2, 2019
6b20c68
Formatting changes
bradmiro Dec 2, 2019
9990637
small bug fixes
bradmiro Dec 2, 2019
5a9a3a3
Fixed CreateCluster sample and added Quickstart
bradmiro Dec 20, 2019
b9eaf25
Added quickstart sample
bradmiro Dec 20, 2019
ea1965f
Updates to createCluster and quickstart
bradmiro Dec 26, 2019
539746f
Fixed quickstart and tests
bradmiro Dec 26, 2019
e7b91c2
Fixed quickstart and tests
bradmiro Dec 26, 2019
cdc616e
Changes to tests
bradmiro Dec 27, 2019
df9d3b9
Merge branch 'master' into dataproc-samples
bradmiro Dec 27, 2019
561ca34
Added periods to comments
bradmiro Dec 27, 2019
e6e9e83
Merge branch 'dataproc-samples' of https://github.com/GoogleCloudPlat…
bradmiro Dec 27, 2019
60822ba
Merge branch 'master' into dataproc-samples
bradmiro Dec 30, 2019
1ad40b6
Fixed pom and added handling for ExecutionException
bradmiro Dec 30, 2019
7f61371
Merge branch 'master' into dataproc-samples
bradmiro Dec 30, 2019
64dbb5e
Fixed lint errors
bradmiro Dec 30, 2019
aebba92
Merge branch 'dataproc-samples' of https://github.com/GoogleCloudPlat…
bradmiro Dec 30, 2019
db2c76f
Fixed linting errors
bradmiro Dec 30, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added quickstart sample
  • Loading branch information
bradmiro committed Dec 20, 2019
commit 400617c65cae60022640b3a38da578a34c1678cd
14 changes: 12 additions & 2 deletions dataproc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
</pluginManagement>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>3.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- Test dependencies -->
<dependency>
Expand All @@ -47,12 +59,10 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-dataproc</artifactId>
<version>0.117.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.101.0</version>
</dependency>
</dependencies>

Expand Down
172 changes: 172 additions & 0 deletions dataproc/src/main/java/Quickstart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Copyright 2019 Google LLC
*
* Licensed 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.
*/

// [START dataproc_quickstart]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.dataproc.v1.Cluster;
import com.google.cloud.dataproc.v1.ClusterConfig;
import com.google.cloud.dataproc.v1.ClusterControllerClient;
import com.google.cloud.dataproc.v1.ClusterControllerSettings;
import com.google.cloud.dataproc.v1.ClusterOperationMetadata;
import com.google.cloud.dataproc.v1.InstanceGroupConfig;
import com.google.cloud.dataproc.v1.Job;
import com.google.cloud.dataproc.v1.JobControllerClient;
import com.google.cloud.dataproc.v1.JobControllerSettings;
import com.google.cloud.dataproc.v1.JobPlacement;
import com.google.cloud.dataproc.v1.PySparkJob;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Quickstart {

public static void quickstart() throws IOException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String region = "your-project-region";
String clusterName = "your-cluster-name";
String jobFilePath = "your-job-file-path";
quickstart(projectId, region, clusterName, jobFilePath);
}

public static void quickstart(
String projectId, String region, String clusterName, String jobFilePath)
throws IOException, InterruptedException {
String myEndpoint = String.format("%s-dataproc.googleapis.com:443", region);

// Configure the settings for the cluster controller client
ClusterControllerSettings clusterControllerSettings =
ClusterControllerSettings.newBuilder().setEndpoint(myEndpoint).build();

// Configure the settings for the job controller client
JobControllerSettings jobControllerSettings =
JobControllerSettings.newBuilder().setEndpoint(myEndpoint).build();

// Create both a cluster controller client and job controller client with the configured
// settings. The client only needs to be created once and can be reused for multiple requests.
// Using a try-with-resources closes the client, but this can also be done manually with
// the .close() method.
try (ClusterControllerClient clusterControllerClient =
ClusterControllerClient.create(clusterControllerSettings);
JobControllerClient jobControllerClient =
JobControllerClient.create(jobControllerSettings)) {
// Configure the settings for our cluster
InstanceGroupConfig masterConfig =
InstanceGroupConfig.newBuilder()
.setMachineTypeUri("n1-standard-1")
.setNumInstances(1)
.build();
InstanceGroupConfig workerConfig =
InstanceGroupConfig.newBuilder()
.setMachineTypeUri("n1-standard-1")
.setNumInstances(2)
.build();
ClusterConfig clusterConfig =
ClusterConfig.newBuilder()
.setMasterConfig(masterConfig)
.setWorkerConfig(workerConfig)
.build();
// Create the cluster object with the desired cluster config
Cluster cluster =
Cluster.newBuilder().setClusterName(clusterName).setConfig(clusterConfig).build();

// Create the Cloud Dataproc cluster
OperationFuture<Cluster, ClusterOperationMetadata> createClusterAsyncRequest =
clusterControllerClient.createClusterAsync(projectId, region, cluster);
Cluster response = createClusterAsyncRequest.get();
System.out.printf("Cluster created successfully: %s", response.getClusterName());

// Configure the settings for our job
JobPlacement jobPlacement = JobPlacement.newBuilder().setClusterName(clusterName).build();
PySparkJob pySparkJob = PySparkJob.newBuilder().setMainPythonFileUri(jobFilePath).build();
Job job = Job.newBuilder().setPlacement(jobPlacement).setPysparkJob(pySparkJob).build();

// Submit an asynchronous request to execute the job
Job request = jobControllerClient.submitJob(projectId, region, job);
String jobId = request.getReference().getJobId();
System.out.println(String.format("Submitted job \"%s\"", jobId));

// Wait for the job to finish
CompletableFuture<Job> finishedJobFuture =
CompletableFuture.supplyAsync(
() -> {
// States that indicate a job is "finished"
while (true) {
// Poll the service periodically until the Job is in a finished state.
Job jobInfo = jobControllerClient.getJob(projectId, region, jobId);
switch (jobInfo.getStatus().getState()) {
case DONE:
return jobInfo;
case CANCELLED:
case ERROR:
case ATTEMPT_FAILURE:
throw new RuntimeException(
String.format(
"Job %s failed due to %s: %s",
jobId,
jobInfo.getStatus().getState(),
jobInfo.getStatus().getDetails()));
default:
try {
// Wait a second in between polling attempts.
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
int timeout = 10;
try {
Job jobInfo = finishedJobFuture.get(timeout, TimeUnit.MINUTES);
System.out.printf("Job %s finished successfully.", jobId);

// Cloud Dataproc job output gets saved to a GCS bucket allocated to it.
Cluster clusterInfo = clusterControllerClient.getCluster(projectId, region, clusterName);
Storage storage = StorageOptions.getDefaultInstance().getService();
Blob blob =
storage.get(
clusterInfo.getConfig().getConfigBucket(),
String.format(
"google-cloud-dataproc-metainfo/%s/jobs/%s/driveroutput.000000000",
clusterInfo.getClusterUuid(), jobId));
System.out.println(
String.format(
"Job \"%s\" finished with state %s:\n%s",
jobId, jobInfo.getStatus().getState(), new String(blob.getContent())));
} catch (TimeoutException e) {
System.err.println(
String.format("Job timed out after %d minutes: %s", timeout, e.getMessage()));
}

// Delete the cluster
OperationFuture<Empty, ClusterOperationMetadata> deleteClusterAsyncRequest =
clusterControllerClient.deleteClusterAsync(projectId, region, clusterName);
deleteClusterAsyncRequest.get();
System.out.println(String.format("Cluster \"%s\" successfully deleted.", clusterName));

} catch (ExecutionException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ExecutionException occurs from interacting with the API (like it's thrown if you don't have permission or if your request is misconfigured), keep it and clarify that it's an error from interacting with the service with the println.

If it's not from the API, please let it bubble up (and be consistent with the other samples - it's bubbled up in a few other places).

Copy link
Contributor Author

@bradmiro bradmiro Dec 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes, I see what you're saying. Errors caused by our API bubble up to be an Execution Exception, so I think it should definitely be caught. I'll add it to the other sample.

System.err.println(String.format("Error executing quickstart: " + e.getMessage()));
}
}
}
// [END dataproc_quickstart]
91 changes: 0 additions & 91 deletions dataproc/src/test/java/CreateCluster

This file was deleted.