Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 541eb98

Browse filesBrowse files
tswastJon Wayne Parrott
authored andcommitted
Add veneer samples for BigQuery. (GoogleCloudPlatform#444)
Put them in their own directory, since they duplicate the content from the existing samples that use the generated libraries.
1 parent 13b10d4 commit 541eb98
Copy full SHA for 541eb98

File tree

Expand file treeCollapse file tree

7 files changed

+211
-2
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+211
-2
lines changed

‎bigquery/cloud-client/README.md

Copy file name to clipboard
+5Lines 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+

‎bigquery/cloud-client/async_query.py

Copy file name to clipboard
+83Lines 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)
+27Lines 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
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gcloud==0.18.1

‎bigquery/cloud-client/sync_query.py

Copy file name to clipboard
+66Lines 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)
+27Lines 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

‎requirements-dev.txt

Copy file name to clipboardExpand all lines: requirements-dev.txt
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Flask-SQLAlchemy==2.1
1010
Flask==0.11.1
1111
fluent-logger==0.4.3
1212
funcsigs==1.0.2
13-
gcloud==0.17.0
14-
gcloud[grpc]==0.17.0
13+
gcloud==0.18.1
14+
gcloud[grpc]==0.18.1
1515
google-api-python-client==1.5.1
1616
grpc-google-cloud-speech-v1beta1==1.0.1
1717
grpcio==1.0.0rc1

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.