Skip to content

Instantly share code, notes, and snippets.

@drcrallen
Created July 5, 2017 03:48
Show Gist options
  • Save drcrallen/7ce5fb627610349c9a9c5296ea91b244 to your computer and use it in GitHub Desktop.
Save drcrallen/7ce5fb627610349c9a9c5296ea91b244 to your computer and use it in GitHub Desktop.
Odd bigquery insert errors
import datetime
from google.cloud import bigquery
from oauth2client.client import GoogleCredentials
# Needs the following defined
#PROJECT_ID='foo'
#DATASET_ID='bar'
#TABLE_ID='bat'
d = {'LOTRANS': 92.0, 'FIRMWARE': '904.W1 .D USB FW:W1', 'MBATTCHG': 5, 'BATTV': 13.7, 'MINTIMEL': 3.0, 'STARTTIME': '2017-05-23T13:27:07', 'NOMBATTV': 12.0, 'DATE': '2017-07-05T03:25:23', 'MODEL': 'Back-UPS ES 550G', 'HITRANS': 139.0, 'TIMELEFT': 335.8, 'BCHARGE': 100.0, 'XONBATT': '2017-07-04T03:55:53', 'TONBATT': 0, 'LINEV': 122.0, 'XOFFBATT': '2017-07-04T03:55:55', 'ALARMDEL': 'No alarm', 'STATUS': 'ONLINE', 'ENDAPC': '2017-07-05T03:25:42', 'CUMONBATT': 2758, 'CABLE': 'USB Cable', 'UPSMODE': 'Stand Alone', 'LASTXFER': 'Unacceptable line voltage changes', 'DRIVER': 'USB UPS Driver', 'VERSION': '3.14.12 (29 March 2014) debian', 'LOADPCT': 0.0, 'NOMINV': 120.0, 'SERIALNO': 'REDACTED', 'HOSTNAME': 'raspberrypi-master', 'UPSNAME': 'raspberrypi-master', 'NUMXFERS': '188', 'SENSE': 'Medium', 'APC': '001,035,0928', 'MAXTIME': 0, 'BATTDATE': '2016-12-02', 'STATFLAG': 83886088}
date = datetime.datetime(2017, 7, 5, 3, 25, 23)
bigquery_client = bigquery.Client(project=PROJECT_ID)
dataset = bigquery_client.dataset(DATASET_ID)
table = dataset.table('{0}${1}'.format(TABLE_ID, date.strftime('%Y%m%d')))
table.reload()
rows = [d]
errors = table.insert_data(rows, row_ids=[date.isoformat()])
if not errors:
print('Success')
else:
print(errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment