bi_etl.bulk_loaders.redshift_s3_json_loader module

class bi_etl.bulk_loaders.redshift_s3_json_loader.RedShiftS3JSONBulk(config: S3_Bulk_Loader_Config)[source]

Bases: RedShiftS3Base

Bulk loader for Redshift using gzipped JSON files staged to S3 and COPY ... JSON 'auto'.

Note: s3_file_max_rows is used as the number of rows per JSON file Each file is queued for upload as soon as it is complete.

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 = None, options: str = '')[source]

Build the JSON COPY statement.

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

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

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[source]

Write rows from iterable to local gzipped JSON files of at most rows_per_file rows each, and load them with load_from_files().

Each file is queued for upload (see _queue_file_for_upload()) as soon as it is closed, so load_from_files() only has to wait for the uploads still in progress.

Parameters:
  • iterable – Source of Row objects 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.

  • progress_frequency – Seconds between progress log messages. Pass None to disable.

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

  • parent_task – Optional parent task (not used).

Returns:

Number of rows written.

Return type:

int

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

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

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

Always False. JSON 'auto' maps keys to columns by name, so a subset of columns can be loaded.

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.