|
| 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] |
0 commit comments