Created
September 24, 2013 21:00
-
-
Save fhoffa/6691166 to your computer and use it in GitHub Desktop.
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
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from apiclient.discovery import build\n", | |
"\n", | |
"from google.appengine.api import app_identity\n", | |
"from oauth2client import appengine\n", | |
"\n", | |
"from apiclient import errors\n", | |
"\n", | |
"import httplib2\n", | |
"\n", | |
"project_id = app_identity.get_application_id()\n", | |
"scope = 'https://www.googleapis.com/auth/bigquery'\n", | |
"credentials = appengine.AppAssertionCredentials(scope=scope)\n", | |
"http = credentials.authorize(httplib2.Http())\n", | |
"service = build('bigquery', 'v2', http=http)\n", | |
"\n", | |
"# Python example\n", | |
"# Loads the table from Google Cloud Storage and prints the table.\n", | |
"def loadTable(service, projectId, datasetId, targetTableId, sourceCSV):\n", | |
" try:\n", | |
" jobCollection = service.jobs()\n", | |
" jobData = {\n", | |
" 'projectId': projectId,\n", | |
" 'configuration': {\n", | |
" 'load': {\n", | |
" 'sourceUris': [sourceCSV],\n", | |
" 'schema': {\n", | |
" 'fields': [\n", | |
" {\n", | |
" 'name': 'Name',\n", | |
" 'type': 'STRING'\n", | |
" },\n", | |
" {\n", | |
" 'name': 'Age',\n", | |
" 'type': 'INTEGER'\n", | |
" },\n", | |
" {\n", | |
" 'name': 'Weight',\n", | |
" 'type': 'FLOAT'\n", | |
" },\n", | |
" {\n", | |
" 'name': 'IsMagic',\n", | |
" 'type': 'BOOLEAN'\n", | |
" }\n", | |
" ]\n", | |
" },\n", | |
" 'destinationTable': {\n", | |
" 'projectId': projectId,\n", | |
" 'datasetId': datasetId,\n", | |
" 'tableId': targetTableId\n", | |
" },\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
"\n", | |
" insertResponse = jobCollection.insert(projectId=projectId,\n", | |
" body=jobData).execute()\n", | |
"\n", | |
" # Ping for status until it is done, with a short pause between calls.\n", | |
" import time\n", | |
" while True:\n", | |
" job = jobCollection.get(\n", | |
" projectId=projectId,\n", | |
" jobId=insertResponse['jobReference']['jobId']).execute()\n", | |
" if 'DONE' == job['status']['state']:\n", | |
" print 'Done sending job!\\n------------'\n", | |
" pprint.pprint(job['status'])\n", | |
" return\n", | |
" print 'Waiting for loading to complete...'\n", | |
" time.sleep(10)\n", | |
" if 'errorResult' in job['status']:\n", | |
" print 'Error loading table: ', pprint.pprint(job)\n", | |
" return\n", | |
"\n", | |
" except errors.HttpError as err:\n", | |
" print 'Error in loadTable: ', pprint.pprint(err)\n", | |
" \n", | |
"\n", | |
"loadTable(service, project_id, 'datasetId', 'targetTableId', 'gs://thisis/file')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment