Skip to content

Commit b0a8173

Browse files
tswastJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Add veneer samples for BigQuery. [(googleapis#444)](GoogleCloudPlatform/python-docs-samples#444)
Put them in their own directory, since they duplicate the content from the existing samples that use the generated libraries.
0 parents  commit b0a8173

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed

samples/snippets/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# BigQuery Google Cloud Client Library Samples
2+
3+
<!-- auto-doc-link -->
4+
<!-- end-auto-doc-link -->
5+

samples/snippets/async_query.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Command-line application to perform asynchronous queries in BigQuery.
18+
19+
For more information, see the README.md under /bigquery.
20+
21+
Example invocation:
22+
$ python async_query.py \
23+
'SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus'
24+
"""
25+
26+
import argparse
27+
import time
28+
import uuid
29+
30+
from gcloud import bigquery
31+
32+
33+
def async_query(query):
34+
client = bigquery.Client()
35+
query_job = client.run_async_query(str(uuid.uuid4()), query)
36+
query_job.use_legacy_sql = False
37+
query_job.begin()
38+
39+
wait_for_job(query_job)
40+
41+
# Manually construct the QueryResults.
42+
# TODO: The client library will provide a helper method that does this.
43+
# https://github.com/GoogleCloudPlatform/gcloud-python/issues/2083
44+
query_results = bigquery.query.QueryResults('', client)
45+
query_results._properties['jobReference'] = {
46+
'jobId': query_job.name,
47+
'projectId': query_job.project
48+
}
49+
50+
# Drain the query results by requesting a page at a time.
51+
page_token = None
52+
53+
while True:
54+
rows, total_rows, page_token = query_results.fetch_data(
55+
max_results=10,
56+
page_token=page_token)
57+
58+
for row in rows:
59+
print(row)
60+
61+
if not page_token:
62+
break
63+
64+
65+
def wait_for_job(job):
66+
while True:
67+
job.reload() # Refreshes the state via a GET request.
68+
if job.state == 'DONE':
69+
if job.error_result:
70+
raise RuntimeError(job.error_result)
71+
return
72+
time.sleep(1)
73+
74+
75+
if __name__ == '__main__':
76+
parser = argparse.ArgumentParser(
77+
description=__doc__,
78+
formatter_class=argparse.RawDescriptionHelpFormatter)
79+
parser.add_argument('query', help='BigQuery SQL Query.')
80+
81+
args = parser.parse_args()
82+
83+
async_query(args.query)

samples/snippets/async_query_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
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+
from async_query import async_query
16+
17+
18+
def test_async_query(cloud_config, capsys):
19+
query = (
20+
'SELECT corpus FROM `publicdata.samples.shakespeare` '
21+
'GROUP BY corpus;')
22+
23+
async_query(query)
24+
25+
out, _ = capsys.readouterr()
26+
27+
assert 'romeoandjuliet' in out

samples/snippets/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gcloud==0.18.1

samples/snippets/sync_query.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 2016 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Command-line application to perform synchronous queries in BigQuery.
18+
19+
For more information, see the README.md under /bigquery.
20+
21+
Example invocation:
22+
$ python sync_query.py \
23+
'SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus'
24+
"""
25+
26+
import argparse
27+
28+
# [START sync_query]
29+
from gcloud import bigquery
30+
31+
32+
def sync_query(query):
33+
client = bigquery.Client()
34+
query_results = client.run_sync_query(query)
35+
36+
# Use standard SQL syntax for queries.
37+
# See: https://cloud.google.com/bigquery/sql-reference/
38+
query_results.use_legacy_sql = False
39+
40+
query_results.run()
41+
42+
# Drain the query results by requesting a page at a time.
43+
page_token = None
44+
45+
while True:
46+
rows, total_rows, page_token = query_results.fetch_data(
47+
max_results=10,
48+
page_token=page_token)
49+
50+
for row in rows:
51+
print(row)
52+
53+
if not page_token:
54+
break
55+
# [END sync_query]
56+
57+
58+
if __name__ == '__main__':
59+
parser = argparse.ArgumentParser(
60+
description=__doc__,
61+
formatter_class=argparse.RawDescriptionHelpFormatter)
62+
parser.add_argument('query', help='BigQuery SQL Query.')
63+
64+
args = parser.parse_args()
65+
66+
sync_query(args.query)

samples/snippets/sync_query_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
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+
from sync_query import sync_query
16+
17+
18+
def test_sync_query(cloud_config, capsys):
19+
query = (
20+
'SELECT corpus FROM `publicdata.samples.shakespeare` '
21+
'GROUP BY corpus;')
22+
23+
sync_query(query)
24+
25+
out, _ = capsys.readouterr()
26+
27+
assert 'romeoandjuliet' in out

0 commit comments

Comments
 (0)