forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_params.py
More file actions
225 lines (196 loc) · 7.58 KB
/
query_params.py
File metadata and controls
225 lines (196 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Command-line app to perform queries with parameters in BigQuery.
For more information, see the README.rst.
Example invocation:
$ python query_params.py named 'romeoandjuliet' 100
$ python query_params.py positional 'romeoandjuliet' 100
"""
import argparse
import datetime
from google.cloud import bigquery
import pytz
def query_positional_params(corpus, min_word_count):
client = bigquery.Client()
query = """
SELECT word, word_count
FROM `bigquery-public-data.samples.shakespeare`
WHERE corpus = ?
AND word_count >= ?
ORDER BY word_count DESC;
"""
# Set the name to None to use positional parameters (? symbol in the
# query). Note that you cannot mix named and positional parameters.
# See: https://cloud.google.com/bigquery/docs/parameterized-queries/
query_params = [
bigquery.ScalarQueryParameter(None, 'STRING', corpus),
bigquery.ScalarQueryParameter(None, 'INT64', min_word_count)
]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(query, job_config=job_config)
query_job.result() # Wait for job to complete
# Print the results.
destination_table_ref = query_job.destination
table = client.get_table(destination_table_ref)
for row in client.list_rows(table):
print(row)
def query_named_params(corpus, min_word_count):
client = bigquery.Client()
query = """
SELECT word, word_count
FROM `bigquery-public-data.samples.shakespeare`
WHERE corpus = @corpus
AND word_count >= @min_word_count
ORDER BY word_count DESC;
"""
query_params = [
bigquery.ScalarQueryParameter('corpus', 'STRING', corpus),
bigquery.ScalarQueryParameter(
'min_word_count', 'INT64', min_word_count)
]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(query, job_config=job_config)
query_job.result() # Wait for job to complete
# Print the results.
destination_table_ref = query_job.destination
table = client.get_table(destination_table_ref)
for row in client.list_rows(table):
print(row)
def query_array_params(gender, states):
client = bigquery.Client()
query = """
SELECT name, sum(number) as count
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE gender = @gender
AND state IN UNNEST(@states)
GROUP BY name
ORDER BY count DESC
LIMIT 10;
"""
query_params = [
bigquery.ScalarQueryParameter('gender', 'STRING', gender),
bigquery.ArrayQueryParameter('states', 'STRING', states)
]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(query, job_config=job_config)
query_job.result() # Wait for job to complete
# Print the results.
destination_table_ref = query_job.destination
table = client.get_table(destination_table_ref)
for row in client.list_rows(table):
print(row)
def query_timestamp_params(year, month, day, hour, minute):
client = bigquery.Client()
query = 'SELECT TIMESTAMP_ADD(@ts_value, INTERVAL 1 HOUR);'
query_params = [
bigquery.ScalarQueryParameter(
'ts_value',
'TIMESTAMP',
datetime.datetime(year, month, day, hour, minute, tzinfo=pytz.UTC))
]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(query, job_config=job_config)
query_job.result() # Waits for job to complete
# Print the results.
destination_table_ref = query_job.destination
table = client.get_table(destination_table_ref)
for row in client.list_rows(table):
print(row)
def query_struct_params(x, y):
client = bigquery.Client()
query = 'SELECT @struct_value AS s;'
query_params = [
bigquery.StructQueryParameter(
'struct_value',
bigquery.ScalarQueryParameter('x', 'INT64', x),
bigquery.ScalarQueryParameter('y', 'STRING', y)
)
]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_job = client.query(query, job_config=job_config)
query_job.result() # Waits for job to complete
# Print the results.
destination_table_ref = query_job.destination
table = client.get_table(destination_table_ref)
for row in client.list_rows(table):
print(row)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
subparsers = parser.add_subparsers(dest='sample', help='samples')
named_parser = subparsers.add_parser(
'named',
help='Run a query with named parameters.')
named_parser.add_argument(
'corpus',
help='Corpus to search from Shakespeare dataset.')
named_parser.add_argument(
'min_word_count',
help='Minimum count of words to query.',
type=int)
positional_parser = subparsers.add_parser(
'positional',
help='Run a query with positional parameters.')
positional_parser.add_argument(
'corpus',
help='Corpus to search from Shakespeare dataset.')
positional_parser.add_argument(
'min_word_count',
help='Minimum count of words to query.',
type=int)
array_parser = subparsers.add_parser(
'array',
help='Run a query with an array parameter.')
array_parser.add_argument(
'gender',
choices=['F', 'M'],
help='Gender of baby in the Social Security baby names database.')
array_parser.add_argument(
'states',
help='U.S. States to consider for popular baby names.',
nargs='+')
timestamp_parser = subparsers.add_parser(
'timestamp',
help='Run a query with a timestamp parameter.')
timestamp_parser.add_argument('year', type=int)
timestamp_parser.add_argument('month', type=int)
timestamp_parser.add_argument('day', type=int)
timestamp_parser.add_argument('hour', type=int)
timestamp_parser.add_argument('minute', type=int)
struct_parser = subparsers.add_parser(
'struct',
help='Run a query with a struct parameter.')
struct_parser.add_argument('x', help='Integer for x', type=int)
struct_parser.add_argument('y', help='String for y')
args = parser.parse_args()
if args.sample == 'named':
query_named_params(args.corpus, args.min_word_count)
elif args.sample == 'positional':
query_positional_params(args.corpus, args.min_word_count)
elif args.sample == 'array':
query_array_params(args.gender, args.states)
elif args.sample == 'timestamp':
query_timestamp_params(
args.year, args.month, args.day, args.hour, args.minute)
elif args.sample == 'struct':
query_struct_params(args.x, args.y)
else:
print('Unexpected value for sample')