Skip to content

Commit cda34f4

Browse files
committed
Merge pull request #1263 from tseaver/1259-sync_bigquery_usage_docs
Sync BigQuery usage docs with current API
2 parents a04840b + a23b486 commit cda34f4

File tree

1 file changed

+41
-46
lines changed

1 file changed

+41
-46
lines changed

docs/bigquery-usage.rst

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,7 @@ Get rows from a table's data:
232232
>>> client = bigquery.Client()
233233
>>> dataset = client.dataset('dataset_name')
234234
>>> table = dataset.table(name='person_ages')
235-
>>> rows, next_page_token = table.data(max_results=100) # API request
236-
>>> rows.csv.headers
237-
('full_name', 'age')
238-
>>> list(rows.csv)
239-
[('Abel Adamson', 27), ('Beverly Bowman', 33)]
235+
>>> rows, next_page_token = table.fetch_data(max_results=100) # API request
240236
>>> for row in rows:
241237
... for field, value in zip(table.schema, row):
242238
... do_something(field, value)
@@ -283,20 +279,22 @@ Run a query which can be expected to complete within bounded time:
283279
>>> query = """\
284280
SELECT count(*) AS age_count FROM dataset_name.person_ages
285281
"""
286-
>>> results = client.run_sync_query(query, timeout_ms=1000)
282+
>>> job = client.run_sync_query(query)
283+
>>> job.timeout_ms = 1000
284+
>>> job.run() # API request
287285
>>> retry_count = 100
288-
>>> while retry_count > 0 and not results.job_complete:
286+
>>> while retry_count > 0 and not job.complete:
289287
... retry_count -= 1
290288
... time.sleep(10)
291-
... results.reload() # API request
292-
>>> results.schema
289+
... job.reload() # API request
290+
>>> job.schema
293291
[{'name': 'age_count', 'type': 'integer', 'mode': 'nullable'}]
294-
>>> results.rows
292+
>>> job.rows
295293
[(15,)]
296294

297295
.. note::
298296

299-
If the query takes longer than the timeout allowed, ``results.job_complete``
297+
If the query takes longer than the timeout allowed, ``job.complete``
300298
will be ``False``: we therefore poll until it is completed.
301299

302300
Querying data (asynchronous)
@@ -315,9 +313,9 @@ Background a query, loading the results into a table:
315313
"""
316314
>>> dataset = client.dataset('dataset_name')
317315
>>> table = dataset.table(name='person_ages')
318-
>>> job = client.run_async_query(query,
319-
... destination=table,
320-
... write_disposition='truncate')
316+
>>> job = client.run_async_query('fullname-age-query-job', query)
317+
>>> job.destination_table = table
318+
>>> job.write_disposition= 'truncate'
321319
>>> job.job_id
322320
'e3344fba-09df-4ae0-8337-fddee34b3840'
323321
>>> job.type
@@ -361,31 +359,22 @@ Poll until the job is complete:
361359
Inserting data (synchronous)
362360
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
363361

364-
Load data synchronously from a local CSV file into a new table. First,
365-
create the job locally:
362+
Load data synchronously from a local CSV file into a new table:
366363

367364
.. doctest::
368365

366+
>>> import csv
369367
>>> from gcloud import bigquery
368+
>>> from gcloud.bigquery import SchemaField
370369
>>> client = bigquery.Client()
371370
>>> table = dataset.table(name='person_ages')
371+
>>> table.schema = [
372+
... SchemaField(name='full_name', type='string', mode='required'),
373+
... SchemaField(name='age', type='int', mode='required)]
372374
>>> with open('/path/to/person_ages.csv', 'rb') as file_obj:
373-
... job = table.load_from_file(
374-
... file_obj,
375-
... source_format='CSV',
376-
... skip_leading_rows=1
377-
... write_disposition='truncate',
378-
... ) # API request
379-
>>> job.job_id
380-
'e3344fba-09df-4ae0-8337-fddee34b3840'
381-
>>> job.type
382-
'load'
383-
>>> job.created
384-
datetime.datetime(2015, 7, 23, 9, 30, 20, 268260, tzinfo=<UTC>)
385-
>>> job.state
386-
'done'
387-
>>> job.ended
388-
datetime.datetime(2015, 7, 23, 9, 30, 21, 334792, tzinfo=<UTC>)
375+
... reader = csv.reader(file_obj)
376+
... rows = list(reader)
377+
>>> table.insert_data(rows) # API request
389378

390379
Inserting data (asynchronous)
391380
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -397,13 +386,17 @@ the job locally:
397386
.. doctest::
398387

399388
>>> from gcloud import bigquery
389+
>>> from gcloud.bigquery import SchemaField
400390
>>> client = bigquery.Client()
401391
>>> table = dataset.table(name='person_ages')
402-
>>> job = table.load_from_storage(bucket_name='bucket-name',
403-
... object_name_glob='object-prefix*',
404-
... source_format='CSV',
405-
... skip_leading_rows=1,
406-
... write_disposition='truncate')
392+
>>> table.schema = [
393+
... SchemaField(name='full_name', type='string', mode='required'),
394+
... SchemaField(name='age', type='int', mode='required)]
395+
>>> job = client.load_table_from_storage(
396+
... 'load-from-storage-job', table, 'gs://bucket-name/object-prefix*')
397+
>>> job.source_format = 'CSV'
398+
>>> job.skip_leading_rows = 1 # count of skipped header rows
399+
>>> job.write_disposition = 'truncate'
407400
>>> job.job_id
408401
'e3344fba-09df-4ae0-8337-fddee34b3840'
409402
>>> job.type
@@ -423,7 +416,7 @@ Then, begin executing the job on the server:
423416

424417
.. doctest::
425418

426-
>>> job.submit() # API call
419+
>>> job.begin() # API call
427420
>>> job.created
428421
datetime.datetime(2015, 7, 23, 9, 30, 20, 268260, tzinfo=<UTC>)
429422
>>> job.state
@@ -455,11 +448,12 @@ located on Google Cloud Storage. First, create the job locally:
455448
>>> from gcloud import bigquery
456449
>>> client = bigquery.Client()
457450
>>> table = dataset.table(name='person_ages')
458-
>>> job = table.export_to_storage(bucket_name='bucket-name',
459-
... object_name_glob='export-prefix*.csv',
460-
... destination_format='CSV',
461-
... print_header=1,
462-
... write_disposition='truncate')
451+
>>> job = client.extract_table_to_storage(
452+
... 'extract-person-ages-job', table,
453+
... 'gs://bucket-name/export-prefix*.csv')
454+
... job.destination_format = 'CSV'
455+
... job.print_header = True
456+
... job.write_disposition = 'truncate'
463457
>>> job.job_id
464458
'e3344fba-09df-4ae0-8337-fddee34b3840'
465459
>>> job.type
@@ -479,7 +473,7 @@ Then, begin executing the job on the server:
479473

480474
.. doctest::
481475

482-
>>> job.submit() # API call
476+
>>> job.begin() # API call
483477
>>> job.created
484478
datetime.datetime(2015, 7, 23, 9, 30, 20, 268260, tzinfo=<UTC>)
485479
>>> job.state
@@ -512,7 +506,8 @@ First, create the job locally:
512506
>>> client = bigquery.Client()
513507
>>> source_table = dataset.table(name='person_ages')
514508
>>> destination_table = dataset.table(name='person_ages_copy')
515-
>>> job = source_table.copy_to(destination_table) # API request
509+
>>> job = client.copy_table(
510+
... 'copy-table-job', destination_table, source_table)
516511
>>> job.job_id
517512
'e3344fba-09df-4ae0-8337-fddee34b3840'
518513
>>> job.type
@@ -532,7 +527,7 @@ Then, begin executing the job on the server:
532527

533528
.. doctest::
534529

535-
>>> job.submit() # API call
530+
>>> job.begin() # API call
536531
>>> job.created
537532
datetime.datetime(2015, 7, 23, 9, 30, 20, 268260, tzinfo=<UTC>)
538533
>>> job.state

0 commit comments

Comments
 (0)