bi_etl.bulk_loaders.redshift_s3_base module

class bi_etl.bulk_loaders.redshift_s3_base.RedShiftS3Base(config: S3_Bulk_Loader_Config)[source]

Bases: S3BulkLoaderMixin, BulkLoader

Common base for Redshift bulk loaders that stage files on S3 and load them with COPY.

Subclasses must implement get_copy_sql() to supply the format specific COPY options and load_from_iterable() (typically by mixing in a format mixin).

Note

S3BulkLoaderMixin is listed before BulkLoader so that its file upload hooks replace the no-op versions.

DEFAULT_ROWS_PER_FILE = 25000
__init__(config: S3_Bulk_Loader_Config)[source]
Parameters:

config – S3_Bulk_Loader_Config with the S3 and Redshift settings.

add_redact_value(redact_value: str)

Register a value (such as a password) that log_with_redactions() must hide.

Parameters:

redact_value – Text to replace with asterisks in logged messages. None is ignored.

apply_updates(table_object: Table, update_rows: Sequence[Row])

NOT TESTED !

clean_s3_folder(s3_full_folder: str)

Delete every object in the bucket whose key starts with s3_full_folder.

Parameters:

s3_full_folder – Key prefix of the folder to empty.

get_copy_sql(s3_source_path: str, table_to_load: str, file_compression: str = '', analyze_compression: str = None, options: str = '')[source]

Build the format specific COPY statement. Must be implemented by each subclass.

Parameters:
  • s3_source_path – Path of the S3 folder (within the bucket) containing the files to load.

  • table_to_load – Qualified name of the table to load.

  • file_compression – Compression option text to add to the statement (e.g. GZIP).

  • analyze_compression – COMPUPDATE setting. Defaults to the configured analyze_compression.

  • options – Additional COPY options to append.

Returns:

The SQL text.

Return type:

str

load_from_files(local_files: List[str | Path], table_object: Table, table_to_load: str = None, perform_rename: bool = False, file_compression: str = '', options: str = '', analyze_compression: str = None) → int[source]

Wait for the uploads of local_files to S3 (uploading any that were not queued) and COPY them into the table.

See _upload_files_to_s3().

Parameters:
  • local_files – Local files to load.

  • table_object – Table being loaded.

  • table_to_load – Name of the table (or temp table) to load into. Defaults to table_object.qualified_table_name.

  • perform_rename – If True, rename table_to_load over the original table after the load.

  • file_compression – Compression option text to add to the statement (e.g. GZIP).

  • options – Additional COPY options to append.

  • analyze_compression – COMPUPDATE setting. Defaults to the configured analyze_compression.

Returns:

Number of rows loaded.

Return type:

int

abstractmethod load_from_iterable(iterable: Iterable[Row], table_object: Table, table_to_load: str | None = None, perform_rename: bool = False, progress_frequency: int = 10, analyze_compression: str | None = None, parent_task: ETLTask | None = None) → int

Note: This method is IS required for all bulk loaders.

load_from_s3_path(s3_source_path: str, table_object: Table, table_to_load: str = None, s3_source_path_is_absolute: bool = True, file_list: List[str] | None = None, file_compression: str = '', options: str = '', analyze_compression: str = None, perform_rename: bool = False) → int[source]

Load files that are already in S3 into the table with COPY.

Parameters:
  • s3_source_path – S3 folder (or file prefix) to load from.

  • table_object – Table being loaded.

  • table_to_load – Name of the table (or temp table) to load into. Defaults to table_object.qualified_table_name.

  • s3_source_path_is_absolute – If False, s3_source_path is relative to the configured S3 folder.

  • file_list – Names of the files being loaded. Not used by the COPY itself.

  • file_compression – Compression option text to add to the statement (e.g. GZIP).

  • options – Additional COPY options to append.

  • analyze_compression – COMPUPDATE setting. Defaults to the configured analyze_compression.

  • perform_rename – If True, rename table_to_load over the original table after the load.

Returns:

Number of rows loaded.

Return type:

int

load_table_from_cache(table_object: Table, table_to_load: str | None = None, perform_rename: bool = False, progress_frequency: int = 10, analyze_compression: str | None = None) → int[source]

Load the rows in the cache of table_object with load_from_iterable().

Parameters:
  • table_object – Table whose cache is loaded.

  • table_to_load – Name of the table (or temp table) used when renaming.

  • perform_rename – If True, rename table_to_load over the original table (keeping the original as _old).

  • progress_frequency – Seconds between progress log messages.

  • analyze_compression – COMPUPDATE setting. Defaults to the configured analyze_compression.

Returns:

Number of rows loaded.

Return type:

int

log_with_redactions(*, log_level: int, message: str)

Log message after replacing every registered redaction value with asterisks.

Parameters:
  • log_level – Logging level, such as logging.INFO.

  • message – Text to log.

property needs_all_columns

True if the rows passed to the loader must contain every column of the target table, in table order.

Returns:

True by default. Loaders that can map columns by name override this to return False.

Return type:

bool

rename_table(temp_table_name: str | None, table_object: Table)

Swap a freshly loaded temp table in place of table_object.

The existing table is renamed with an _old suffix (dropping any earlier _old table first), and temp_table_name is renamed to the name of table_object.

Parameters:
  • temp_table_name – Name of the table that was loaded. If None, a warning is logged and nothing is renamed.

  • table_object – The table to replace.

s3_folder_contents(s3_full_folder: str)

List the objects in the bucket whose keys start with s3_full_folder.

Parameters:

s3_full_folder – Key prefix of the folder.

Returns:

The matching S3 objects.

Return type:

list

property s3_password

The S3 password from the config. It is registered for redaction in log messages.