|
| 1 | +"""Amazon S3 Read Delta Lake Module (PRIVATE).""" |
| 2 | +import importlib.util |
| 3 | +from typing import Any, Dict, List, Optional, Tuple |
| 4 | + |
| 5 | +import boto3 |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | +from awswrangler import _utils |
| 9 | + |
| 10 | +_deltalake_found = importlib.util.find_spec("deltalake") |
| 11 | +if _deltalake_found: |
| 12 | + from deltalake import DeltaTable # pylint: disable=import-error |
| 13 | + |
| 14 | + |
| 15 | +def _set_default_storage_options_kwargs( |
| 16 | + session: boto3.Session, s3_additional_kwargs: Optional[Dict[str, Any]] |
| 17 | +) -> Dict[str, Any]: |
| 18 | + defaults = {key.upper(): value for key, value in _utils.boto3_to_primitives(boto3_session=session).items()} |
| 19 | + s3_additional_kwargs = s3_additional_kwargs or {} |
| 20 | + return { |
| 21 | + **defaults, |
| 22 | + **s3_additional_kwargs, |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | +def read_deltalake( |
| 27 | + path: Optional[str] = None, |
| 28 | + version: Optional[int] = None, |
| 29 | + partitions: Optional[List[Tuple[str, str, Any]]] = None, |
| 30 | + columns: Optional[List[str]] = None, |
| 31 | + without_files: bool = False, |
| 32 | + boto3_session: Optional[boto3.Session] = None, |
| 33 | + s3_additional_kwargs: Optional[Dict[str, str]] = None, |
| 34 | + pyarrow_additional_kwargs: Optional[Dict[str, Any]] = None, |
| 35 | +) -> pd.DataFrame: |
| 36 | + """Load a Deltalake table data from an S3 path. |
| 37 | +
|
| 38 | + This function requires the `deltalake package |
| 39 | + <https://delta-io.github.io/delta-rs/python>`__. |
| 40 | + See the `How to load a Delta table |
| 41 | + <https://delta-io.github.io/delta-rs/python/usage.html#loading-a-delta-table>`__ |
| 42 | + guide for loading instructions. |
| 43 | +
|
| 44 | + Parameters |
| 45 | + ---------- |
| 46 | + path: Optional[str] |
| 47 | + The path of the DeltaTable. |
| 48 | + version: Optional[int] |
| 49 | + The version of the DeltaTable. |
| 50 | + partitions: Optional[List[Tuple[str, str, Any]] |
| 51 | + A list of partition filters, see help(DeltaTable.files_by_partitions) |
| 52 | + for filter syntax. |
| 53 | + columns: Optional[List[str]] |
| 54 | + The columns to project. This can be a list of column names to include |
| 55 | + (order and duplicates are preserved). |
| 56 | + without_files: bool |
| 57 | + If True, load the table without tracking files (memory-friendly). |
| 58 | + Some append-only applications might not need to track files. |
| 59 | + boto3_session: Optional[boto3.Session()] |
| 60 | + Boto3 Session. If None, the default boto3 session is used. |
| 61 | + s3_additional_kwargs: Optional[Dict[str, str]] |
| 62 | + Forwarded to the Delta Table class for the storage options of the S3 backend. |
| 63 | + pyarrow_additional_kwargs: Optional[Dict[str, str]] |
| 64 | + Forwarded to the PyArrow to_pandas method. |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + df: pd.DataFrame |
| 69 | + DataFrame with the results. |
| 70 | +
|
| 71 | + See Also |
| 72 | + -------- |
| 73 | + deltalake.DeltaTable : Create a DeltaTable instance with the deltalake library. |
| 74 | + """ |
| 75 | + pyarrow_additional_kwargs = pyarrow_additional_kwargs or {} # TODO: Use defaults in 3.0.0 # pylint: disable=fixme |
| 76 | + storage_options = _set_default_storage_options_kwargs(_utils.ensure_session(boto3_session), s3_additional_kwargs) |
| 77 | + |
| 78 | + return ( |
| 79 | + DeltaTable( |
| 80 | + table_uri=path, |
| 81 | + version=version, |
| 82 | + storage_options=storage_options, |
| 83 | + without_files=without_files, |
| 84 | + ) |
| 85 | + .to_pyarrow_table(partitions=partitions, columns=columns) |
| 86 | + .to_pandas(**pyarrow_additional_kwargs) |
| 87 | + ) |
0 commit comments