Skip to content

Commit 7652467

Browse files
committed
Merge pull request #130 from GoogleCloudPlatform/bq-json
Specify source format, so json uploads work.
2 parents 9dc64e3 + ec81550 commit 7652467

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

bigquery/api/load_data_by_post.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,35 @@ def load_data(schema_path, data_path, project_id, dataset_id, table_id):
5050
credentials = GoogleCredentials.get_application_default()
5151
bigquery = discovery.build('bigquery', 'v2', credentials=credentials)
5252

53+
# Infer the data format from the name of the data file.
54+
source_format = 'CSV'
55+
if data_path[-5:].lower() == '.json':
56+
source_format = 'NEWLINE_DELIMITED_JSON'
57+
58+
# Post to the jobs resource using the client's media upload interface. See:
59+
# http://developers.google.com/api-client-library/python/guide/media_upload
5360
insert_request = bigquery.jobs().insert(
5461
projectId=project_id,
62+
# Provide a configuration object. See:
63+
# https://cloud.google.com/bigquery/docs/reference/v2/jobs#resource
5564
body={
56-
"configuration": {
57-
"load": {
58-
"schema": {
59-
"fields": json.load(open(schema_path, 'r'))
65+
'configuration': {
66+
'load': {
67+
'schema': {
68+
'fields': json.load(open(schema_path, 'r'))
69+
},
70+
'destinationTable': {
71+
'projectId': project_id,
72+
'datasetId': dataset_id,
73+
'tableId': table_id
6074
},
61-
"destinationTable": {
62-
"projectId": project_id,
63-
"datasetId": dataset_id,
64-
"tableId": table_id
65-
}
75+
'sourceFormat': source_format,
6676
}
6777
}
6878
},
6979
media_body=MediaFileUpload(
7080
data_path,
71-
mimetype="application/octet-stream"))
72-
81+
mimetype='application/octet-stream'))
7382
job = insert_request.execute()
7483

7584
print('Waiting for job to finish...')
@@ -78,12 +87,14 @@ def load_data(schema_path, data_path, project_id, dataset_id, table_id):
7887
projectId=job['jobReference']['projectId'],
7988
jobId=job['jobReference']['jobId'])
8089

90+
# Poll the job until it finishes.
8191
while True:
8292
result = status_request.execute(num_retries=2)
8393

8494
if result['status']['state'] == 'DONE':
85-
if 'errorResult' in result['status']:
86-
raise RuntimeError(result['status']['errorResult'])
95+
if result['status'].get('errors'):
96+
raise RuntimeError('\n'.join(
97+
e['message'] for e in result['status']['errors']))
8798
print('Job complete.')
8899
return
89100

0 commit comments

Comments
 (0)