1414# See the License for the specific language governing permissions and
1515# limitations under the License.
1616
17- """Command-line application to perform synchronous queries in BigQuery.
17+ """Command-line app to perform synchronous queries with parameters in BigQuery.
1818
1919For more information, see the README.md under /bigquery.
2020
2121Example invocation:
22- $ python sync_query_params.py 'romeoandjuliet' 100
22+ $ python sync_query_params.py --use-named-params 'romeoandjuliet' 100
23+ $ python sync_query_params.py --use-positional-params 'romeoandjuliet' 100
2324"""
2425
2526import argparse
2627
27- # [START sync_query_params]
2828from google .cloud import bigquery
2929
3030
31- def sync_query_params (corpus , min_word_count ):
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 ):
3248 client = bigquery .Client ()
3349 query_results = client .run_sync_query (
3450 """SELECT word, word_count
@@ -38,15 +54,24 @@ def sync_query_params(corpus, min_word_count):
3854 ORDER BY word_count DESC;
3955 """ ,
4056 query_parameters = (
41- bigquery .ScalarQueryParameter (None , 'STRING' , corpus ),
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 ),
4264 bigquery .ScalarQueryParameter (None , 'INT64' , min_word_count )))
4365
4466 # Only standard SQL syntax supports parameters in queries.
4567 # See: https://cloud.google.com/bigquery/sql-reference/
4668 query_results .use_legacy_sql = False
4769 query_results .run ()
48- print ( 'Positional parameter query completed' )
70+ print_results ( query_results )
4971
72+
73+ def sync_query_named_params (corpus , min_word_count ):
74+ client = bigquery .Client ()
5075 query_results = client .run_sync_query (
5176 """SELECT word, word_count
5277 FROM `bigquery-public-data.samples.shakespeare`
@@ -62,23 +87,14 @@ def sync_query_params(corpus, min_word_count):
6287 min_word_count )))
6388 query_results .use_legacy_sql = False
6489 query_results .run ()
65- print ( 'Named parameter query completed' )
90+ print_results ( query_results )
6691
67- # Print the query results by requesting a page at a time.
68- page_token = None
6992
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]
93+ def main (use_named_params = False , corpus = 'romeoandjuliet' , min_word_count = 100 ):
94+ if use_named_params :
95+ sync_query_named_params (corpus , min_word_count )
96+ else :
97+ sync_query_positional_params (corpus , min_word_count )
8298
8399
84100if __name__ == '__main__' :
@@ -93,6 +109,15 @@ def sync_query_params(corpus, min_word_count):
93109 help = 'Minimum count of words to query.' ,
94110 type = int )
95111
112+ params_type_parser = parser .add_mutually_exclusive_group (required = False )
113+ params_type_parser .add_argument (
114+ '--use-named-params' ,
115+ dest = 'use_named_params' ,
116+ action = 'store_true' )
117+ params_type_parser .add_argument (
118+ '--use-positional-params' ,
119+ dest = 'use_named_params' ,
120+ action = 'store_false' )
121+ parser .set_defaults (use_named_params = False )
96122 args = parser .parse_args ()
97-
98- sync_query_params (args .corpus , args .min_word_count )
123+ main (args .use_named_params , args .corpus , args .min_word_count )
0 commit comments