Python
BigQuery#
Using the API#
Authorization / Configuration#
Use
Client
objects to configure your applications.Client
objects hold both aproject
and an authenticated connection to the PubSub service.The authentication credentials can be implicitly determined from the environment or directly via
from_service_account_json
andfrom_service_account_p12
.After setting
GOOGLE_APPLICATION_CREDENTIALS
andGCLOUD_PROJECT
environment variables, create an instance ofClient
.>>> from gcloud import bigquery >>> client = bigquery.Client()
Override the credentials inferred from the environment by passing explicit
credentials
to one of the alternative classmethods, from_service_account_json:>>> from gcloud import bigquery >>> client = bigquery.Client.from_service_account_json('/path/to/creds.json')
or from_service_account_p12:
>>> from gcloud import bigquery >>> client = bigquery.Client.from_service_account_p12('/path/to/creds.p12', 'jrandom@example.com')
Override the project inferred from the environment by passing an explicit
project
to the constructor, or to either of the alternative classmethods:>>> from gcloud import bigquery >>> client = bigquery.Client(project='PROJECT_ID')
Manage datasets#
Create a new dataset for the client’s project:
>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> dataset.create() # API request
Check for the existence of a dataset:
>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> dataset.exists() # API request
True
List datasets for the client’s project:
>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> datasets, next_page_token = client.list_datasets() # API request
>>> [dataset.name for dataset in datasets]
['dataset_name']
Patch metadata for a dataset:
>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> one_day_ms = 24 * 60 * 60 * 1000
>>> dataset.patch(description='Description goes here',
... default_table_expiration_ms=one_day_ms) # API request
Replace the ACL for a project, and update all writeable fields:
>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> dataset.get() # API request
>>> acl = list(dataset.acl)
>>> acl.append(bigquery.Access(role='READER', entity_type='domain', entity='example.com'))
>>> dataset.acl = acl
>>> dataset.update() # API request
Delete a dataset:
>>> from gcloud import bigquery
>>> client = bigquery.Client()
>>> dataset = client.dataset('dataset_name')
>>> dataset.delete() # API request