|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2015, Google, Inc. |
| 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 | +"""Command-line skeleton application for BigQuery API. |
| 16 | +
|
| 17 | +This is the sample for this page: |
| 18 | +
|
| 19 | + https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects |
| 20 | +
|
| 21 | +In order to run it, your environment must be setup with authentication |
| 22 | +information [1]. If you're running it in your local development environment and |
| 23 | +you have the Google Cloud SDK [2] installed, you can do this easily by running: |
| 24 | +
|
| 25 | + $ gcloud auth login |
| 26 | +
|
| 27 | +Usage: |
| 28 | +
|
| 29 | + $ python list_datasets_projects.py <project-id> |
| 30 | +
|
| 31 | +where <project-id> is the id of the developers console [3] project you'd like |
| 32 | +to list the bigquery datasets and projects for. |
| 33 | +
|
| 34 | +[1] https://developers.google.com/identity/protocols/application-default-credentials#howtheywork |
| 35 | +[2] https://cloud.google.com/sdk/ |
| 36 | +[3] https://console.developers.google.com |
| 37 | +""" # NOQA |
| 38 | + |
| 39 | +import argparse |
| 40 | +from pprint import pprint |
| 41 | + |
| 42 | +from urllib2 import HTTPError |
| 43 | + |
| 44 | +from apiclient import discovery |
| 45 | + |
| 46 | +from oauth2client.client import GoogleCredentials |
| 47 | + |
| 48 | + |
| 49 | +# [START list_datasets] |
| 50 | +def list_datasets(service, project): |
| 51 | + try: |
| 52 | + datasets = service.datasets() |
| 53 | + list_reply = datasets.list(projectId=project).execute() |
| 54 | + print('Dataset list:') |
| 55 | + pprint(list_reply) |
| 56 | + |
| 57 | + except HTTPError as err: |
| 58 | + print('Error in list_datasets: %s' % err.content) |
| 59 | +# [END list_datasets] |
| 60 | + |
| 61 | + |
| 62 | +# [START list_projects] |
| 63 | +def list_projects(service): |
| 64 | + try: |
| 65 | + # Start training on a data set |
| 66 | + projects = service.projects() |
| 67 | + list_reply = projects.list().execute() |
| 68 | + |
| 69 | + print('Project list:') |
| 70 | + pprint(list_reply) |
| 71 | + |
| 72 | + except HTTPError as err: |
| 73 | + print('Error in list_projects: %s' % err.content) |
| 74 | +# [END list_projects] |
| 75 | + |
| 76 | + |
| 77 | +def main(project_id): |
| 78 | + credentials = GoogleCredentials.get_application_default() |
| 79 | + # Construct the service object for the interacting with the BigQuery API. |
| 80 | + service = discovery.build('bigquery', 'v2', credentials=credentials) |
| 81 | + |
| 82 | + list_datasets(service, project_id) |
| 83 | + list_projects(service) |
| 84 | + |
| 85 | + |
| 86 | +# For more information on the BigQuery API you can visit: |
| 87 | +# |
| 88 | +# https://developers.google.com/bigquery/docs/overview |
| 89 | +# |
| 90 | +# For more information on the BigQuery API Python library surface you |
| 91 | +# can visit: |
| 92 | +# |
| 93 | +# https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/python/latest/ |
| 94 | +# |
| 95 | +# For information on the Python Client Library visit: |
| 96 | +# |
| 97 | +# https://developers.google.com/api-client-library/python/start/get_started |
| 98 | +if __name__ == '__main__': |
| 99 | + parser = argparse.ArgumentParser( |
| 100 | + description='Lists BigQuery datasets and projects.') |
| 101 | + parser.add_argument('project_id', help='the project id to list.') |
| 102 | + |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + main(args.project_id) |
0 commit comments