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 704c564

Browse filesBrowse files
tswastJon Wayne Parrott
authored andcommitted
BigQuery parameterized query sample (GoogleCloudPlatform#699)
Available in latest version of google-cloud-bigquery (0.22.0), so upgrades the requirement, too.
1 parent c4a1411 commit 704c564
Copy full SHA for 704c564

File tree

Expand file treeCollapse file tree

3 files changed

+152
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+152
-1
lines changed
+120Lines 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)
+31Lines 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

‎bigquery/dml/requirements.txt

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
flake8==3.2.0
2-
google-cloud-bigquery==0.21.0
2+
google-cloud-bigquery==0.22.0
33
PyMySQL==0.7.9
44
six==1.10.0
55
SQLAlchemy==1.1.4

0 commit comments

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