Skip to content

Commit 4361e74

Browse files
committed
BigQuery samples using query parameters.
This query syntax is supported in the not-yet-release version of google-cloud-python.
1 parent b763fe6 commit 4361e74

File tree

4 files changed

+266
-0
lines changed

4 files changed

+266
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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_params.py 'romeoandjuliet' 100
23+
"""
24+
25+
import argparse
26+
import time
27+
import uuid
28+
29+
from google.cloud import bigquery
30+
31+
32+
def wait_for_job(job):
33+
while True:
34+
job.reload() # Refreshes the state via a GET request.
35+
if job.state == 'DONE':
36+
if job.error_result:
37+
raise RuntimeError(job.errors)
38+
return
39+
time.sleep(1)
40+
41+
42+
def async_query_params(corpus, min_word_count):
43+
client = bigquery.Client()
44+
query_job = client.run_async_query(
45+
str(uuid.uuid4()),
46+
"""SELECT word, word_count
47+
FROM `bigquery-public-data.samples.shakespeare`
48+
WHERE corpus = ?
49+
AND word_count >= ?
50+
ORDER BY word_count DESC;
51+
""",
52+
query_parameters=(
53+
bigquery.ScalarQueryParameter(
54+
# Set the name to None to use positional parameters (? symbol
55+
# in the query). Note that you cannot mix named and positional
56+
# parameters.
57+
None,
58+
'STRING',
59+
corpus),
60+
bigquery.ScalarQueryParameter(None, 'INT64', min_word_count)))
61+
62+
# Only standard SQL syntax supports parameters in queries.
63+
# See: https://cloud.google.com/bigquery/sql-reference/
64+
query_job.use_legacy_sql = False
65+
query_job.begin()
66+
wait_for_job(query_job)
67+
print('Positional parameter query completed')
68+
69+
query_job = client.run_async_query(
70+
str(uuid.uuid4()),
71+
"""SELECT word, word_count
72+
FROM `bigquery-public-data.samples.shakespeare`
73+
WHERE corpus = @corpus
74+
AND word_count >= @min_word_count
75+
ORDER BY word_count DESC;
76+
""",
77+
query_parameters=(
78+
bigquery.ScalarQueryParameter('corpus', 'STRING', corpus),
79+
bigquery.ScalarQueryParameter(
80+
'min_word_count',
81+
'INT64',
82+
min_word_count)))
83+
query_job.use_legacy_sql = False
84+
query_job.begin()
85+
wait_for_job(query_job)
86+
print('Named parameter query completed')
87+
88+
# Print the query results by requesting a page at a time.
89+
query_results = query_job.results()
90+
page_token = None
91+
92+
while True:
93+
rows, total_rows, page_token = query_results.fetch_data(
94+
max_results=10,
95+
page_token=page_token)
96+
97+
for row in rows:
98+
print(row)
99+
100+
if not page_token:
101+
break
102+
103+
104+
if __name__ == '__main__':
105+
parser = argparse.ArgumentParser(
106+
description=__doc__,
107+
formatter_class=argparse.RawDescriptionHelpFormatter)
108+
parser.add_argument(
109+
'corpus',
110+
help='Corpus to search from Shakespeare dataset.')
111+
parser.add_argument(
112+
'min_word_count',
113+
help='Minimum count of words to query.',
114+
type=int)
115+
116+
args = parser.parse_args()
117+
118+
async_query_params(args.corpus, args.min_word_count)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
sync_query('romeoandjuliet', 100)
20+
21+
out, _ = capsys.readouterr()
22+
23+
assert 'Positional parameter query completed' in out
24+
assert 'Named parameter query completed' in out
25+
assert 'love' in out
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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_params.py 'romeoandjuliet' 100
23+
"""
24+
25+
import argparse
26+
27+
# [START sync_query_params]
28+
from google.cloud import bigquery
29+
30+
31+
def sync_query_params(corpus, min_word_count):
32+
client = bigquery.Client()
33+
query_results = client.run_sync_query(
34+
"""SELECT word, word_count
35+
FROM `bigquery-public-data.samples.shakespeare`
36+
WHERE corpus = ?
37+
AND word_count >= ?
38+
ORDER BY word_count DESC;
39+
""",
40+
query_parameters=(
41+
bigquery.ScalarQueryParameter(None, 'STRING', corpus),
42+
bigquery.ScalarQueryParameter(None, 'INT64', min_word_count)))
43+
44+
# Only standard SQL syntax supports parameters in queries.
45+
# See: https://cloud.google.com/bigquery/sql-reference/
46+
query_results.use_legacy_sql = False
47+
query_results.run()
48+
print('Positional parameter query completed')
49+
50+
query_results = client.run_sync_query(
51+
"""SELECT word, word_count
52+
FROM `bigquery-public-data.samples.shakespeare`
53+
WHERE corpus = @corpus
54+
AND word_count >= @min_word_count
55+
ORDER BY word_count DESC;
56+
""",
57+
query_parameters=(
58+
bigquery.ScalarQueryParameter('corpus', 'STRING', corpus),
59+
bigquery.ScalarQueryParameter(
60+
'min_word_count',
61+
'INT64',
62+
min_word_count)))
63+
query_results.use_legacy_sql = False
64+
query_results.run()
65+
print('Named parameter query completed')
66+
67+
# Print the query results by requesting a page at a time.
68+
page_token = None
69+
70+
while True:
71+
rows, total_rows, page_token = query_results.fetch_data(
72+
max_results=10,
73+
page_token=page_token)
74+
75+
for row in rows:
76+
print(row)
77+
78+
if not page_token:
79+
break
80+
81+
# [END sync_query_params]
82+
83+
84+
if __name__ == '__main__':
85+
parser = argparse.ArgumentParser(
86+
description=__doc__,
87+
formatter_class=argparse.RawDescriptionHelpFormatter)
88+
parser.add_argument(
89+
'corpus',
90+
help='Corpus to search from Shakespeare dataset.')
91+
parser.add_argument(
92+
'min_word_count',
93+
help='Minimum count of words to query.',
94+
type=int)
95+
96+
args = parser.parse_args()
97+
98+
sync_query_params(args.corpus, args.min_word_count)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
sync_query('romeoandjuliet', 100)
20+
21+
out, _ = capsys.readouterr()
22+
23+
assert 'Positional parameter query completed' in out
24+
assert 'Named parameter query completed' in out
25+
assert 'love' in out

0 commit comments

Comments
 (0)