BigQuery Connection - Python

This coding example contains instructions on how to connect to BigQuery with the Python programming language in the Sandbox. It also gives examples of:

  • different ways of displaying the results from the query execution

  • how to configure a job to run as a query

  • how to list datasets and tables in a project

Location of the script and readme in the Sandbox

/finngen/library-green/scripts/code_snippets/

Run the whole and script and prints out results of multiple queries

python3 /finngen/library-green/scripts/code_snippets/bigquery_python.py

You can copy paste from below explanation or take the code directly from the file itself.

Connecting to BigQuery using Python package

# Import system packages
import os, sys
# Import bigquery
from google.cloud import bigquery
# Import pandas package
import pandas as pd

# Connection to BigQuery
client = bigquery.Client()

Once the connection is established you can list the datasets

Similarly, you can list tables in a particular dataset or all datasets.

Running a simple query to select FINNGENID, SOURCE, APPROX_EVENT_DAY from the table finngen_r10_service_sector_detailed_longitudinal in the dataset sandbox_tools_r10 within the project finngen-production-library. Only select 10 rows from the table.

You can print the results of the query in list format.

You can also print the results in dataframe format.

More complex queries can be found in the script file. Also, you can use anaconda environment to connect to BigQuery in the Sandbox. Details on how to use anaconda python in Sandbox are here.

Last updated

Was this helpful?