Skip to content

Commit 98dd920

Browse files
committed
Merge pull request #78 from GoogleCloudPlatform/getting-started
Move bigquery 'getting started' sample into github.
2 parents 89880d7 + ab2c5e0 commit 98dd920

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

bigquery/samples/getting_started.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2015, Google, Inc.
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""Sample for making BigQuery queries using the python sdk.
17+
18+
This is a command-line script that queries a public shakespeare dataset, and
19+
displays the 10 of Shakespeare's works with the greatest number of distinct
20+
words.
21+
"""
22+
# [START all]
23+
from apiclient.discovery import build
24+
from apiclient.errors import HttpError
25+
26+
from oauth2client.client import GoogleCredentials
27+
28+
29+
def main(project_id):
30+
# [START build_service]
31+
# Grab the application's default credentials from the environment.
32+
credentials = GoogleCredentials.get_application_default()
33+
# Construct the service object for interacting with the BigQuery API.
34+
bigquery_service = build('bigquery', 'v2', credentials=credentials)
35+
# [END build_service]
36+
37+
try:
38+
# [START run_query]
39+
query_request = bigquery_service.jobs()
40+
query_data = {
41+
'query': ('SELECT TOP(corpus, 10) as title, '
42+
'COUNT(*) as unique_words '
43+
'FROM [publicdata:samples.shakespeare];')
44+
}
45+
46+
query_response = query_request.query(
47+
projectId=project_id,
48+
body=query_data).execute()
49+
# [END run_query]
50+
51+
# [START print_results]
52+
print('Query Results:')
53+
for row in query_response['rows']:
54+
print('\t'.join(field['v'] for field in row['f']))
55+
# [END print_results]
56+
57+
except HttpError as err:
58+
print('Error: {}'.format(err.content))
59+
raise err
60+
61+
62+
if __name__ == '__main__':
63+
# The id of the project to run queries under.
64+
project_id = input("Enter the project ID: ")
65+
main(project_id)
66+
# [END all]

bigquery/samples/list_datasets_projects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def list_projects(service):
7575

7676
def main(project_id):
7777
credentials = GoogleCredentials.get_application_default()
78-
# Construct the service object for the interacting with the BigQuery API.
78+
# Construct the service object for interacting with the BigQuery API.
7979
service = discovery.build('bigquery', 'v2', credentials=credentials)
8080

8181
list_datasets(service, project_id)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 getting_started
18+
19+
import tests
20+
21+
22+
class TestGettingStarted(tests.CloudBaseTest):
23+
def test_main(self):
24+
with tests.capture_stdout() as mock_stdout:
25+
getting_started.main(self.constants['projectId'])
26+
stdout = mock_stdout.getvalue()
27+
self.assertRegexpMatches(stdout, re.compile(
28+
r'Query Results:.hamlet', re.DOTALL))
29+
30+
31+
if __name__ == '__main__':
32+
unittest.main()

0 commit comments

Comments
 (0)