Created
June 27, 2018 18:43
-
-
Save tseaver/6c40a6a0f2bf9e71f0431cd37c9ca9fa to your computer and use it in GitHub Desktop.
Attempt to reproduce https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5539
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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