airflow.contrib.operators.bigquery_operator

This module contains Google BigQuery operators.

Module Contents

Bases: airflow.models.baseoperator.BaseOperatorLink

Helper class for constructing BigQuery link.

name = BigQuery Console[source]
class airflow.contrib.operators.bigquery_operator.BigQueryOperator(bql=None, sql=None, destination_dataset_table=None, write_disposition='WRITE_EMPTY', allow_large_results=False, flatten_results=None, bigquery_conn_id='bigquery_default', delegate_to=None, udf_config=None, use_legacy_sql=True, maximum_billing_tier=None, maximum_bytes_billed=None, create_disposition='CREATE_IF_NEEDED', schema_update_options=(), query_params=None, labels=None, priority='INTERACTIVE', time_partitioning=None, api_resource_configs=None, cluster_fields=None, location=None, *args, **kwargs)[source]

Bases: airflow.models.baseoperator.BaseOperator

Executes BigQuery SQL queries in a specific BigQuery database

Parameters
  • bql (Can receive a str representing a sql statement, a list of str (sql statements), or reference to a template file. Template reference are recognized by str ending in '.sql'.) – (Deprecated. Use sql parameter instead) the sql code to be executed (templated)

  • sql (Can receive a str representing a sql statement, a list of str (sql statements), or reference to a template file. Template reference are recognized by str ending in '.sql'.) – the sql code to be executed (templated)

  • destination_dataset_table (str) – A dotted (<project>.|<project>:)<dataset>.<table> that, if set, will store the results of the query. (templated)

  • write_disposition (str) – Specifies the action that occurs if the destination table already exists. (default: ‘WRITE_EMPTY’)

  • create_disposition (str) – Specifies whether the job is allowed to create new tables. (default: ‘CREATE_IF_NEEDED’)

  • allow_large_results (bool) – Whether to allow large results.

  • flatten_results (bool) – If true and query uses legacy SQL dialect, flattens all nested and repeated fields in the query results. allow_large_results must be true if this is set to false. For standard SQL queries, this flag is ignored and results are never flattened.

  • bigquery_conn_id (str) – reference to a specific BigQuery hook.

  • delegate_to (str) – The account to impersonate, if any. For this to work, the service account making the request must have domain-wide delegation enabled.

  • udf_config (list) – The User Defined Function configuration for the query. See https://cloud.google.com/bigquery/user-defined-functions for details.

  • use_legacy_sql (bool) – Whether to use legacy SQL (true) or standard SQL (false).

  • maximum_billing_tier (int) – Positive integer that serves as a multiplier of the basic price. Defaults to None, in which case it uses the value set in the project.

  • maximum_bytes_billed (float) – Limits the bytes billed for this job. Queries that will have bytes billed beyond this limit will fail (without incurring a charge). If unspecified, this will be set to your project default.

  • api_resource_configs (dict) – a dictionary that contain params ‘configuration’ applied for Google BigQuery Jobs API: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs for example, {‘query’: {‘useQueryCache’: False}}. You could use it if you need to provide some params that are not supported by BigQueryOperator like args.

  • schema_update_options (tuple) – Allows the schema of the destination table to be updated as a side effect of the load job.

  • query_params (list) – a list of dictionary containing query parameter types and values, passed to BigQuery. The structure of dictionary should look like ‘queryParameters’ in Google BigQuery Jobs API: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs. For example, [{ ‘name’: ‘corpus’, ‘parameterType’: { ‘type’: ‘STRING’ }, ‘parameterValue’: { ‘value’: ‘romeoandjuliet’ } }].

  • labels (dict) – a dictionary containing labels for the job/query, passed to BigQuery

  • priority (str) – Specifies a priority for the query. Possible values include INTERACTIVE and BATCH. The default value is INTERACTIVE.

  • time_partitioning (dict) – configure optional time partitioning fields i.e. partition by field, type and expiration as per API specifications.

  • cluster_fields (list[str]) – Request that the result of this query be stored sorted by one or more columns. This is only available in conjunction with time_partitioning. The order of columns given determines the sort order.

  • location (str) – The geographic location of the job. Required except for US and EU. See details at https://cloud.google.com/bigquery/docs/locations#specifying_your_location

template_fields = ['bql', 'sql', 'destination_dataset_table', 'labels'][source]
template_ext = ['.sql'][source]
ui_color = #e4f0e8[source]
execute(self, context)[source]
on_kill(self)[source]
class airflow.contrib.operators.bigquery_operator.BigQueryCreateEmptyTableOperator(dataset_id, table_id, project_id=None, schema_fields=None, gcs_schema_object=None, time_partitioning=None, bigquery_conn_id='bigquery_default', google_cloud_storage_conn_id='google_cloud_default', delegate_to=None, labels=None, *args, **kwargs)[source]

Bases: airflow.models.baseoperator.BaseOperator

Creates a new, empty table in the specified BigQuery dataset, optionally with schema.

The schema to be used for the BigQuery table may be specified in one of two ways. You may either directly pass the schema fields in, or you may point the operator to a Google cloud storage object name. The object in Google cloud storage must be a JSON file with the schema fields in it. You can also create a table without schema.

Parameters
  • project_id (str) – The project to create the table into. (templated)

  • dataset_id (str) – The dataset to create the table into. (templated)

  • table_id (str) – The Name of the table to be created. (templated)

  • schema_fields (list) –

    If set, the schema field list as defined here: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.load.schema

    Example:

    schema_fields=[{"name": "emp_name", "type": "STRING", "mode": "REQUIRED"},
                   {"name": "salary", "type": "INTEGER", "mode": "NULLABLE"}]
    

  • gcs_schema_object (str) – Full path to the JSON file containing schema (templated). For example: gs://test-bucket/dir1/dir2/employee_schema.json

  • time_partitioning (dict) –

    configure optional time partitioning fields i.e. partition by field, type and expiration as per API specifications.

  • bigquery_conn_id (str) – Reference to a specific BigQuery hook.

  • google_cloud_storage_conn_id (str) – Reference to a specific Google cloud storage hook.

  • delegate_to (str) – The account to impersonate, if any. For this to work, the service account making the request must have domain-wide delegation enabled.

  • labels (dict) –

    a dictionary containing labels for the table, passed to BigQuery

    Example (with schema JSON in GCS):

    CreateTable = BigQueryCreateEmptyTableOperator(
        task_id='BigQueryCreateEmptyTableOperator_task',
        dataset_id='ODS',
        table_id='Employees',
        project_id='internal-gcp-project',
        gcs_schema_object='gs://schema-bucket/employee_schema.json',
        bigquery_conn_id='airflow-service-account',
        google_cloud_storage_conn_id='airflow-service-account'
    )
    

    Corresponding Schema file (employee_schema.json):

    [
      {
        "mode": "NULLABLE",
        "name": "emp_name",
        "type": "STRING"
      },
      {
        "mode": "REQUIRED",
        "name": "salary",
        "type": "INTEGER"
      }
    ]
    

    Example (with schema in the DAG):

    CreateTable = BigQueryCreateEmptyTableOperator(
        task_id='BigQueryCreateEmptyTableOperator_task',
        dataset_id='ODS',
        table_id='Employees',
        project_id='internal-gcp-project',
        schema_fields=[{"name": "emp_name", "type": "STRING", "mode": "REQUIRED"},
                       {"name": "salary", "type": "INTEGER", "mode": "NULLABLE"}],
        bigquery_conn_id='airflow-service-account',
        google_cloud_storage_conn_id='airflow-service-account'
    )
    

template_fields = ['dataset_id', 'table_id', 'project_id', 'gcs_schema_object', 'labels'][source]
ui_color = #f0eee4[source]
execute(self, context)[source]
class airflow.contrib.operators.bigquery_operator.BigQueryCreateExternalTableOperator(bucket, source_objects, destination_project_dataset_table, schema_fields=None, schema_object=None, source_format='CSV', compression='NONE', skip_leading_rows=0, field_delimiter=', ', max_bad_records=0, quote_character=None, allow_quoted_newlines=False, allow_jagged_rows=False, bigquery_conn_id='bigquery_default', google_cloud_storage_conn_id='google_cloud_default', delegate_to=None, src_fmt_configs=None, labels=None, *args, **kwargs)[source]

Bases: airflow.models.baseoperator.BaseOperator

Creates a new external table in the dataset with the data in Google Cloud Storage.

The schema to be used for the BigQuery table may be specified in one of two ways. You may either directly pass the schema fields in, or you may point the operator to a Google cloud storage object name. The object in Google cloud storage must be a JSON file with the schema fields in it.

Parameters
  • bucket (str) – The bucket to point the external table to. (templated)

  • source_objects (list) – List of Google cloud storage URIs to point table to. (templated) If source_format is ‘DATASTORE_BACKUP’, the list must only contain a single URI.

  • destination_project_dataset_table (str) – The dotted (<project>.)<dataset>.<table> BigQuery table to load data into (templated). If <project> is not included, project will be the project defined in the connection json.

  • schema_fields (list) –

    If set, the schema field list as defined here: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.load.schema

    Example:

    schema_fields=[{"name": "emp_name", "type": "STRING", "mode": "REQUIRED"},
                   {"name": "salary", "type": "INTEGER", "mode": "NULLABLE"}]
    

    Should not be set when source_format is ‘DATASTORE_BACKUP’.

  • schema_object (str) – If set, a GCS object path pointing to a .json file that contains the schema for the table. (templated)

  • source_format (str) – File format of the data.

  • compression (str) – [Optional] The compression type of the data source. Possible values include GZIP and NONE. The default value is NONE. This setting is ignored for Google Cloud Bigtable, Google Cloud Datastore backups and Avro formats.

  • skip_leading_rows (int) – Number of rows to skip when loading from a CSV.

  • field_delimiter (str) – The delimiter to use for the CSV.

  • max_bad_records (int) – The maximum number of bad records that BigQuery can ignore when running the job.

  • quote_character (str) – The value that is used to quote data sections in a CSV file.

  • allow_quoted_newlines (bool) – Whether to allow quoted newlines (true) or not (false).

  • allow_jagged_rows (bool) – Accept rows that are missing trailing optional columns. The missing values are treated as nulls. If false, records with missing trailing columns are treated as bad records, and if there are too many bad records, an invalid error is returned in the job result. Only applicable to CSV, ignored for other formats.

  • bigquery_conn_id (str) – Reference to a specific BigQuery hook.

  • google_cloud_storage_conn_id (str) – Reference to a specific Google cloud storage hook.

  • delegate_to (str) – The account to impersonate, if any. For this to work, the service account making the request must have domain-wide delegation enabled.

  • src_fmt_configs (dict) – configure optional fields specific to the source format

  • labels (dict) – a dictionary containing labels for the table, passed to BigQuery

template_fields = ['bucket', 'source_objects', 'schema_object', 'destination_project_dataset_table', 'labels'][source]
ui_color = #f0eee4[source]
execute(self, context)[source]
class airflow.contrib.operators.bigquery_operator.BigQueryDeleteDatasetOperator(dataset_id, project_id=None, bigquery_conn_id='bigquery_default', delegate_to=None, *args, **kwargs)[source]

Bases: airflow.models.baseoperator.BaseOperator

This operator deletes an existing dataset from your Project in Big query. https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets/delete

Parameters
  • project_id (str) – The project id of the dataset.

  • dataset_id (str) – The dataset to be deleted.

Example:

delete_temp_data = BigQueryDeleteDatasetOperator(dataset_id = 'temp-dataset',
                                                 project_id = 'temp-project',
                                                 bigquery_conn_id='_my_gcp_conn_',
                                                 task_id='Deletetemp',
                                                 dag=dag)
template_fields = ['dataset_id', 'project_id'][source]
ui_color = #f00004[source]
execute(self, context)[source]
class airflow.contrib.operators.bigquery_operator.BigQueryCreateEmptyDatasetOperator(dataset_id, project_id=None, dataset_reference=None, bigquery_conn_id='bigquery_default', delegate_to=None, *args, **kwargs)[source]

Bases: airflow.models.baseoperator.BaseOperator

This operator is used to create new dataset for your Project in Big query. https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#resource

Parameters
  • project_id (str) – The name of the project where we want to create the dataset. Don’t need to provide, if projectId in dataset_reference.

  • dataset_id (str) – The id of dataset. Don’t need to provide, if datasetId in dataset_reference.

  • dataset_reference – Dataset reference that could be provided with request body. More info: https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#resource

template_fields = ['dataset_id', 'project_id'][source]
ui_color = #f0eee4[source]
execute(self, context)[source]