Args: df: Dataframe we'd like to write back. cursor: cursor to be used to communicate with Snowflake. stage_name: stage name in Snowflake connection. chunk_size: Number of elements to be inserted once, if not provided all elements will be dumped once
(
df: pd.DataFrame,
cursor: SnowflakeCursor,
stage_name: str,
chunk_size: Optional[int] = None,
parallel: int = 4,
compression: str = "gzip",
)
| 386 | |
| 387 | |
| 388 | def upload_df( |
| 389 | df: pd.DataFrame, |
| 390 | cursor: SnowflakeCursor, |
| 391 | stage_name: str, |
| 392 | chunk_size: Optional[int] = None, |
| 393 | parallel: int = 4, |
| 394 | compression: str = "gzip", |
| 395 | ): |
| 396 | """ |
| 397 | Args: |
| 398 | df: Dataframe we'd like to write back. |
| 399 | cursor: cursor to be used to communicate with Snowflake. |
| 400 | stage_name: stage name in Snowflake connection. |
| 401 | chunk_size: Number of elements to be inserted once, if not provided all elements will be dumped once |
| 402 | (Default value = None). |
| 403 | parallel: Number of threads to be used when uploading chunks, default follows documentation at: |
| 404 | https://docs.snowflake.com/en/sql-reference/sql/put.html#optional-parameters (Default value = 4). |
| 405 | compression: The compression used on the Parquet files, can only be gzip, or snappy. Gzip gives supposedly a |
| 406 | better compression, while snappy is faster. Use whichever is more appropriate (Default value = 'gzip'). |
| 407 | |
| 408 | """ |
| 409 | if chunk_size is None: |
| 410 | chunk_size = len(df) |
| 411 | |
| 412 | with TemporaryDirectory() as tmp_folder: |
| 413 | for i, chunk in chunk_helper(df, chunk_size): |
| 414 | chunk_path = os.path.join(tmp_folder, "file{}.txt".format(i)) |
| 415 | # Dump chunk into parquet file |
| 416 | chunk.to_parquet( |
| 417 | chunk_path, |
| 418 | compression=compression, |
| 419 | use_deprecated_int96_timestamps=True, |
| 420 | ) |
| 421 | # Upload parquet file |
| 422 | upload_sql = ( |
| 423 | "PUT /* Python:feast.infra.utils.snowflake_utils.upload_df() */ " |
| 424 | "'file://{path}' @\"{stage_name}\" PARALLEL={parallel}" |
| 425 | ).format( |
| 426 | path=chunk_path.replace("\\", "\\\\").replace("'", "\\'"), |
| 427 | stage_name=stage_name, |
| 428 | parallel=parallel, |
| 429 | ) |
| 430 | logger.debug(f"uploading files with '{upload_sql}'") |
| 431 | cursor.execute(upload_sql, _is_internal=True) |
| 432 | # Remove chunk file |
| 433 | os.remove(chunk_path) |
| 434 | |
| 435 | |
| 436 | def upload_local_pq( |
no test coverage detected