mysql - Big Query with Python -
is there anyway run queries repeatedly on google big query using python script?
i want query dataset using google big query platform weeks data , want on year. bit tedious query dataset 52 times. instead prefer write python script(as know python).
i hope point me in right direction regarding this.
bigquery supplies client libraries several languages -- see https://cloud.google.com/bigquery/client-libraries -- , in particular python, docs @ https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/python/latest/?_ga=1.176926572.834714677.1415848949 (you'll need follow hyperlinks understand docs).
https://cloud.google.com/bigquery/bigquery-api-quickstart gives example of command-line program, in either java or python, uses google bigquery api run query on 1 of available sample datasets , display result. after imports, , setting few constants, python script boils down to
storage = storage('bigquery_credentials.dat') credentials = storage.get() if credentials none or credentials.invalid: # run oauth2 flow default arguments. credentials = tools.run_flow(flow, storage, tools.argparser.parse_args([])) http = httplib2.http() http = credentials.authorize(http) bigquery_service = build('bigquery', 'v2', http=http) try: query_request = bigquery_service.jobs() query_data = {'query':'select top( title, 10) title, count(*) revision_count [publicdata:samples.wikipedia] wp_namespace = 0;'} query_response = query_request.query(projectid=project_number, body=query_data).execute() print 'query results:' row in query_response['rows']: result_row = [] field in row['f']: result_row.append(field['v']) print ('\t').join(result_row) except httperror err: print 'error:', pprint.pprint(err.content) except accesstokenrefresherror: print ("credentials have been revoked or expired, please re-run" "the application re-authorize")
as see, 30 lines, concerned getting , checking authorization , handling errors. "core" part, net of such considerations, half lines:
bigquery_service = build('bigquery', 'v2', http=http) query_request = bigquery_service.jobs() query_data = {'query':'select top( title, 10) title, count(*) revision_count [publicdata:samples.wikipedia] wp_namespace = 0;'} query_response = query_request.query(projectid=project_number, body=query_data).execute() print 'query results:' row in query_response['rows']: result_row = [] field in row['f']: result_row.append(field['v']) print ('\t').join(result_row)
Comments
Post a Comment