Skip to content

Instantly share code, notes, and snippets.

@tseaver
Created June 27, 2018 18:43
Show Gist options
  • Save tseaver/6c40a6a0f2bf9e71f0431cd37c9ca9fa to your computer and use it in GitHub Desktop.
Save tseaver/6c40a6a0f2bf9e71f0431cd37c9ca9fa to your computer and use it in GitHub Desktop.
import pprint
from google.api_core import exceptions
from google.cloud import bigquery
def setup_dataset(client):
ds_ref = client.dataset('gcp_5539')
try:
return client.get_dataset(ds_ref)
except exceptions.NotFound:
return client.create_dataset(
dataset=bigquery.Dataset(ds_ref))
def setup_table(client, dataset):
table_ref = dataset.table('test')
schema = [
bigquery.SchemaField('doi', 'STRING', 'REQUIRED', None, ()),
bigquery.SchemaField('subjects', 'STRING', 'REPEATED', None, ()),
]
try:
return client.get_table(table_ref)
except exceptions.NotFound:
return client.create_table(
table=bigquery.Table(table_ref, schema))
def main():
client = bigquery.Client()
dataset = setup_dataset(client)
table = setup_table(client, dataset)
print('Schema:')
print('-' * 78)
pprint.pprint(table.schema)
print('-' * 78)
rows = [
{"doi": "test-{}".format(i), "subjects": ["something"]}
for i in range(1000)
]
errors = client.insert_rows(table, rows)
print('Errors:')
print('-' * 78)
pprint.pprint(errors)
print('-' * 78)
rows = client.list_rows(table)
print('Fetched:')
print('-' * 78)
pprint.pprint(list(rows))
print('-' * 78)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment