Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make table snippets testable #1833

Merged
merged 1 commit into from
Jul 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 33 additions & 76 deletions docs/bigquery-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,107 +120,64 @@ Tables

Tables exist within datasets. List tables for the dataset:

.. doctest::

>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> tables, next_page_token = dataset.list_tables() # API request
>>> [table.name for table in tables]
['table_name']
.. literalinclude:: bigquery_snippets.py
:start-after: [START dataset_list_tables]
:end-before: [END dataset_list_tables]

Create a table:

.. doctest::

>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> table = dataset.table(name='person_ages')
>>> table.create() # API request
.. literalinclude:: bigquery_snippets.py
:start-after: [START table_create]
:end-before: [END table_create]

Check for the existence of a table:

.. doctest::

>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> table = dataset.table(name='person_ages')
>>> table.exists() # API request
True
.. literalinclude:: bigquery_snippets.py
:start-after: [START table_exists]
:end-before: [END table_exists]

Refresh metadata for a table (to pick up changes made by another client):

.. doctest::

>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> dataset.reload() # API request
.. literalinclude:: bigquery_snippets.py
:start-after: [START table_reload]
:end-before: [END table_reload]

Patch specific properties for a table:

.. doctest::

>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> table = dataset.table(name='person_ages')
>>> table.patch(friendly_name='Person Ages',
... description='Ages of persons') # API request
.. literalinclude:: bigquery_snippets.py
:start-after: [START table_patch]
:end-before: [END table_patch]

Update all writable metadata for a table

.. doctest::

>>> from gcloud import bigquery
>>> from gcloud.bigquery import SchemaField
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> table = dataset.table(name='person_ages')
>>> table.schema = [
... SchemaField('full_name', 'STRING', mode='required'),
... SchemaField('age', 'INTEGER', mode='required)]
>>> table.update() # API request
.. literalinclude:: bigquery_snippets.py
:start-after: [START table_update]
:end-before: [END table_update]

Upload table data from a file:
Get rows from a table's data:

.. doctest::
.. literalinclude:: bigquery_snippets.py
:start-after: [START table_fetch_data]
:end-before: [END table_fetch_data]

>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> table = dataset.table(name='person_ages')
>>> table.schema = [
... SchemaField('full_name', 'STRING', mode='required'),
... SchemaField('age', 'INTEGER', mode='required)]
>>> with open('person_ages.csv', 'rb') as csv_file:
... table.upload_from_file(csv_file, 'CSV',
... create_disposition='CREATE_IF_NEEDED')
Insert rows into a table's data:

Get rows from a table's data:
.. literalinclude:: bigquery_snippets.py
:start-after: [START table_insert_data]
:end-before: [END table_insert_data]

.. doctest::
Upload table data from a file:

>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> table = dataset.table(name='person_ages')
>>> rows, next_page_token = table.fetch_data(max_results=100) # API request
>>> for row in rows:
... for field, value in zip(table.schema, row):
... do_something(field, value)
.. literalinclude:: bigquery_snippets.py
:start-after: [START table_upload_from_file]
:end-before: [END table_upload_from_file]

Delete a table:

.. doctest::
.. literalinclude:: bigquery_snippets.py
:start-after: [START table_delete]
:end-before: [END table_delete]

>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> table = dataset.table(name='person_ages')
>>> table.delete() # API request

Jobs
----
Expand Down
Loading