Uploads an Arrow Table to Redshift to a new or existing table. Here's how the upload process works: 1. PyArrow Table is serialized into a Parquet format on local disk 2. The Parquet file is uploaded to S3 3. The S3 file is uploaded to Redshift as a new table through COPY
(
table: Union[pyarrow.Table, Path],
redshift_data_client,
cluster_id: Optional[str],
workgroup: Optional[str],
database: str,
user: Optional[str],
s3_resource,
iam_role: str,
s3_path: str,
table_name: str,
schema: Optional[pyarrow.Schema] = None,
fail_if_exists: bool = True,
)
| 288 | |
| 289 | |
| 290 | def upload_arrow_table_to_redshift( |
| 291 | table: Union[pyarrow.Table, Path], |
| 292 | redshift_data_client, |
| 293 | cluster_id: Optional[str], |
| 294 | workgroup: Optional[str], |
| 295 | database: str, |
| 296 | user: Optional[str], |
| 297 | s3_resource, |
| 298 | iam_role: str, |
| 299 | s3_path: str, |
| 300 | table_name: str, |
| 301 | schema: Optional[pyarrow.Schema] = None, |
| 302 | fail_if_exists: bool = True, |
| 303 | ): |
| 304 | """Uploads an Arrow Table to Redshift to a new or existing table. |
| 305 | |
| 306 | Here's how the upload process works: |
| 307 | 1. PyArrow Table is serialized into a Parquet format on local disk |
| 308 | 2. The Parquet file is uploaded to S3 |
| 309 | 3. The S3 file is uploaded to Redshift as a new table through COPY command |
| 310 | 4. The local disk & s3 paths are cleaned up |
| 311 | |
| 312 | Args: |
| 313 | redshift_data_client: Redshift Data API Service client |
| 314 | cluster_id: Redshift Cluster Identifier |
| 315 | workgroup: Redshift Serverless Workgroup |
| 316 | database: Redshift Database Name |
| 317 | user: Redshift username |
| 318 | s3_resource: S3 Resource object |
| 319 | s3_path: S3 path where the Parquet file is temporarily uploaded |
| 320 | iam_role: IAM Role for Redshift to assume during the COPY command. |
| 321 | The role must grant permission to read the S3 location. |
| 322 | table_name: The name of the new Redshift table where we copy the dataframe |
| 323 | table: The Arrow Table or Path to parquet dataset to upload |
| 324 | schema: (Optionally) client may provide arrow Schema which will be converted into redshift table schema |
| 325 | fail_if_exists: fail if table with such name exists or append data to existing table |
| 326 | |
| 327 | Raises: |
| 328 | RedshiftTableNameTooLong: The specified table name is too long. |
| 329 | """ |
| 330 | if len(table_name) > REDSHIFT_TABLE_NAME_MAX_LENGTH: |
| 331 | raise RedshiftTableNameTooLong(table_name) |
| 332 | |
| 333 | if isinstance(table, pyarrow.Table) and not schema: |
| 334 | schema = table.schema |
| 335 | |
| 336 | if not schema: |
| 337 | raise ValueError("Schema must be specified when data is passed as a Path") |
| 338 | |
| 339 | bucket, key = get_bucket_and_key(s3_path) |
| 340 | |
| 341 | column_query_list = ", ".join( |
| 342 | [f"{field.name} {pa_to_redshift_value_type(field.type)}" for field in schema] |
| 343 | ) |
| 344 | |
| 345 | uploaded_files = [] |
| 346 | |
| 347 | if isinstance(table, Path): |
no test coverage detected