airflow.contrib.operators.sql_to_gcs

Base operator for SQL to GCS operators.

Module Contents

airflow.contrib.operators.sql_to_gcs.PY3[source]
class airflow.contrib.operators.sql_to_gcs.BaseSQLToGoogleCloudStorageOperator(sql, bucket, filename, schema_filename=None, approx_max_file_size_bytes=1900000000, export_format='json', field_delimiter=', ', gzip=False, schema=None, parameters=None, google_cloud_storage_conn_id='google_cloud_default', delegate_to=None, *args, **kwargs)[source]

Bases: airflow.models.BaseOperator

Parameters
  • 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) – 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 (long) – 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 https://cloud.google.com/storage/quotas to see the maximum allowed file size for a single object.

  • export_format (str) – Desired format of files to be exported.

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

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

  • schema (str or list) – 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: https://cloud.google.com/bigquery/docs /schemas#specifying_a_json_schema_file

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

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

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

template_fields = ['sql', 'bucket', 'filename', 'schema_filename', 'schema', 'parameters'][source]
template_ext = ['.sql'][source]
ui_color = #a0e08c[source]
execute(self, context)[source]
convert_types(self, schema, col_type_dict, row)[source]

Convert values from DBAPI to output-friendly formats.

_write_local_data_files(self, cursor)[source]

Takes a cursor, and writes results to a local file.

Returns

A dictionary where keys are filenames to be used as object names in GCS, and values are file handles to local files that contain the data for the GCS objects.

_configure_csv_file(self, file_handle, schema)[source]

Configure a csv writer with the file_handle and write schema as headers for the new file.

query(self)[source]

Execute DBAPI query.

field_to_bigquery(self, field)[source]

Convert a DBAPI field to BigQuery schema format.

convert_type(self, value, schema_type)[source]

Convert a value from DBAPI to output-friendly formats.

_get_col_type_dict(self)[source]

Return a dict of column name and column type based on self.schema if not None.

_write_local_schema_file(self, cursor)[source]

Takes a cursor, and writes the BigQuery schema for the results to a local file system.

Returns

A dictionary where key is a filename to be used as an object name in GCS, and values are file handles to local files that contains the BigQuery schema fields in .json format.

_upload_to_gcs(self, files_to_upload)[source]

Upload all of the file splits (and optionally the schema .json file) to Google cloud storage.

Was this entry helpful?