-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat: dataproc quickstart sample added and create_cluster updated #2629
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
19 commits
Select commit
Hold shift + click to select a range
d4ac869
Adding quickstart sample
bradmiro 225fb34
Added new quickstart sample and updated create_cluster sample
bradmiro 3a9874b
Fix to create_cluster.py
bradmiro 194ba5b
deleted dataproc quickstart files not under dataproc/quickstart/
bradmiro 19a4997
Added quickstart test
bradmiro c5afcbc
Linting and formatting fixes
bradmiro 3233dc2
Revert "Linting and formatting fixes"
bradmiro 6c0dcc7
Added bucket cleanup to quickstart test
bradmiro 7588e6e
Changes to samples and tests
bradmiro cec52d4
Linting fixes
bradmiro 4fed5e8
Merge branch 'master' into dataproc-samples-new
bradmiro d25e883
Merge branch 'master' into dataproc-samples-new
bradmiro 5ca25ee
Merge branch 'master' into dataproc-samples-new
bradmiro bb844e4
Merge branch 'master' into dataproc-samples-new
bradmiro c4f1f73
Merge branch 'master' into dataproc-samples-new
leahecole 0623f7c
Removed todos in favor of clearer docstring
bradmiro 0d16a8f
Fixed lint error
bradmiro 0127776
Merge branch 'master' into dataproc-samples-new
bradmiro 95d9491
Merge branch 'master' into dataproc-samples-new
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
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
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,128 @@ | ||
#!/usr/bin/env python | ||
|
||
# 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. | ||
|
||
# [START dataproc_quickstart] | ||
import time | ||
|
||
from google.cloud import dataproc_v1 as dataproc | ||
from google.cloud import storage | ||
|
||
|
||
def quickstart(project_id, region, cluster_name, job_file_path): | ||
"""This quickstart sample walks a user through creating a Cloud Dataproc | ||
cluster, submitting a PySpark job from Google Cloud Storage to the | ||
cluster, reading the output of the job and deleting the cluster, all | ||
using the Python client library. | ||
|
||
Args: | ||
project_id (string): Project to use for creating resources. | ||
region (string): Region where the resources should live. | ||
cluster_name (string): Name to use for creating a cluster. | ||
job_file_path (string): Job in GCS to execute against the cluster. | ||
""" | ||
|
||
# Create the cluster client. | ||
cluster_client = dataproc.ClusterControllerClient(client_options={ | ||
'api_endpoint': '{}-dataproc.googleapis.com:443'.format(region) | ||
}) | ||
|
||
# Create the cluster config. | ||
cluster = { | ||
'project_id': project_id, | ||
'cluster_name': cluster_name, | ||
'config': { | ||
'master_config': { | ||
'num_instances': 1, | ||
'machine_type_uri': 'n1-standard-1' | ||
}, | ||
'worker_config': { | ||
'num_instances': 2, | ||
'machine_type_uri': 'n1-standard-1' | ||
} | ||
} | ||
} | ||
|
||
# Create the cluster. | ||
operation = cluster_client.create_cluster(project_id, region, cluster) | ||
result = operation.result() | ||
|
||
print('Cluster created successfully: {}'.format(result.cluster_name)) | ||
|
||
# Create the job client. | ||
job_client = dataproc.JobControllerClient(client_options={ | ||
'api_endpoint': '{}-dataproc.googleapis.com:443'.format(region) | ||
}) | ||
|
||
# Create the job config. | ||
job = { | ||
'placement': { | ||
'cluster_name': cluster_name | ||
}, | ||
'pyspark_job': { | ||
'main_python_file_uri': job_file_path | ||
} | ||
} | ||
|
||
job_response = job_client.submit_job(project_id, region, job) | ||
job_id = job_response.reference.job_id | ||
|
||
print('Submitted job \"{}\".'.format(job_id)) | ||
|
||
# Termimal states for a job. | ||
terminal_states = { | ||
dataproc.types.JobStatus.ERROR, | ||
dataproc.types.JobStatus.CANCELLED, | ||
dataproc.types.JobStatus.DONE | ||
} | ||
|
||
# Create a timeout such that the job gets cancelled if not in a | ||
# terminal state after a fixed period of time. | ||
timeout_seconds = 600 | ||
time_start = time.time() | ||
|
||
# Wait for the job to complete. | ||
while job_response.status.state not in terminal_states: | ||
if time.time() > time_start + timeout_seconds: | ||
job_client.cancel_job(project_id, region, job_id) | ||
print('Job {} timed out after threshold of {} seconds.'.format( | ||
job_id, timeout_seconds)) | ||
|
||
# Poll for job termination once a second. | ||
time.sleep(1) | ||
job_response = job_client.get_job(project_id, region, job_id) | ||
|
||
# Cloud Dataproc job output gets saved to a GCS bucket allocated to it. | ||
cluster_info = cluster_client.get_cluster( | ||
project_id, region, cluster_name) | ||
|
||
storage_client = storage.Client() | ||
bucket = storage_client.get_bucket(cluster_info.config.config_bucket) | ||
output_blob = ( | ||
'google-cloud-dataproc-metainfo/{}/jobs/{}/driveroutput.000000000' | ||
.format(cluster_info.cluster_uuid, job_id)) | ||
output = bucket.blob(output_blob).download_as_string() | ||
|
||
print('Job {} finished with state {}:\n{}'.format( | ||
job_id, | ||
job_response.status.State.Name(job_response.status.state), | ||
output)) | ||
|
||
# Delete the cluster once the job has terminated. | ||
operation = cluster_client.delete_cluster(project_id, region, cluster_name) | ||
operation.result() | ||
|
||
print('Cluster {} successfully deleted.'.format(cluster_name)) | ||
bradmiro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# [END dataproc_quickstart] |
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,70 @@ | ||
# 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 os | ||
import uuid | ||
import pytest | ||
|
||
from google.cloud import dataproc_v1 as dataproc | ||
from google.cloud import storage | ||
|
||
import quickstart | ||
|
||
|
||
PROJECT_ID = os.environ['GCLOUD_PROJECT'] | ||
REGION = 'us-central1' | ||
CLUSTER_NAME = 'py-qs-test-{}'.format(str(uuid.uuid4())) | ||
STAGING_BUCKET = 'py-dataproc-qs-bucket-{}'.format(str(uuid.uuid4())) | ||
JOB_FILE_NAME = 'sum.py' | ||
JOB_FILE_PATH = 'gs://{}/{}'.format(STAGING_BUCKET, JOB_FILE_NAME) | ||
SORT_CODE = ( | ||
"import pyspark\n" | ||
"sc = pyspark.SparkContext()\n" | ||
"rdd = sc.parallelize((1,2,3,4,5))\n" | ||
"sum = rdd.reduce(lambda x, y: x + y)\n" | ||
) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def setup_teardown(): | ||
storage_client = storage.Client() | ||
bucket = storage_client.create_bucket(STAGING_BUCKET) | ||
blob = bucket.blob(JOB_FILE_NAME) | ||
blob.upload_from_string(SORT_CODE) | ||
|
||
yield | ||
|
||
cluster_client = dataproc.ClusterControllerClient(client_options={ | ||
'api_endpoint': '{}-dataproc.googleapis.com:443'.format(REGION) | ||
}) | ||
|
||
# The quickstart sample deletes the cluster, but if the test fails | ||
# before cluster deletion occurs, it can be manually deleted here. | ||
clusters = cluster_client.list_clusters(PROJECT_ID, REGION) | ||
|
||
for cluster in clusters: | ||
if cluster.cluster_name == CLUSTER_NAME: | ||
cluster_client.delete_cluster(PROJECT_ID, REGION, CLUSTER_NAME) | ||
|
||
blob.delete() | ||
|
||
|
||
def test_quickstart(capsys): | ||
quickstart.quickstart(PROJECT_ID, REGION, CLUSTER_NAME, JOB_FILE_PATH) | ||
|
||
out, _ = capsys.readouterr() | ||
assert 'Cluster created successfully' in out | ||
assert 'Submitted job' in out | ||
assert 'finished with state DONE:' in out | ||
assert 'successfully deleted' in out |
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.