Skip to content

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 15 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 53 additions & 0 deletions dataproc/pom.xml
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>
84 changes: 84 additions & 0 deletions dataproc/src/main/java/CreateCluster.java
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())
);

} catch (IOException e) {
// 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]
88 changes: 88 additions & 0 deletions dataproc/src/test/java/CreateClusterTest.java
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());
}
}
}