Skip to content

Commit 961f326

Browse files
tswastJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
BigQuery parameterized query sample [(googleapis#699)](GoogleCloudPlatform/python-docs-samples#699)
Available in latest version of google-cloud-bigquery (0.22.0), so upgrades the requirement, too.
1 parent f45c6bf commit 961f326

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

samples/snippets/sync_query_params.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2016 Google Inc.
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 app to perform synchronous queries with parameters in BigQuery.
18+
19+
For more information, see the README.md under /bigquery.
20+
21+
Example invocation:
22+
$ python sync_query_params.py --use-named-params 'romeoandjuliet' 100
23+
$ python sync_query_params.py --use-positional-params 'romeoandjuliet' 100
24+
"""
25+
26+
import argparse
27+
28+
from google.cloud import bigquery
29+
30+
31+
def print_results(query_results):
32+
"""Print the query results by requesting a page at a time."""
33+
page_token = None
34+
35+
while True:
36+
rows, total_rows, page_token = query_results.fetch_data(
37+
max_results=10,
38+
page_token=page_token)
39+
40+
for row in rows:
41+
print(row)
42+
43+
if not page_token:
44+
break
45+
46+
47+
def sync_query_positional_params(corpus, min_word_count):
48+
client = bigquery.Client()
49+
query_results = client.run_sync_query(
50+
"""SELECT word, word_count
51+
FROM `bigquery-public-data.samples.shakespeare`
52+
WHERE corpus = ?
53+
AND word_count >= ?
54+
ORDER BY word_count DESC;
55+
""",
56+
query_parameters=(
57+
bigquery.ScalarQueryParameter(
58+
# Set the name to None to use positional parameters (? symbol
59+
# in the query). Note that you cannot mix named and positional
60+
# parameters.
61+
None,
62+
'STRING',
63+
corpus),
64+
bigquery.ScalarQueryParameter(None, 'INT64', min_word_count)))
65+
66+
# Only standard SQL syntax supports parameters in queries.
67+
# See: https://cloud.google.com/bigquery/sql-reference/
68+
query_results.use_legacy_sql = False
69+
query_results.run()
70+
print_results(query_results)
71+
72+
73+
def sync_query_named_params(corpus, min_word_count):
74+
client = bigquery.Client()
75+
query_results = client.run_sync_query(
76+
"""SELECT word, word_count
77+
FROM `bigquery-public-data.samples.shakespeare`
78+
WHERE corpus = @corpus
79+
AND word_count >= @min_word_count
80+
ORDER BY word_count DESC;
81+
""",
82+
query_parameters=(
83+
bigquery.ScalarQueryParameter('corpus', 'STRING', corpus),
84+
bigquery.ScalarQueryParameter(
85+
'min_word_count',
86+
'INT64',
87+
min_word_count)))
88+
query_results.use_legacy_sql = False
89+
query_results.run()
90+
print_results(query_results)
91+
92+
93+
if __name__ == '__main__':
94+
parser = argparse.ArgumentParser(
95+
description=__doc__,
96+
formatter_class=argparse.RawDescriptionHelpFormatter)
97+
parser.add_argument(
98+
'corpus',
99+
help='Corpus to search from Shakespeare dataset.')
100+
parser.add_argument(
101+
'min_word_count',
102+
help='Minimum count of words to query.',
103+
type=int)
104+
105+
params_type_parser = parser.add_mutually_exclusive_group(required=False)
106+
params_type_parser.add_argument(
107+
'--use-named-params',
108+
dest='use_named_params',
109+
action='store_true')
110+
params_type_parser.add_argument(
111+
'--use-positional-params',
112+
dest='use_named_params',
113+
action='store_false')
114+
parser.set_defaults(use_named_params=False)
115+
args = parser.parse_args()
116+
117+
if args.use_named_params:
118+
sync_query_named_params(args.corpus, args.min_word_count)
119+
else:
120+
sync_query_positional_params(args.corpus, args.min_word_count)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2016 Google Inc.
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+
import sync_query_params
16+
17+
18+
def test_sync_query_named_params(cloud_config, capsys):
19+
sync_query_params.sync_query_named_params(
20+
corpus='romeoandjuliet',
21+
min_word_count=100)
22+
out, _ = capsys.readouterr()
23+
assert 'love' in out
24+
25+
26+
def test_sync_query_positional_params(cloud_config, capsys):
27+
sync_query_params.sync_query_positional_params(
28+
corpus='romeoandjuliet',
29+
min_word_count=100)
30+
out, _ = capsys.readouterr()
31+
assert 'love' in out

0 commit comments

Comments
 (0)