Query BigQuery with Python

This week, I wanted to connect with BigQuery using Python. BigQuery is a service from Google, which helps to store and process large volumes of data quickly. We are using it to store Google billing data and wanted to generate some reports by executing different SQL queries.

from google.cloud import bigquery
from google.oauth2 import service_account

auth_json = Your JSON for authentication

credentials = service_account.Credentials.from_service_account_info(auth_json)

client = bigquery.Client(project="project name", credentials=credentials)

query_job = client.query("""your query inside""")

results = query_job.result()  # Waits for job to complete.

for row in results:
    print("{}".format(row.id, any other fields))


 

The above code shows, how we can connect and query the bigquery. It returns an iteratable, which can allow looping over the rows and access each column as shown in the code.

I hope it would help you in your big projects.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s