Skip to content

Commit 2439415

Browse files
author
Sudeep Agarwal
committed
Add sample for updating cluster
1 parent f39766c commit 2439415

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

dataproc/create_cluster_and_submit_job.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,35 @@ def get_cluster_id_by_name(cluster_list, cluster_name):
119119
cluster = [c for c in cluster_list if c['clusterName'] == cluster_name][0]
120120
return cluster['clusterUuid'], cluster['config']['configBucket']
121121

122+
# [START update_cluster]
123+
def update_cluster(dataproc, project, cluster_name, zone, num_workers):
124+
"""Updates the specified cluster to the given number of workers.
125+
Use 'config.worker_config.num_instances' and 'workerConfig' for workers.
126+
Use 'config.secondary_worker_config.num_instances' and
127+
'secondaryWorkerConfig' for preemptible workers."""
128+
print('Updating cluster to %d workers.' % num_workers)
129+
zone_uri = \
130+
'https://www.googleapis.com/compute/v1/projects/{}/zones/{}'.format(
131+
project, zone)
132+
cluster_data = {
133+
'projectId': project,
134+
'clusterName': cluster_name,
135+
'config': {
136+
'gceClusterConfig': {
137+
'zoneUri': zone_uri
138+
},
139+
'workerConfig': {
140+
'numInstances': num_workers
141+
}
142+
}
143+
}
144+
result = dataproc.projects().regions().clusters().patch(
145+
projectId = project,
146+
region = REGION,
147+
clusterName = cluster_name,
148+
updateMask = 'config.worker_config.num_instances',
149+
body = cluster_data).execute()
150+
# [END update_cluster]
122151

123152
# [START submit_pyspark_job]
124153
def submit_pyspark_job(dataproc, project, cluster_name, bucket_name, filename):

0 commit comments

Comments
 (0)