Skip to content

Commit e9f8cb5

Browse files
author
Jerjou Cheng
committed
Add quick start sample.
1 parent 2fe2289 commit e9f8cb5

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
14+
import re
15+
import unittest
16+
17+
from bigquery.samples import list_datasets_projects
18+
19+
import tests
20+
21+
22+
class TestListDatasetsProjects(tests.CloudBaseTest):
23+
24+
def test_main(self):
25+
with tests.capture_stdout() as mock_stdout:
26+
list_datasets_projects.main(self.constants['projectId'])
27+
stdout = mock_stdout.getvalue()
28+
self.assertRegexpMatches(stdout, re.compile(
29+
r'Project list:.*bigquery#projectList.*projects', re.DOTALL))
30+
self.assertRegexpMatches(stdout, re.compile(
31+
r'Dataset list:.*datasets.*datasetId', re.DOTALL))
32+
33+
34+
if __name__ == '__main__':
35+
unittest.main()

0 commit comments

Comments
 (0)