Base operator for SQL to GCS operators.

Module Contents



Copy data from SQL to Google Cloud Storage in JSON, CSV, or Parquet format.

class*, sql, bucket, filename, schema_filename=None, approx_max_file_size_bytes=1900000000, export_format='json', stringify_dict=False, field_delimiter=',', null_marker=None, gzip=False, schema=None, parameters=None, gcp_conn_id='google_cloud_default', impersonation_chain=None, upload_metadata=False, exclude_columns=None, partition_columns=None, write_on_empty=False, parquet_row_group_size=100000, **kwargs)[source]

Bases: airflow.models.BaseOperator

Copy data from SQL to Google Cloud Storage in JSON, CSV, or Parquet format.

  • sql (str) – The SQL to execute.

  • bucket (str) – The bucket to upload to.

  • filename (str) – The filename to use as the object name when uploading to Google Cloud Storage. A {} should be specified in the filename to allow the operator to inject file numbers in cases where the file is split due to size.

  • schema_filename (str | None) – If set, the filename to use as the object name when uploading a .json file containing the BigQuery schema fields for the table that was dumped from the database.

  • approx_max_file_size_bytes (int) – This operator supports the ability to split large table dumps into multiple files (see notes in the filename param docs above). This param allows developers to specify the file size of the splits. Check to see the maximum allowed file size for a single object.

  • export_format (str) – Desired format of files to be exported. (json, csv or parquet)

  • stringify_dict (bool) – Whether to dump Dictionary type objects (such as JSON columns) as a string. Applies only to CSV/JSON export format.

  • field_delimiter (str) – The delimiter to be used for CSV files.

  • null_marker (str | None) – The null marker to be used for CSV files.

  • gzip (bool) – Option to compress file for upload (does not apply to schemas).

  • schema (str | list | None) – The schema to use, if any. Should be a list of dict or a str. Pass a string if using Jinja template, otherwise, pass a list of dict. Examples could be seen: /schemas#specifying_a_json_schema_file

  • gcp_conn_id (str) – (Optional) The connection ID used to connect to Google Cloud.

  • parameters (dict | None) – a parameters dict that is substituted at query runtime.

  • impersonation_chain (str | Sequence[str] | None) – Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated).

  • upload_metadata (bool) – whether to upload the row count metadata as blob metadata

  • exclude_columns (set | None) – set of columns to exclude from transmission

  • partition_columns (list | None) – list of columns to use for file partitioning. In order to use this parameter, you must sort your dataset by partition_columns. Do this by passing an ORDER BY clause to the sql query. Files are uploaded to GCS as objects with a hive style partitioning directory structure (templated).

  • write_on_empty (bool) – Optional parameter to specify whether to write a file if the export does not return any rows. Default is False so we will not write a file if the export returns no rows.

  • parquet_row_group_size (int) – The approximate number of rows in each row group when using parquet format. Using a large row group size can reduce the file size and improve the performance of reading the data, but it needs more memory to execute the operator. (default: 100000)

template_fields: Sequence[str] = ('sql', 'bucket', 'filename', 'schema_filename', 'schema', 'parameters', 'impersonation_chain',...[source]
template_ext: Sequence[str] = ('.sql',)[source]
ui_color = '#a0e08c'[source]

Derive when creating an operator.

Context is the same dictionary used as when rendering jinja templates.

Refer to get_template_context for more context.

convert_types(schema, col_type_dict, row)[source]

Convert values from DBAPI to output-friendly formats.

abstract query()[source]

Execute DBAPI query.

abstract field_to_bigquery(field)[source]

Convert a DBAPI field to BigQuery schema format.

abstract convert_type(value, schema_type, **kwargs)[source]

Convert a value from DBAPI to output-friendly formats.

Was this entry helpful?