Skip to content

Commit e5c0ff2

Browse files
alixhamiJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
BigQuery: Updates samples for BigQuery Beta 2 (do not merge until release) (#1178)
* updates client.extract_table() sample * updates client.load_table_from_file() sample * updates client.load_table_from_uri() sample * updates parameterized query samples * updates query samples * updates dataset quickstart * fixes typo in sample * updates simple app sample * updates snippets * updates stream data sample * updates user credentials sample * removes job_id generation * Update BQ lib to 0.28.0. Use max_results in list_rows. * updates public dataset queries to use 'bigquery-public-data' instead of outdated 'publicdata' * updates readme and help sections
1 parent cf081f6 commit e5c0ff2

16 files changed

+347
-338
lines changed

bigquery/cloud-client/README.rst

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ Install Dependencies
7070
Samples
7171
-------------------------------------------------------------------------------
7272

73+
Simple Application
74+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75+
76+
77+
78+
To run this sample:
79+
80+
.. code-block:: bash
81+
82+
$ python simple_app.py
83+
84+
7385
Quickstart
7486
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7587

@@ -82,7 +94,7 @@ To run this sample:
8294
$ python quickstart.py
8395
8496
85-
Sync query
97+
Query
8698
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8799

88100

@@ -91,26 +103,35 @@ To run this sample:
91103

92104
.. code-block:: bash
93105
94-
$ python sync_query.py
106+
$ python query.py
95107
96-
usage: sync_query.py [-h] query
108+
usage: query.py [-h] [--use_standard_sql]
109+
[--destination_table DESTINATION_TABLE]
110+
query
97111
98-
Command-line application to perform synchronous queries in BigQuery.
112+
Command-line application to perform queries in BigQuery.
99113
100114
For more information, see the README.rst.
101115
102116
Example invocation:
103-
$ python sync_query.py \
104-
'SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus'
117+
$ python query.py '#standardSQL
118+
SELECT corpus
119+
FROM `bigquery-public-data.samples.shakespeare`
120+
GROUP BY corpus
121+
ORDER BY corpus'
105122
106123
positional arguments:
107-
query BigQuery SQL Query.
124+
query BigQuery SQL Query.
108125
109126
optional arguments:
110-
-h, --help show this help message and exit
127+
-h, --help show this help message and exit
128+
--use_standard_sql Use standard SQL syntax.
129+
--destination_table DESTINATION_TABLE
130+
Destination table to use for results. Example:
131+
my_dataset.my_table
111132
112133
113-
Async query
134+
Parameterized Query
114135
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
115136

116137

@@ -119,23 +140,29 @@ To run this sample:
119140

120141
.. code-block:: bash
121142
122-
$ python async_query.py
143+
$ python query_params.py
123144
124-
usage: async_query.py [-h] query
145+
usage: query_params.py [-h] {named,positional,array,timestamp,struct} ...
125146
126-
Command-line application to perform asynchronous queries in BigQuery.
147+
Command-line app to perform queries with parameters in BigQuery.
127148
128149
For more information, see the README.rst.
129150
130151
Example invocation:
131-
$ python async_query.py \
132-
'SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus'
152+
$ python query_params.py named 'romeoandjuliet' 100
153+
$ python query_params.py positional 'romeoandjuliet' 100
133154
134155
positional arguments:
135-
query BigQuery SQL Query.
156+
{named,positional,array,timestamp,struct}
157+
samples
158+
named Run a query with named parameters.
159+
positional Run a query with positional parameters.
160+
array Run a query with an array parameter.
161+
timestamp Run a query with a timestamp parameter.
162+
struct Run a query with a struct parameter.
136163
137164
optional arguments:
138-
-h, --help show this help message and exit
165+
-h, --help show this help message and exit
139166
140167
141168
Snippets
@@ -202,20 +229,21 @@ To run this sample:
202229
203230
$ python load_data_from_file.py
204231
205-
usage: load_data_from_file.py [-h] dataset_name table_name source_file_name
232+
usage: load_data_from_file.py [-h] dataset_id table_id source_file_name
206233
207234
Loads data into BigQuery from a local file.
208235
209236
For more information, see the README.rst.
210237
211238
Example invocation:
212-
$ python load_data_from_file.py example_dataset example_table example-data.csv
239+
$ python load_data_from_file.py example_dataset example_table \
240+
example-data.csv
213241
214242
The dataset and table should already exist.
215243
216244
positional arguments:
217-
dataset_name
218-
table_name
245+
dataset_id
246+
table_id
219247
source_file_name Path to a .csv file to upload.
220248
221249
optional arguments:
@@ -233,25 +261,26 @@ To run this sample:
233261
234262
$ python load_data_from_gcs.py
235263
236-
usage: load_data_from_gcs.py [-h] dataset_name table_name source
264+
usage: load_data_from_gcs.py [-h] dataset_id table_id source
237265
238266
Loads data into BigQuery from an object in Google Cloud Storage.
239267
240268
For more information, see the README.rst.
241269
242270
Example invocation:
243-
$ python load_data_from_gcs.py example_dataset example_table gs://example-bucket/example-data.csv
271+
$ python load_data_from_gcs.py example_dataset example_table \
272+
gs://example-bucket/example-data.csv
244273
245274
The dataset and table should already exist.
246275
247276
positional arguments:
248-
dataset_name
249-
table_name
250-
source The Google Cloud Storage object to load. Must be in the format
251-
gs://bucket_name/object_name
277+
dataset_id
278+
table_id
279+
source The Google Cloud Storage object to load. Must be in the format
280+
gs://bucket_name/object_name
252281
253282
optional arguments:
254-
-h, --help show this help message and exit
283+
-h, --help show this help message and exit
255284
256285
257286
Load streaming data
@@ -265,24 +294,25 @@ To run this sample:
265294
266295
$ python stream_data.py
267296
268-
usage: stream_data.py [-h] dataset_name table_name json_data
297+
usage: stream_data.py [-h] dataset_id table_id json_data
269298
270299
Loads a single row of data directly into BigQuery.
271300
272301
For more information, see the README.rst.
273302
274303
Example invocation:
275-
$ python stream_data.py example_dataset example_table '["Gandalf", 2000]'
304+
$ python stream_data.py example_dataset example_table \
305+
'["Gandalf", 2000]'
276306
277307
The dataset and table should already exist.
278308
279309
positional arguments:
280-
dataset_name
281-
table_name
282-
json_data The row to load into BigQuery as an array in JSON format.
310+
dataset_id
311+
table_id
312+
json_data The row to load into BigQuery as an array in JSON format.
283313
284314
optional arguments:
285-
-h, --help show this help message and exit
315+
-h, --help show this help message and exit
286316
287317
288318
Export data to Cloud Storage
@@ -296,25 +326,26 @@ To run this sample:
296326
297327
$ python export_data_to_gcs.py
298328
299-
usage: export_data_to_gcs.py [-h] dataset_name table_name destination
329+
usage: export_data_to_gcs.py [-h] dataset_id table_id destination
300330
301331
Exports data from BigQuery to an object in Google Cloud Storage.
302332
303333
For more information, see the README.rst.
304334
305335
Example invocation:
306-
$ python export_data_to_gcs.py example_dataset example_table gs://example-bucket/example-data.csv
336+
$ python export_data_to_gcs.py example_dataset example_table \
337+
gs://example-bucket/example-data.csv
307338
308339
The dataset and table should already exist.
309340
310341
positional arguments:
311-
dataset_name
312-
table_name
313-
destination The desintation Google Cloud Storage object.Must be in the
314-
format gs://bucket_name/object_name
342+
dataset_id
343+
table_id
344+
destination The destination Google Cloud Storage object. Must be in the
345+
format gs://bucket_name/object_name
315346
316347
optional arguments:
317-
-h, --help show this help message and exit
348+
-h, --help show this help message and exit
318349
319350
320351

bigquery/cloud-client/README.rst.in

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ setup:
1616
- install_deps
1717

1818
samples:
19+
- name: Simple Application
20+
file: simple_app.py
1921
- name: Quickstart
2022
file: quickstart.py
21-
- name: Sync query
22-
file: sync_query.py
23+
- name: Query
24+
file: query.py
2325
show_help: true
24-
- name: Async query
25-
file: async_query.py
26+
- name: Parameterized Query
27+
file: query_params.py
2628
show_help: true
2729
- name: Snippets
2830
file: snippets.py

bigquery/cloud-client/export_data_to_gcs.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,43 @@
1919
For more information, see the README.rst.
2020
2121
Example invocation:
22-
$ python export_data_to_gcs.py example_dataset example_table \
22+
$ python export_data_to_gcs.py example_dataset example_table \\
2323
gs://example-bucket/example-data.csv
2424
2525
The dataset and table should already exist.
2626
"""
2727

2828
import argparse
29-
import uuid
3029

3130
from google.cloud import bigquery
3231

3332

34-
def export_data_to_gcs(dataset_name, table_name, destination):
33+
def export_data_to_gcs(dataset_id, table_id, destination):
3534
bigquery_client = bigquery.Client()
36-
dataset = bigquery_client.dataset(dataset_name)
37-
table = dataset.table(table_name)
38-
job_name = str(uuid.uuid4())
35+
dataset_ref = bigquery_client.dataset(dataset_id)
36+
table_ref = dataset_ref.table(table_id)
3937

40-
job = bigquery_client.extract_table_to_storage(
41-
job_name, table, destination)
38+
job = bigquery_client.extract_table(table_ref, destination)
4239

43-
job.begin()
44-
job.result() # Wait for job to complete
40+
job.result() # Waits for job to complete
4541

4642
print('Exported {}:{} to {}'.format(
47-
dataset_name, table_name, destination))
43+
dataset_id, table_id, destination))
4844

4945

5046
if __name__ == '__main__':
5147
parser = argparse.ArgumentParser(
5248
description=__doc__,
5349
formatter_class=argparse.RawDescriptionHelpFormatter)
54-
parser.add_argument('dataset_name')
55-
parser.add_argument('table_name')
50+
parser.add_argument('dataset_id')
51+
parser.add_argument('table_id')
5652
parser.add_argument(
57-
'destination', help='The desintation Google Cloud Storage object.'
53+
'destination', help='The destination Google Cloud Storage object. '
5854
'Must be in the format gs://bucket_name/object_name')
5955

6056
args = parser.parse_args()
6157

6258
export_data_to_gcs(
63-
args.dataset_name,
64-
args.table_name,
59+
args.dataset_id,
60+
args.table_id,
6561
args.destination)

bigquery/cloud-client/load_data_from_file.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
For more information, see the README.rst.
2020
2121
Example invocation:
22-
$ python load_data_from_file.py example_dataset example_table \
22+
$ python load_data_from_file.py example_dataset example_table \\
2323
example-data.csv
2424
2525
The dataset and table should already exist.
@@ -30,38 +30,37 @@
3030
from google.cloud import bigquery
3131

3232

33-
def load_data_from_file(dataset_name, table_name, source_file_name):
33+
def load_data_from_file(dataset_id, table_id, source_file_name):
3434
bigquery_client = bigquery.Client()
35-
dataset = bigquery_client.dataset(dataset_name)
36-
table = dataset.table(table_name)
37-
38-
# Reload the table to get the schema.
39-
table.reload()
35+
dataset_ref = bigquery_client.dataset(dataset_id)
36+
table_ref = dataset_ref.table(table_id)
4037

4138
with open(source_file_name, 'rb') as source_file:
4239
# This example uses CSV, but you can use other formats.
4340
# See https://cloud.google.com/bigquery/loading-data
44-
job = table.upload_from_file(
45-
source_file, source_format='text/csv')
41+
job_config = bigquery.LoadJobConfig()
42+
job_config.source_format = 'text/csv'
43+
job = bigquery_client.load_table_from_file(
44+
source_file, table_ref, job_config=job_config)
4645

47-
job.result() # Wait for job to complete
46+
job.result() # Waits for job to complete
4847

4948
print('Loaded {} rows into {}:{}.'.format(
50-
job.output_rows, dataset_name, table_name))
49+
job.output_rows, dataset_id, table_id))
5150

5251

5352
if __name__ == '__main__':
5453
parser = argparse.ArgumentParser(
5554
description=__doc__,
5655
formatter_class=argparse.RawDescriptionHelpFormatter)
57-
parser.add_argument('dataset_name')
58-
parser.add_argument('table_name')
56+
parser.add_argument('dataset_id')
57+
parser.add_argument('table_id')
5958
parser.add_argument(
6059
'source_file_name', help='Path to a .csv file to upload.')
6160

6261
args = parser.parse_args()
6362

6463
load_data_from_file(
65-
args.dataset_name,
66-
args.table_name,
64+
args.dataset_id,
65+
args.table_id,
6766
args.source_file_name)

0 commit comments

Comments
 (0)