Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.

Commit 961ff59

Browse files
authored
feat: adding samples for dataproc - create cluster [(#2536)](GoogleCloudPlatform/python-docs-samples#2536)
* adding sample for cluster create * small fix * Add create cluster samples * Fixed copyright, added 'dataproc' to region tag and changed imports from 'dataproc' to 'dataproc_v1' * Fix copyright in create_cluster.py
1 parent 1e3d12b commit 961ff59

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

samples/snippets/create_cluster.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
def create_cluster(project_id, region, cluster_name):
19+
# [START dataproc_create_cluster]
20+
from google.cloud import dataproc_v1 as dataproc
21+
22+
# TODO(developer): Uncomment and set the following variables
23+
# project_id = 'YOUR_PROJECT_ID'
24+
# region = 'YOUR_CLUSTER_REGION'
25+
# cluster_name = 'YOUR_CLUSTER_NAME'
26+
27+
# Create a client with the endpoint set to the desired cluster region
28+
client = dataproc.ClusterControllerClient(client_options={
29+
'api_endpoint': '{}-dataproc.googleapis.com:443'.format(region)
30+
})
31+
32+
# Create the cluster config
33+
cluster = {
34+
'project_id': project_id,
35+
'cluster_name': cluster_name,
36+
'config': {
37+
'master_config': {
38+
'num_instances': 1,
39+
'machine_type_uri': 'n1-standard-1'
40+
},
41+
'worker_config': {
42+
'num_instances': 2,
43+
'machine_type_uri': 'n1-standard-1'
44+
}
45+
}
46+
}
47+
48+
# Create the cluster
49+
operation = client.create_cluster(project_id, region, cluster)
50+
result = operation.result()
51+
52+
# Output a success message
53+
print('Cluster created successfully: {}'.format(result.cluster_name))
54+
# [END dataproc_create_cluster]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
import pytest
18+
19+
from google.cloud import dataproc_v1 as dataproc
20+
21+
import create_cluster
22+
23+
PROJECT_ID = os.environ['GCLOUD_PROJECT']
24+
REGION = 'us-central1'
25+
CLUSTER_NAME = 'test-cluster-{}'.format(str(uuid.uuid4()))
26+
27+
28+
@pytest.fixture(autouse=True)
29+
def teardown():
30+
yield
31+
32+
client = dataproc.ClusterControllerClient(client_options={
33+
'api_endpoint': '{}-dataproc.googleapis.com:443'.format(REGION)
34+
})
35+
# Client library function
36+
client.delete_cluster(PROJECT_ID, REGION, CLUSTER_NAME)
37+
38+
39+
def test_cluster_create(capsys):
40+
# Wrapper function for client library function
41+
create_cluster.create_cluster(PROJECT_ID, REGION, CLUSTER_NAME)
42+
43+
out, _ = capsys.readouterr()
44+
assert CLUSTER_NAME in out

samples/snippets/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ google-auth==1.6.3
33
google-auth-httplib2==0.0.3
44
google-cloud==0.34.0
55
google-cloud-storage==1.19.1
6-
google-cloud-dataproc==0.5.0
6+
google-cloud-dataproc==0.6.1

0 commit comments

Comments
 (0)