Skip to content

Add quick start sample. #59

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 1 commit into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions bigquery/samples/list_datasets_projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-
# Copyright 2015, Google, Inc.
# 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.
#
"""Command-line skeleton application for BigQuery API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any configuration/setup the user needs to do first? Turn on any APIs? gcloud auth?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some doc to clarify.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a note that gcloud auth is the easiest way to auth locally?

This is the sample for this page:

https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects

In order to run it, your environment must be setup with authentication
information [1]. If you're running it in your local development environment and
you have the Google Cloud SDK [2] installed, you can do this easily by running:

$ gcloud auth login

Usage:

$ python list_datasets_projects.py <project-id>

where <project-id> is the id of the developers console [3] project you'd like
to list the bigquery datasets and projects for.

[1] https://developers.google.com/identity/protocols/application-default-credentials#howtheywork
[2] https://cloud.google.com/sdk/
[3] https://console.developers.google.com
""" # NOQA

import argparse
from pprint import pprint

from urllib2 import HTTPError

from apiclient import discovery

from oauth2client.client import GoogleCredentials


# [START list_datasets]
def list_datasets(service, project):
try:
datasets = service.datasets()
list_reply = datasets.list(projectId=project).execute()
print('Dataset list:')
pprint(list_reply)

except HTTPError as err:
print('Error in list_datasets: %s' % err.content)
# [END list_datasets]


# [START list_projects]
def list_projects(service):
try:
# Start training on a data set
projects = service.projects()
list_reply = projects.list().execute()

print('Project list:')
pprint(list_reply)

except HTTPError as err:
print('Error in list_projects: %s' % err.content)
# [END list_projects]


def main(project_id):
credentials = GoogleCredentials.get_application_default()
# Construct the service object for the interacting with the BigQuery API.
service = discovery.build('bigquery', 'v2', credentials=credentials)

list_datasets(service, project_id)
list_projects(service)


# For more information on the BigQuery API you can visit:
#
# https://developers.google.com/bigquery/docs/overview
#
# For more information on the BigQuery API Python library surface you
# can visit:
#
# https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/python/latest/
#
# For information on the Python Client Library visit:
#
# https://developers.google.com/api-client-library/python/start/get_started
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Lists BigQuery datasets and projects.')
parser.add_argument('project_id', help='the project id to list.')

args = parser.parse_args()

main(args.project_id)
35 changes: 35 additions & 0 deletions bigquery/tests/test_list_datasets_projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2015, Google, Inc.
# 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 re
import unittest

from bigquery.samples import list_datasets_projects

import tests


class TestListDatasetsProjects(tests.CloudBaseTest):

def test_main(self):
with tests.capture_stdout() as mock_stdout:
list_datasets_projects.main(self.constants['projectId'])
stdout = mock_stdout.getvalue()
self.assertRegexpMatches(stdout, re.compile(
r'Project list:.*bigquery#projectList.*projects', re.DOTALL))
self.assertRegexpMatches(stdout, re.compile(
r'Dataset list:.*datasets.*datasetId', re.DOTALL))


if __name__ == '__main__':
unittest.main()