|
| 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 | +# [START dataproc_quickstart] |
| 18 | +import time |
| 19 | + |
| 20 | +from google.cloud import dataproc_v1 as dataproc |
| 21 | +from google.cloud import storage |
| 22 | + |
| 23 | + |
| 24 | +def quickstart(project_id, region, cluster_name, job_file_path): |
| 25 | + """This quickstart sample walks a user through creating a Cloud Dataproc |
| 26 | + cluster, submitting a PySpark job from Google Cloud Storage to the |
| 27 | + cluster, reading the output of the job and deleting the cluster, all |
| 28 | + using the Python client library. |
| 29 | +
|
| 30 | + Args: |
| 31 | + project_id (string): Project to use for creating resources. |
| 32 | + region (string): Region where the resources should live. |
| 33 | + cluster_name (string): Name to use for creating a cluster. |
| 34 | + job_file_path (string): Job in GCS to execute against the cluster. |
| 35 | + """ |
| 36 | + |
| 37 | + # Create the cluster client. |
| 38 | + cluster_client = dataproc.ClusterControllerClient(client_options={ |
| 39 | + 'api_endpoint': '{}-dataproc.googleapis.com:443'.format(region) |
| 40 | + }) |
| 41 | + |
| 42 | + # Create the cluster config. |
| 43 | + cluster = { |
| 44 | + 'project_id': project_id, |
| 45 | + 'cluster_name': cluster_name, |
| 46 | + 'config': { |
| 47 | + 'master_config': { |
| 48 | + 'num_instances': 1, |
| 49 | + 'machine_type_uri': 'n1-standard-1' |
| 50 | + }, |
| 51 | + 'worker_config': { |
| 52 | + 'num_instances': 2, |
| 53 | + 'machine_type_uri': 'n1-standard-1' |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + # Create the cluster. |
| 59 | + operation = cluster_client.create_cluster(project_id, region, cluster) |
| 60 | + result = operation.result() |
| 61 | + |
| 62 | + print('Cluster created successfully: {}'.format(result.cluster_name)) |
| 63 | + |
| 64 | + # Create the job client. |
| 65 | + job_client = dataproc.JobControllerClient(client_options={ |
| 66 | + 'api_endpoint': '{}-dataproc.googleapis.com:443'.format(region) |
| 67 | + }) |
| 68 | + |
| 69 | + # Create the job config. |
| 70 | + job = { |
| 71 | + 'placement': { |
| 72 | + 'cluster_name': cluster_name |
| 73 | + }, |
| 74 | + 'pyspark_job': { |
| 75 | + 'main_python_file_uri': job_file_path |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + job_response = job_client.submit_job(project_id, region, job) |
| 80 | + job_id = job_response.reference.job_id |
| 81 | + |
| 82 | + print('Submitted job \"{}\".'.format(job_id)) |
| 83 | + |
| 84 | + # Termimal states for a job. |
| 85 | + terminal_states = { |
| 86 | + dataproc.types.JobStatus.ERROR, |
| 87 | + dataproc.types.JobStatus.CANCELLED, |
| 88 | + dataproc.types.JobStatus.DONE |
| 89 | + } |
| 90 | + |
| 91 | + # Create a timeout such that the job gets cancelled if not in a |
| 92 | + # terminal state after a fixed period of time. |
| 93 | + timeout_seconds = 600 |
| 94 | + time_start = time.time() |
| 95 | + |
| 96 | + # Wait for the job to complete. |
| 97 | + while job_response.status.state not in terminal_states: |
| 98 | + if time.time() > time_start + timeout_seconds: |
| 99 | + job_client.cancel_job(project_id, region, job_id) |
| 100 | + print('Job {} timed out after threshold of {} seconds.'.format( |
| 101 | + job_id, timeout_seconds)) |
| 102 | + |
| 103 | + # Poll for job termination once a second. |
| 104 | + time.sleep(1) |
| 105 | + job_response = job_client.get_job(project_id, region, job_id) |
| 106 | + |
| 107 | + # Cloud Dataproc job output gets saved to a GCS bucket allocated to it. |
| 108 | + cluster_info = cluster_client.get_cluster( |
| 109 | + project_id, region, cluster_name) |
| 110 | + |
| 111 | + storage_client = storage.Client() |
| 112 | + bucket = storage_client.get_bucket(cluster_info.config.config_bucket) |
| 113 | + output_blob = ( |
| 114 | + 'google-cloud-dataproc-metainfo/{}/jobs/{}/driveroutput.000000000' |
| 115 | + .format(cluster_info.cluster_uuid, job_id)) |
| 116 | + output = bucket.blob(output_blob).download_as_string() |
| 117 | + |
| 118 | + print('Job {} finished with state {}:\n{}'.format( |
| 119 | + job_id, |
| 120 | + job_response.status.State.Name(job_response.status.state), |
| 121 | + output)) |
| 122 | + |
| 123 | + # Delete the cluster once the job has terminated. |
| 124 | + operation = cluster_client.delete_cluster(project_id, region, cluster_name) |
| 125 | + operation.result() |
| 126 | + |
| 127 | + print('Cluster {} successfully deleted.'.format(cluster_name)) |
| 128 | + # [END dataproc_quickstart] |
0 commit comments