Skip to content

Instantly share code, notes, and snippets.

@rkulla
Created November 7, 2015 17:47
Show Gist options
  • Save rkulla/3d154cca3bca6c21041f to your computer and use it in GitHub Desktop.
Save rkulla/3d154cca3bca6c21041f to your computer and use it in GitHub Desktop.
BigQuery CLI query to json
#!/usr/bin/env python
# Query Big Query and return JSON results.
# Just a python example. There are better CLI tools than this:
# https://cloud.google.com/bigquery/bq-command-line-tool?hl=en
# usage: python bq.py
# For pretty output, pipe to 'prettyjson' or json.tool and/or pygmentize:
# bq.py | python -m json.tool | pygmentize -l javascript
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
import json
import httplib2
import logging
import simplejson as json
# Change these config values
SQL = """SELECT * FROM [dataset.table] WHERE ..."""
JSON_KEY_PATH='<YOUR JSON PEM KEY PATH'
PROJECT_ID='<YOUR PROJECT ID>'
def sync_query(bigquery, project_id, query, auth_http, timeout=10000):
# https://cloud.google.com/bigquery/docs/reference/v2/jobs/query
query_data = {
'query': query,
'timeoutMs': timeout
}
return bigquery.jobs().query(
projectId=project_id,
body=query_data).execute(http=auth_http)
def main():
json_key = json.load(open(JSON_KEY_PATH))
logging.basicConfig()
credentials = SignedJwtAssertionCredentials(
json_key['client_email'],
json_key['private_key'],
scope='https://www.googleapis.com/auth/bigquery')
auth_http = credentials.authorize(httplib2.Http())
service = build('bigquery', 'v2')
res = sync_query(service, PROJECT_ID, SQL, auth_http)
j = json.dumps(res)
print j
# https://cloud.google.com/bigquery/docs/reference/v2/jobs/getQueryResults
# j = json.loads(j)
# fields = j['schema']['fields']
# rows = j["rows"]
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment