-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Changes from all commits
Commits
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
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. | ||
|
||
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) |
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,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() |
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?