Skip to content

Commit 9da9d82

Browse files
committed
Add snippets for 'Client.list_jobs', 'Client.run_sync_query'.
1 parent 42f4497 commit 9da9d82

2 files changed

Lines changed: 133 additions & 88 deletions

File tree

docs/bigquery-usage.rst

Lines changed: 12 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -195,104 +195,34 @@ Jobs describe actions peformed on data in BigQuery tables:
195195

196196
List jobs for a project:
197197

198-
.. doctest::
198+
.. literalinclude:: bigquery_snippets.py
199+
:start-after: [START client_list_jobs]
200+
:end-before: [END client_list_jobs]
199201

200-
>>> from gcloud import bigquery
201-
>>> client = bigquery.Client()
202-
>>> jobs, token = client.list_jobs() # API request
203-
>>> [(job.name, job.job_type, job.created, job.state) for job in jobs]
204-
['load-table-job', 'load', (datetime.datetime(2015, 7, 23, 9, 30, 20, 268260, tzinfo=<UTC>), 'done')]
205202

206203
Querying data (synchronous)
207204
~~~~~~~~~~~~~~~~~~~~~~~~~~~
208205

209206
Run a query which can be expected to complete within bounded time:
210207

211-
.. doctest::
212-
213-
>>> from gcloud import bigquery
214-
>>> client = bigquery.Client()
215-
>>> QUERY = """\
216-
... SELECT count(*) AS age_count FROM dataset_name.person_ages
217-
... """
218-
>>> query = client.run_sync_query(QUERY)
219-
>>> query.timeout_ms = 1000
220-
>>> query.run() # API request
221-
>>> query.complete
222-
True
223-
>>> len(query.schema)
224-
1
225-
>>> field = query.schema[0]
226-
>>> field.name
227-
u'count'
228-
>>> field.field_type
229-
u'INTEGER'
230-
>>> field.mode
231-
u'NULLABLE'
232-
>>> query.rows
233-
[(15,)]
234-
>>> query.total_rows
235-
1
208+
.. literalinclude:: bigquery_snippets.py
209+
:start-after: [START client_run_sync_query]
210+
:end-before: [END client_run_sync_query]
236211

237212
If the rows returned by the query do not fit into the inital response,
238213
then we need to fetch the remaining rows via ``fetch_data``:
239214

240-
.. doctest::
241-
242-
>>> from gcloud import bigquery
243-
>>> client = bigquery.Client()
244-
>>> QUERY = """\
245-
... SELECT * FROM dataset_name.person_ages
246-
... """
247-
>>> query = client.run_sync_query(QUERY)
248-
>>> query.timeout_ms = 1000
249-
>>> query.run() # API request
250-
>>> query.complete
251-
True
252-
>>> query.total_rows
253-
1234
254-
>>> query.page_token
255-
'8d6e452459238eb0fe87d8eb191dd526ee70a35e'
256-
>>> do_something_with(query.schema, query.rows)
257-
>>> token = query.page_token # for initial request
258-
>>> while True:
259-
... do_something_with(query.schema, rows)
260-
... if token is None:
261-
... break
262-
... rows, _, token = query.fetch_data(page_token=token)
263-
215+
.. literalinclude:: bigquery_snippets.py
216+
:start-after: [START client_run_sync_query_paged]
217+
:end-before: [END client_run_sync_query_paged]
264218

265219
If the query takes longer than the timeout allowed, ``query.complete``
266220
will be ``False``. In that case, we need to poll the associated job until
267221
it is done, and then fetch the reuslts:
268222

269-
.. doctest::
270-
271-
>>> from gcloud import bigquery
272-
>>> client = bigquery.Client()
273-
>>> QUERY = """\
274-
... SELECT * FROM dataset_name.person_ages
275-
... """
276-
>>> query = client.run_sync_query(QUERY)
277-
>>> query.timeout_ms = 1000
278-
>>> query.run() # API request
279-
>>> query.complete
280-
False
281-
>>> job = query.job
282-
>>> retry_count = 100
283-
>>> while retry_count > 0 and job.state == 'running':
284-
... retry_count -= 1
285-
... time.sleep(10)
286-
... job.reload() # API call
287-
>>> job.state
288-
'done'
289-
>>> token = None # for initial request
290-
>>> while True:
291-
... rows, _, token = query.fetch_data(page_token=token)
292-
... do_something_with(query.schema, rows)
293-
... if token is None:
294-
... break
295-
223+
.. literalinclude:: bigquery_snippets.py
224+
:start-after: [START client_run_sync_query_timeout]
225+
:end-before: [END client_run_sync_query_timeout]
296226

297227

298228
Querying data (asynchronous)

docs/bigquery_snippets.py

Lines changed: 121 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ def delete(self):
6868

6969

7070
@snippet
71-
def client_list_datasets(client, to_delete): # pylint: disable=unused-argument
71+
def client_list_datasets(client, _):
7272
"""List datasets for a project."""
7373

74-
def do_something_with(sub): # pylint: disable=unused-argument
74+
def do_something_with(_):
7575
pass
7676

7777
# [START client_list_datasets]
@@ -182,7 +182,7 @@ def dataset_update(client, to_delete):
182182

183183

184184
@snippet
185-
def dataset_delete(client, to_delete): # pylint: disable=unused-argument
185+
def dataset_delete(client, _):
186186
"""Delete a dataset."""
187187
DATASET_NAME = 'dataset_delete_%d' % (_millis(),)
188188
dataset = client.dataset(DATASET_NAME)
@@ -439,13 +439,12 @@ def table_upload_from_file(client, to_delete):
439439

440440

441441
@snippet
442-
def table_delete(client, to_delete): # pylint: disable=unused-argument
442+
def table_delete(client, _):
443443
"""Delete a table."""
444444
DATASET_NAME = 'table_delete_dataset_%d' % (_millis(),)
445445
TABLE_NAME = 'table_create_table_%d' % (_millis(),)
446446
dataset = client.dataset(DATASET_NAME)
447447
dataset.create()
448-
to_delete.append(dataset)
449448

450449
table = dataset.table(TABLE_NAME, SCHEMA)
451450
table.create()
@@ -457,6 +456,122 @@ def table_delete(client, to_delete): # pylint: disable=unused-argument
457456
# [END table_delete]
458457

459458

459+
@snippet
460+
def client_list_jobs(client, _):
461+
"""List jobs for a project."""
462+
463+
def do_something_with(_):
464+
pass
465+
466+
# [START client_list_jobs]
467+
jobs, token = client.list_jobs() # API request
468+
while True:
469+
for job in jobs:
470+
do_something_with(job)
471+
if token is None:
472+
break
473+
jobs, token = client.list_jobs(page_token=token) # API request
474+
# [END client_list_jobs]
475+
476+
477+
@snippet
478+
def client_run_sync_query(client, _):
479+
"""Run a synchronous query."""
480+
LIMIT = 100
481+
LIMITED = '%s LIMIT %d' % (QUERY, LIMIT)
482+
TIMEOUT_MS = 1000
483+
484+
# [START client_run_sync_query]
485+
query = client.run_sync_query(LIMITED)
486+
query.timeout_ms = TIMEOUT_MS
487+
query.run() # API request
488+
489+
assert query.complete
490+
assert len(query.rows) == LIMIT
491+
assert [field.name for field in query.schema] == ['name']
492+
# [END client_run_sync_query]
493+
494+
495+
@snippet
496+
def client_run_sync_query_paged(client, _):
497+
"""Run a synchronous query with paged results."""
498+
TIMEOUT_MS = 1000
499+
PAGE_SIZE = 100
500+
LIMIT = 1000
501+
LIMITED = '%s LIMIT %d' % (QUERY, LIMIT)
502+
503+
all_rows = []
504+
505+
def do_something_with(rows):
506+
all_rows.extend(rows)
507+
508+
# [START client_run_sync_query_paged]
509+
query = client.run_sync_query(LIMITED)
510+
query.timeout_ms = TIMEOUT_MS
511+
query.max_results = PAGE_SIZE
512+
query.run() # API request
513+
514+
assert query.complete
515+
assert query.page_token is not None
516+
assert len(query.rows) == PAGE_SIZE
517+
assert [field.name for field in query.schema] == ['name']
518+
519+
rows = query.rows
520+
token = query.page_token
521+
522+
while True:
523+
do_something_with(rows)
524+
if token is None:
525+
break
526+
rows, total_count, token = query.fetch_data(
527+
page_token=token) # API request
528+
# [END client_run_sync_query_paged]
529+
530+
assert total_count == LIMIT
531+
assert len(all_rows) == LIMIT
532+
533+
534+
@snippet
535+
def client_run_sync_query_timeout(client, _):
536+
"""Run a synchronous query w/ timeout"""
537+
TIMEOUT_MS = 10
538+
539+
all_rows = []
540+
541+
def do_something_with(rows):
542+
all_rows.extend(rows)
543+
544+
# [START client_run_sync_query_timeout]
545+
query = client.run_sync_query(QUERY)
546+
query.timeout_ms = TIMEOUT_MS
547+
query.use_query_cache = False
548+
query.run() # API request
549+
550+
assert not query.complete
551+
552+
job = query.job
553+
job.reload() # API rquest
554+
retry_count = 0
555+
556+
while retry_count < 10 and job.state != u'DONE':
557+
time.sleep(1.5**retry_count) # exponential backoff
558+
retry_count += 1
559+
job.reload() # API request
560+
561+
assert job.state == u'DONE'
562+
563+
rows, total_count, token = query.fetch_data() # API request
564+
while True:
565+
do_something_with(rows)
566+
if token is None:
567+
break
568+
rows, total_count, token = query.fetch_data(
569+
page_token=token) # API request
570+
# [END client_run_sync_query_timeout]
571+
572+
assert len(all_rows) == total_count
573+
574+
460575
def _find_examples():
461576
funcs = [obj for obj in globals().values()
462577
if getattr(obj, '_snippet', False)]
@@ -468,7 +583,7 @@ def main():
468583
client = Client()
469584
for example in _find_examples():
470585
to_delete = []
471-
print('%-25s: %s' % (
586+
print('%-30s: %s' % (
472587
example.func_name, example.func_doc))
473588
try:
474589
example(client, to_delete)

0 commit comments

Comments
 (0)