-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: adding CreateCluster sample for Dataproc #1734
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0392815
refactored and added tags to infinite speech streaming sample (#1605)
bradmiro 0078c4a
Changed 'main' region tag
bradmiro a69558d
Removed extra lines around tags and changed client import to v1
bradmiro 6a68e0a
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-…
bradmiro 69eeae6
Create dataproc directory and add CreateCluster sample
bradmiro de1aa3d
reverting changes to speech infinite streaming sample
bradmiro 73f3116
Added java versions to pom
bradmiro 6cfccf6
Several changes to file formatting as per request in the PR
bradmiro 1421f57
Added comments to exceptions in CreateCluster, expanded exceptions a…
bradmiro 30f4b42
Fixed version for parent config
bradmiro c4b1d82
Added clarity to futures requests by expanding variables
bradmiro 85aff43
Fixed linting errors
bradmiro f65765e
Fixed import ordering
bradmiro 6beb303
Moved exceptions to function level in dataproc create cluster sample …
bradmiro 160f97e
Merge branch 'master' into dataproc-samples
bradmiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.example</groupId> | ||
<artifactId>dataproc</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.0.11</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
|
||
<dependencies> | ||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-dataproc</artifactId> | ||
<version>0.117.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2019 Google Inc. | ||
* | ||
* 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 create_cluster] | ||
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 java.io.IOException; | ||
import java.io.InterruptedIOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class CreateCluster { | ||
|
||
public static void createCluster(String projectId, String region, String clusterName) | ||
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(); | ||
|
||
// Create a cluster controller client with the configured settings. We only need to create | ||
// the client once, and can be reused for multiple requests. Using a try-with-resources | ||
// will close the client for us, but this can also be done manually with the .close() method. | ||
try (ClusterControllerClient clusterControllerClient = ClusterControllerClient | ||
.create(clusterControllerSettings)) { | ||
// 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(); | ||
|
||
// Send a request to create a Dataproc cluster. | ||
OperationFuture<Cluster, ClusterOperationMetadata> createClusterAsyncRequest = | ||
clusterControllerClient.createClusterAsync(projectId, region, cluster); | ||
Cluster response = createClusterAsyncRequest.get(); | ||
|
||
// Print out the response | ||
System.out.println( | ||
String.format("Cluster created successfully: %s", response.getClusterName()) | ||
); | ||
|
||
bradmiro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (IOException e) { | ||
kurtisvg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Likely this would occur due to issues authenticating with GCP. Make sure the environment | ||
// variable GOOGLE_APPLICATION_CREDENTIALS is configured. | ||
System.out.println("Error creating the cluster controller client: \n" + e.toString()); | ||
} catch (ExecutionException e) { | ||
// Common issues for this include needing to increase compute engine quotas or a cluster of | ||
// the same name already exists. | ||
System.out.println("Error during cluster creation request: \n" + e.toString()); | ||
} | ||
} | ||
} | ||
// [END create_cluster] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import static junit.framework.TestCase.assertNotNull; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.dataproc.v1.ClusterControllerClient; | ||
import com.google.cloud.dataproc.v1.ClusterOperationMetadata; | ||
import com.google.protobuf.Empty; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class CreateClusterTest { | ||
|
||
private static final String BASE_CLUSTER_NAME = "test-cluster"; | ||
private static final String REGION = "us-central1"; | ||
|
||
private static String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private String clusterName; | ||
private ByteArrayOutputStream bout; | ||
|
||
private static void requireEnv(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
String.format("Environment variable '%s' is required to perform these tests.", varName) | ||
); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnv("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnv("GOOGLE_CLOUD_PROJECT"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
clusterName = String.format("%s-%s", BASE_CLUSTER_NAME, UUID.randomUUID().toString()); | ||
|
||
bout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(bout)); | ||
} | ||
|
||
@Test | ||
public void createClusterTest() throws IOException, InterruptedException { | ||
CreateCluster.createCluster(projectId, REGION, clusterName); | ||
String output = bout.toString(); | ||
|
||
assertThat(output, CoreMatchers.containsString(clusterName)); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException, InterruptedException { | ||
try (ClusterControllerClient clusterControllerClient = ClusterControllerClient | ||
.create()) { | ||
OperationFuture<Empty, ClusterOperationMetadata> deleteClusterAsyncRequest = | ||
clusterControllerClient.deleteClusterAsync(projectId, REGION, clusterName); | ||
deleteClusterAsyncRequest.get(); | ||
|
||
} catch (ExecutionException e) { | ||
System.out.println("Error during cluster deletion: \n" + e.toString()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.