mozanalysis.bq

class mozanalysis.bq.BigQueryContext(dataset_id, project_id='moz-fx-data-bq-data-science')[source]

Holds a BigQuery client, and some configuration.

Parameters:
  • dataset_id (str) – Your BigQuery dataset id.

  • project_id (str, optional) – Your BigQuery project_id. Defaults to the DS team’s project.

run_query(sql, results_table=None, replace_tables=False)[source]

Run a query and return the result.

If results_table is provided, then save the results into there (or just query from there if it already exists). Returns a google.cloud.bigquery.table.RowIterator

Parameters:
  • sql (str) – A SQL query.

  • results_table (str, optional) – A table name, not including a project_id or dataset_id. The table name is used as a cache key (if the table already exists, we ignore sql and return the table’s contents), so it is wise for results_table to include a hash of sql.

  • replace_tables (bool) – Indicates if the results table should be replaced with new results, if that table is found.

fully_qualify_table_name(table_name)[source]

Given a table name, return it fully qualified.