Metadata-Version: 2.1
Name: lakehouse-engine
Version: 1.18.0.post1
Summary: A Spark framework serving as the engine for several lakehouse algorithms and data flows.
Home-page: https://adidas.github.io/lakehouse-engine-docs/lakehouse_engine.html
Author: Adidas Lakehouse Foundations Team
Author-email: software.engineering@adidas.com
Project-URL: Source Code, https://adidas.github.io/lakehouse-engine
Project-URL: Documentation, https://adidas.github.io/lakehouse-engine-docs/lakehouse_engine.html
Project-URL: Issues, https://github.com/adidas/lakehouse-engine/issues
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python :: 3
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Other Audience
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Software Development
Classifier: License :: OSI Approved :: Apache Software License
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Requires-Dist: aiohttp ==3.8.6
Requires-Dist: aiosignal ==1.3.1
Requires-Dist: altair ==4.2.2
Requires-Dist: anyio ==4.0.0
Requires-Dist: argon2-cffi ==23.1.0
Requires-Dist: argon2-cffi-bindings ==21.2.0
Requires-Dist: arrow ==1.3.0
Requires-Dist: asttokens ==2.4.0
Requires-Dist: async-lru ==2.0.4
Requires-Dist: async-timeout ==4.0.3
Requires-Dist: attrs ==23.1.0
Requires-Dist: azure-core ==1.29.4
Requires-Dist: azure-identity ==1.14.1
Requires-Dist: babel ==2.13.0
Requires-Dist: backcall ==0.2.0
Requires-Dist: bcrypt ==4.0.1
Requires-Dist: beautifulsoup4 ==4.12.2
Requires-Dist: bleach ==6.1.0
Requires-Dist: boto3 ==1.21.32
Requires-Dist: botocore ==1.24.46
Requires-Dist: certifi ==2023.7.22
Requires-Dist: cffi ==1.16.0
Requires-Dist: charset-normalizer ==3.3.0
Requires-Dist: click ==8.1.7
Requires-Dist: colorama ==0.4.6
Requires-Dist: comm ==0.1.4
Requires-Dist: cryptography ==41.0.4
Requires-Dist: cycler ==0.12.1
Requires-Dist: dacite ==1.8.1
Requires-Dist: debugpy ==1.8.0
Requires-Dist: decorator ==5.1.1
Requires-Dist: defusedxml ==0.7.1
Requires-Dist: delta-spark ==2.4.0
Requires-Dist: deprecated ==1.2.14
Requires-Dist: entrypoints ==0.4
Requires-Dist: exceptiongroup ==1.1.3
Requires-Dist: executing ==2.0.0
Requires-Dist: fastjsonschema ==2.18.1
Requires-Dist: fonttools ==4.43.1
Requires-Dist: fqdn ==1.5.1
Requires-Dist: frozenlist ==1.4.0
Requires-Dist: great-expectations ==0.17.11
Requires-Dist: h11 ==0.14.0
Requires-Dist: h2 ==4.1.0
Requires-Dist: hpack ==4.0.0
Requires-Dist: htmlmin ==0.1.12
Requires-Dist: httpcore ==0.18.0
Requires-Dist: httpx[http2] ==0.25.0
Requires-Dist: hyperframe ==6.0.1
Requires-Dist: idna ==3.4
Requires-Dist: imagehash ==4.3.1
Requires-Dist: importlib-metadata ==6.8.0
Requires-Dist: ipykernel ==6.25.2
Requires-Dist: ipython ==8.16.1
Requires-Dist: ipywidgets ==8.1.1
Requires-Dist: isoduration ==20.11.0
Requires-Dist: jedi ==0.19.1
Requires-Dist: jinja2 ==3.0.3
Requires-Dist: jmespath ==1.0.1
Requires-Dist: joblib ==1.3.2
Requires-Dist: json5 ==0.9.14
Requires-Dist: jsonpatch ==1.33
Requires-Dist: jsonpointer ==2.4
Requires-Dist: jsonschema[format-nongpl] ==4.19.1
Requires-Dist: jsonschema-specifications ==2023.7.1
Requires-Dist: jupyter-client ==8.4.0
Requires-Dist: jupyter-core ==5.4.0
Requires-Dist: jupyter-events ==0.8.0
Requires-Dist: jupyter-lsp ==2.2.0
Requires-Dist: jupyter-server ==2.8.0
Requires-Dist: jupyter-server-terminals ==0.4.4
Requires-Dist: jupyterlab ==4.0.7
Requires-Dist: jupyterlab-pygments ==0.2.2
Requires-Dist: jupyterlab-server ==2.25.0
Requires-Dist: jupyterlab-widgets ==3.0.9
Requires-Dist: kiwisolver ==1.4.5
Requires-Dist: llvmlite ==0.41.0
Requires-Dist: makefun ==1.15.1
Requires-Dist: markupsafe ==2.1.3
Requires-Dist: marshmallow ==3.20.1
Requires-Dist: matplotlib ==3.5.3
Requires-Dist: matplotlib-inline ==0.1.6
Requires-Dist: microsoft-kiota-abstractions ==0.8.7
Requires-Dist: microsoft-kiota-authentication-azure ==0.3.0
Requires-Dist: microsoft-kiota-http ==0.6.1
Requires-Dist: microsoft-kiota-serialization-json ==0.4.2
Requires-Dist: microsoft-kiota-serialization-text ==0.2.1
Requires-Dist: mistune ==3.0.2
Requires-Dist: msal ==1.24.1
Requires-Dist: msal-extensions ==1.0.0
Requires-Dist: msgraph-core ==1.0.0a4
Requires-Dist: msgraph-sdk ==1.0.0a13
Requires-Dist: multidict ==6.0.4
Requires-Dist: multimethod ==1.10
Requires-Dist: nbclient ==0.8.0
Requires-Dist: nbconvert ==7.9.2
Requires-Dist: nbformat ==5.9.2
Requires-Dist: nest-asyncio ==1.5.6
Requires-Dist: networkx ==3.1
Requires-Dist: notebook ==7.0.5
Requires-Dist: notebook-shim ==0.2.3
Requires-Dist: numba ==0.58.0
Requires-Dist: numpy ==1.23.5
Requires-Dist: opentelemetry-api ==1.20.0
Requires-Dist: opentelemetry-sdk ==1.20.0
Requires-Dist: opentelemetry-semantic-conventions ==0.41b0
Requires-Dist: overrides ==7.4.0
Requires-Dist: packaging ==23.2
Requires-Dist: pandas ==2.0.3
Requires-Dist: pandocfilters ==1.5.0
Requires-Dist: paramiko ==3.4.0
Requires-Dist: parso ==0.8.3
Requires-Dist: patsy ==0.5.3
Requires-Dist: pendulum ==2.1.2
Requires-Dist: pexpect ==4.8.0
Requires-Dist: phik ==0.12.3
Requires-Dist: pickleshare ==0.7.5
Requires-Dist: pillow ==10.1.0
Requires-Dist: platformdirs ==3.11.0
Requires-Dist: portalocker ==2.8.2
Requires-Dist: prometheus-client ==0.17.1
Requires-Dist: prompt-toolkit ==3.0.39
Requires-Dist: psutil ==5.9.6
Requires-Dist: ptyprocess ==0.7.0
Requires-Dist: pure-eval ==0.2.2
Requires-Dist: py4j ==0.10.9.7
Requires-Dist: pycparser ==2.21
Requires-Dist: pydantic ==1.10.13
Requires-Dist: pygments ==2.16.1
Requires-Dist: pyjwt[crypto] ==2.8.0
Requires-Dist: pynacl ==1.5.0
Requires-Dist: pyparsing ==3.1.1
Requires-Dist: pyspark ==3.4.1
Requires-Dist: python-dateutil ==2.8.2
Requires-Dist: python-json-logger ==2.0.7
Requires-Dist: pytz ==2023.3.post1
Requires-Dist: pytzdata ==2020.1
Requires-Dist: pywavelets ==1.4.1
Requires-Dist: pyyaml ==6.0.1
Requires-Dist: pyzmq ==25.1.1
Requires-Dist: referencing ==0.30.2
Requires-Dist: requests ==2.31.0
Requires-Dist: rfc3339-validator ==0.1.4
Requires-Dist: rfc3986-validator ==0.1.1
Requires-Dist: rpds-py ==0.10.6
Requires-Dist: ruamel-yaml ==0.17.17
Requires-Dist: ruamel-yaml-clib ==0.2.8
Requires-Dist: s3transfer ==0.5.2
Requires-Dist: scipy ==1.11.3
Requires-Dist: seaborn ==0.12.2
Requires-Dist: send2trash ==1.8.2
Requires-Dist: six ==1.16.0
Requires-Dist: sniffio ==1.3.0
Requires-Dist: soupsieve ==2.5
Requires-Dist: stack-data ==0.6.3
Requires-Dist: statsmodels ==0.14.0
Requires-Dist: std-uritemplate ==0.0.43
Requires-Dist: tangled-up-in-unicode ==0.2.0
Requires-Dist: terminado ==0.17.1
Requires-Dist: tinycss2 ==1.2.1
Requires-Dist: tomli ==2.0.1
Requires-Dist: toolz ==0.12.0
Requires-Dist: tornado ==6.3.3
Requires-Dist: tqdm ==4.66.1
Requires-Dist: traitlets ==5.11.2
Requires-Dist: typeguard ==4.1.5
Requires-Dist: types-cryptography ==3.3.23.2
Requires-Dist: types-paramiko ==2.12.0.0
Requires-Dist: types-python-dateutil ==2.8.19.14
Requires-Dist: typing-extensions ==4.8.0
Requires-Dist: tzdata ==2023.3
Requires-Dist: tzlocal ==5.1
Requires-Dist: uri-template ==1.3.0
Requires-Dist: urllib3 ==1.26.17
Requires-Dist: visions[type-image-path] ==0.7.5
Requires-Dist: wcwidth ==0.2.8
Requires-Dist: webcolors ==1.13
Requires-Dist: webencodings ==0.5.1
Requires-Dist: websocket-client ==1.6.4
Requires-Dist: widgetsnbextension ==4.0.9
Requires-Dist: wordcloud ==1.9.2
Requires-Dist: wrapt ==1.15.0
Requires-Dist: yarl ==1.9.2
Requires-Dist: ydata-profiling ==4.6.0
Requires-Dist: zipp ==3.17.0

<img align="right" src="assets/img/lakehouse_engine_logo_no_bg_160.png" alt="Lakehouse Engine Logo">

# Lakehouse Engine
A configuration driven Spark framework, written in Python, serving as a scalable and distributed engine for several lakehouse algorithms, data flows and utilities for Data Products.
> **Note:** whenever you read Data Product or Data Product team, we want to refer to Teams and use cases, whose main focus is on 
leveraging the power of data, on a particular topic, end-to-end (ingestion, consumption...) to achieve insights, supporting faster and better decisions, 
which generate value for their businesses. These Teams should not be focusing on building reusable frameworks, but on re-using the existing frameworks to achieve their goals.

## Main Goals
The goal of the Lakehouse Engine is to bring some advantages, such as:
- offer cutting-edge, standard, governed and battle-tested foundations that several Data Product teams can benefit from;
- avoid that Data Product teams develop siloed solutions, reducing technical debts and high operating costs (redundant developments across teams);
- allow Data Product teams to focus mostly on data-related tasks, avoiding wasting time & resources on developing the same code for different use cases;
- benefit from the fact that many teams are reusing the same code, which increases the likelihood that common issues are surfaced and solved faster;
- decrease the dependency and learning curve to Spark and other technologies that the Lakehouse Engine abstracts;
- speed up repetitive tasks;
- reduced vendor lock-in.
  > **Note**: even though you will see a focus on AWS and Databricks, this is just due to the lack of use cases for other technologies like GCP and Azure, but we are open for contribution.

## Key Features
⭐ **Data Loads:** perform data loads from disparate source types and apply transformations and data quality validations, 
ensuring trustworthy data, before integrating it into disparate target types. Additionally, people can also define termination 
actions like optimisations or notifications. [On the usage section](#load-data-usage-example) you will find an example using all the supported keywords for data loads.
> **Note:** The Lakehouse 
Engine supports different types of sources and targets, such as, kafka, jdbc, dataframes, files (csv, parquet, json, delta...), sftp, sap bw, sap b4...

⭐ **Transformations:** configuration driven transformations without the need to write any code. Transformations can be applied by using the `transform_specs` in the Data Loads.
> **Note:** you can search all the available transformations, as well as checking implementation details and examples [here](https://adidas.github.io/lakehouse-engine-docs/lakehouse_engine/transformers.html).

⭐ **Data Quality Validations:** the Lakehouse Engine uses Great Expectations as a backend and abstracts any implementation
details by offering people the capability to specify what validations to apply on the data, solely using dict/json based configurations.
The Data Quality validations can be applied on:
- post-mortem (static) data, using the DQ Validator algorithm (`execute_dq_validation`)
- data in-motion, using the `dq_specs` keyword in the Data Loads, to add it as one more step while loading data. 
[On the usage section](#usage) you will find an example using this type of Data Quality validations.

⭐ **Reconciliation:** useful algorithm two compare two source of data, by defining one version of the `truth` to compare
against the `current` version of the data. It can be particularly useful during migrations phases, two compare a few KPIs
and ensure the new version of a table (`current`), for example, delivers the same vision of the data as the old one (`truth`).
Find usage examples [here](tests/feature/test_reconciliation.py).

⭐ **Sensors:** an abstraction to otherwise complex spark code that can be executed in very small single-node clusters 
to check if an upstream system or Data Product contains new data since the last execution. With this feature, people can 
trigger jobs to run in more frequent intervals and if the upstream does not contain new data, then the rest of the job 
exits without creating bigger clusters to execute more intensive data ETL (Extraction, Transformation, and Loading).
Find usage examples [here](tests/feature/test_sensors.py).

⭐ **Terminators:** this feature allow people to specify what to do as a last action, before finishing a Data Load. 
Some examples of actions are: optimising target table, vacuum, compute stats, expose change data feed to external location 
or even send e-mail notifications. Thus, it is specifically used in Data Loads, using the `terminate_specs` keyword.
[On the usage section](#usage) you will find an example using terminators.

⭐ **Table Manager:** function `manage_table`, offers a set of actions to manipulate tables/views in several ways, such as:
- compute table statistics;
- create/drop tables and views;
- delete/truncate/repair tables;
- vacuum delta tables or locations;
- optimize table;
- describe table;
- show table properties;
- execute sql.

⭐ **File Manager:** function `manage_files`, offers a set of actions to manipulate files in several ways, such as:
- delete Objects in S3;
- copy Objects in S3;
- restore Objects from S3 Glacier;
- check the status of a restore from S3 Glacier;
- request a restore of objects from S3 Glacier and wait for them to be copied to a destination.


⭐ **Notifications:** you can configure and send email notifications.
> **Note:** it can be used as an independent function (`send_notification`) or as a `terminator_spec`, using the function `notify`.

📖 In case you want to check further details you can check the documentation of the [Lakehouse Engine facade](https://adidas.github.io/lakehouse-engine-docs/lakehouse_engine/engine.html).

## Installation
As the Lakehouse Engine is built as wheel (look into our **build** and **deploy** make targets) you can install it as any other python package using **pip**.

```
pip install lakehouse_engine
```

Alternatively, you can also upload the wheel to any target of your like (e.g. S3) and perform a pip installation pointing to that target location.

> **Note:** It is advisable for a Data Product to pin a specific version of the Lakehouse Engine (and have recurring upgrading activities)
to avoid breaking changes in a new release.
In case you don't want to be so conservative, you can pin to a major version, which usually shouldn't include changes that break backwards compatibility.

## How Data Products use the Lakehouse Engine Framework?
![Lakehouse Data Product](assets/img/lakehouse_dp_usage.drawio.png)

The Lakehouse Engine is a configuration-first Data Engineering framework, using the concept of ACONs to configure algorithms. 
An ACON, stands for Algorithm Configuration and is a JSON representation, as the [Load Data Usage Example](#load-data-usage-example) demonstrates. 

Below you find described the main keywords you can use to configure and ACON for a Data Load.
> **Note:** the usage logic for the other [algorithms/features presented](#key-features) will always be similar, but using different keywords, 
which you can search for in the examples and documentation provided in the [Key Features](#key-features) and [Community Support and Contributing](#community-support-and-contributing) sections.

- **Input specifications (input_specs):** specify how to read data. This is a **mandatory** keyword; 
- **Transform specifications (transform_specs):** specify how to transform data; 
- **Data quality specifications (dq_specs):** specify how to execute the data quality process. 
- **Output specifications (output_specs):** specify how to write data to the target. This is a **mandatory** keyword; 
- **Terminate specifications (terminate_specs):** specify what to do after writing into the target (e.g., optimising target table, vacuum, compute stats, expose change data feed to external location, etc). 
- **Execution environment (exec_env):** custom Spark session configurations to be provided for your algorithm (configurations can also be provided from your job/cluster configuration, which we highly advise you to do instead of passing performance related configs here for example).

## Load Data Usage Example

You can use the Lakehouse Engine in a **pyspark script** or **notebook**.
Below you can find an example on how to execute a Data Load using the Lakehouse Engine, which is doing the following:
1. Read CSV files, from a specified location, in a streaming fashion and providing a specific schema and some additional 
options for properly read the files (e.g. header, delimiter...);
2. Apply two transformations on the input data:
   1. Add a new column having the Row ID;
   2. Add a new column `extraction_date`, which extracts the date from the `lhe_extraction_filepath`, based on a regex.
3. Apply Data Quality validations and store the result of their execution in the table `your_database.order_events_dq_checks`:
   1. Check if the column `omnihub_locale_code` is not having null values;
   2. Check if the distinct value count for the column `product_division` is between 10 and 100;
   3. Check if the max of the column `so_net_value` is between 10 and 1000;
   4. Check if the length of the values in the column `omnihub_locale_code` is between 1 and 10;
   5. Check if the mean of the values for the column `coupon_code` is between 15 and 20.
4. Write the output into the table `your_database.order_events_with_dq` in a delta format, partitioned by `order_date_header`
and applying a merge predicate condition, ensuring the data is only inserted into the table if it does not match the predicate
(meaning the data is not yet available in the table). Moreover, the `insert_only` flag is used to specify that there should not 
be any updates or deletes in the target table, only inserts;
5. Optimize the Delta Table that we just wrote in (e.g. z-ordering);
6. Specify 3 custom Spark Session configurations.
> ⚠️ **Note:** `spec_id` is one of the main concepts to ensure you can chain the steps of the algorithm,
so, for example, you can specify the transformations (in `transform_specs`) of a DataFrame that was read in the `input_specs`.

```python
from lakehouse_engine.engine import load_data

acon = {
    "input_specs": [
        {
            "spec_id": "orders_bronze",
            "read_type": "streaming",
            "data_format": "csv",
            "schema_path": "s3://my-data-product-bucket/artefacts/metadata/bronze/schemas/orders.json",
            "with_filepath": True,
            "options": {
                "badRecordsPath": "s3://my-data-product-bucket/badrecords/order_events_with_dq/",
                "header": False,
                "delimiter": "\u005E",
                "dateFormat": "yyyyMMdd",
            },
            "location": "s3://my-data-product-bucket/bronze/orders/",
        }
    ],
    "transform_specs": [
        {
            "spec_id": "orders_bronze_with_extraction_date",
            "input_id": "orders_bronze",
            "transformers": [
                {"function": "with_row_id"},
                {
                    "function": "with_regex_value",
                    "args": {
                        "input_col": "lhe_extraction_filepath",
                        "output_col": "extraction_date",
                        "drop_input_col": True,
                        "regex": ".*WE_SO_SCL_(\\d+).csv",
                    },
                },
            ],
        }
    ],
    "dq_specs": [
        {
            "spec_id": "check_orders_bronze_with_extraction_date",
            "input_id": "orders_bronze_with_extraction_date",
            "dq_type": "validator",
            "result_sink_db_table": "your_database.order_events_dq_checks",
            "fail_on_error": False,
            "dq_functions": [
                {
                  "dq_function": "expect_column_values_to_not_be_null", 
                  "args": {
                    "column": "omnihub_locale_code"
                  }
                },
                {
                    "dq_function": "expect_column_unique_value_count_to_be_between",
                    "args": {
                      "column": "product_division", 
                      "min_value": 10,
                      "max_value": 100
                    },
                },
                {
                    "dq_function": "expect_column_max_to_be_between", 
                    "args": {
                      "column": "so_net_value", 
                      "min_value": 10, 
                      "max_value": 1000
                    }
                },
                {
                    "dq_function": "expect_column_value_lengths_to_be_between",
                    "args": {
                      "column": "omnihub_locale_code", 
                      "min_value": 1, 
                      "max_value": 10
                    },
                },
                {
                  "dq_function": "expect_column_mean_to_be_between", 
                  "args": {
                    "column": "coupon_code", 
                    "min_value": 15, 
                    "max_value": 20
                  }
                },
            ],
        },
    ],
    "output_specs": [
        {
            "spec_id": "orders_silver",
            "input_id": "check_orders_bronze_with_extraction_date",
            "data_format": "delta",
            "write_type": "merge",
            "partitions": ["order_date_header"],
            "merge_opts": {
                "merge_predicate": """
                    new.sales_order_header = current.sales_order_header
                    AND new.sales_order_schedule = current.sales_order_schedule
                    AND new.sales_order_item=current.sales_order_item
                    AND new.epoch_status=current.epoch_status
                    AND new.changed_on=current.changed_on
                    AND new.extraction_date=current.extraction_date
                    AND new.lhe_batch_id=current.lhe_batch_id
                    AND new.lhe_row_id=current.lhe_row_id
                """,
                "insert_only": True,
            },
            "db_table": "your_database.order_events_with_dq",
            "options": {
                "checkpointLocation": "s3://my-data-product-bucket/checkpoints/template_order_events_with_dq/"
            },
        }
    ],
    "terminate_specs": [
        {
            "function": "optimize_dataset",
            "args": {
              "db_table": "your_database.order_events_with_dq"
            }
        }
    ],
    "exec_env": {
        "spark.databricks.delta.schema.autoMerge.enabled": True,
        "spark.databricks.delta.optimizeWrite.enabled": True,
        "spark.databricks.delta.autoCompact.enabled": True,
    },
}

load_data(acon=acon)
```

> **Note:** Although it is possible to interact with the Lakehouse Engine functions directly from your python code, 
instead of relying on creating an ACON dict and use the engine api, we do not ensure the stability across new 
Lakehouse Engine releases when calling internal functions (not exposed in the facade) directly.

> **Note:** ACON structure might change across releases, please test your Data Product first before updating to a 
new version of the Lakehouse Engine in your Production environment.

## Who maintains the Lakehouse Engine?
The Lakehouse Engine is under active development and production usage by the Adidas Lakehouse Foundations Engineering team. 

## Community Support and Contributing

🤝 Do you want to contribute or need any support? Check out all the details in [CONTRIBUTING.md](CONTRIBUTING.md).

## License and Software Information

© adidas AG

adidas AG publishes this software and accompanied documentation (if any) subject to the terms of the [license](LICENSE.txt) 
with the aim of helping the community with our tools and libraries which we think can be also useful for other people. 
You will find a copy of the [license](LICENSE.txt) in the root folder of this package. All rights not explicitly granted 
to you under the [license](LICENSE.txt) remain the sole and exclusive property of adidas AG.

NOTICE: The software has been designed solely for the purposes described in this ReadMe file. The software is NOT designed, 
tested or verified for productive use whatsoever, nor or for any use related to high risk environments, such as health care, 
highly or fully autonomous driving, power plants, or other critical infrastructures or services.

If you want to contact adidas regarding the software, you can mail us at _software.engineering@adidas.com_.

For further information open the [adidas terms and conditions](https://github.com/adidas/adidas-contribution-guidelines/wiki/Terms-and-conditions) page.
