Package: feathrpiper
Feathr Online Transformation Python SupportThis is the Python wrapper of the Feathr online transformation service.There are 2 major classes in this package:PiperService, this is the service class, it is used to start a HTTP service to handle the transformation requests. It doesn't support HTTPS and authentication, so you may need to setup gateway or proxy to handle the security issues.
ThePiperServiceclass has astartmethod to start the HTTP service in blocking mode, andstart_asyncmethod to start the service in theasynccontext.Piper, this is the transformation engine, it can be use to transform data directly, mainly for development and testing purpose.
ThePiperclass has aprocessmethod to transform data in blocking mode, andprocess_asyncmethod to transform data in theasynccontext.Both above classes supportUDFandUDLFwritten in Python.NOTE: Because of the GIL, pure Python code cannot run concurrently, that means using Python UDF could slow down the transformation service, especially on heavy load.Value TypesAll values passed to the pipeline must be in one of the following types:NoneSimple types:bool,int,float,strDate/time is represented asdatetime.DateTime.List: List of supported types.Map: Map of supported types, keys must be string, and value can be any supported type.All values returned by the pipeline will also be in above types.NOTE: When using Python big integer, exception will be thrown if any it exceeds the range of 64-bit signed integer.User Defined Function (UDF) in PythonThe UDF is implemented as ordinary Python function, and it must be registered to the service before it can be used in the pipeline.The UDF function can only accept positional arguments, keyword arguments are not supported.The UDF function must be able to be invoked by the usage in the DSL script, i.e. a UDF with 2 fixed arguments and 1 optional argument can be invoked asudf(1, 2)orudf(1, 2, 3), but notudf(1, 2, 3, 4)orudf(1).The UDF function may raise any exception, and the returned value will be record as an error. This error will be propagated to the caller.Every function and operator that takes error as the input will return the error.At the final output stage of the pipeline, the error value will be converted toNone, and the error will be recorded in a separated error list.The UDF function will never see the error as the input, the invocation is bypassed before the UDF function is called if any of the argument is error.The execution order is non-deterministic, so the UDF function shall not make any assumptions.The UDF function should not block, such behavior is not strictly forbidden but the performance will be impacted significantly.User Defined Lookup Function (UDLF) in PythonUsuallylookupis to fetch external data, such as a database or a web service, so the lookup data source is implemented as a Python async functions, and it must be registered to the piper or the service before it can be used in the pipeline.The lookup function is called with a single key and a list of requested field names, and it should return a list of rows that each row is a list that aligns with the requested fields, or an empty list when lookup failed. The key must be in the supported simple types, list and dict cannot be used as key, and usingNoneas the key will getNoneas the value of all returned fields without actually calling the lookup function.async def my_fancy_lookup_function(key: Any, fields: List[str]) -> List[List[Any]]:
    ...
    return [
        [some_data[f] for f in fields],
        [some_other_data[f] for f in fields],
    ]The lookup function must be added to thePiperorPiperServicebefore it can be used in the pipeline:piper = Piper(pipeline_def, {"lookup_name": my_fancy_lookup_function}, ...)orsvc = PiperService(pipeline_def, {"lookup_name": my_fancy_lookup_function}, ...)Then you can use the lookup data source in the pipeline in alookuptransformation:pipeline_name(...)
| ...
| lookup field1, field2 from lookup_name on key
| ...
;or ajointransformation:pipeline_name(...)
| ...
| join kind=left-inner field1, field2 from lookup_name on key
| ...
;Once the user-defined lookup function is used, thePiperandPiperServicemust be used inasynccontext, otherwise all async function will never be executed and the program may hang forever.
Also you need to replaceprocesswithprocess_async, andstartwithstart_async.piper = Piper(pipeline_def, {"lookup_name": lookup_function})

async def test():
    await piper.process_async(...)

asyncio.run(test())For more information about Python async programming, please refer toPython Asyncio.NOTE:Because of the asynchronous nature of the lookup function, it's recommended to useasynciocompatible libraries to implement the lookup function, traditional blocking libraries may cause the performance issue, e.g. useaiohttporHTTPXinstead ofRequests.This package only supportsasyncio,TwistedorGeventbased libraries are not supported.In order to lookup data from a standard JSON-based HTTP API, you can use builtin HTTP client instead of implementing your own lookup function, register the lookup data source either in a JSON string or adictwith correct content, detailed doc is athere.Thefeathrpiperalso has builtin support of SqlServer/AzureSQL, Sqlite3, and Azure CosmosDb.Integration with Other Web-Service FrameworksThefeathrpipercontains built-in web service, but it doesn't support HTTPS and authentication, and has a specific HTTP API spec which cannot be changed from the Python side. In case you need to use it in any other scenario, you may integrate it with other Web service frameworks.Flask: prefer to use async version of Flask, such asFlask-Async,Flask-RESTful-Async,Flask-RESTX-Async, etc. And you should useprocess_asyncto process the request.FastAPI: FastAPI is fully async-based, useprocess_asyncto process the request.Any other Web framework that doesn't support async: You can useprocessin non-async context, but the user-defined lookup function feature will be unavailable.A demo of integrating with FastAPI is atherePackaging and DeploymentThefeathrpiperpackage is a standard Python package without external dependency, you need to write your own code using the package to implement your own transformation service.The packaging and the deployment process is also standard, refer tothe official documentif you need to build Docker image, currently we don't have any pre-built Docker image for the Python package.In most cases, the packaging process could be like:Prepare therequirements.txtfile which includes thefeathrpiperpackage and all the other dependencies.# This package
feathrpiper >= 0.4.3
# Any other dependencies
pandas == 1.5.2
pytorch >= 1.0.0
...Prepare aDockerfilefile which includes therequirements.txtfile and the code to run the service.FROM python:3.9-slim-buster
COPY requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt
COPY . /app
WORKDIR /app
# In case you want to use the built-in web service provided by `PiperService` class and it's listening at the port 8000
# Or you write your own web service and it's listening at the port 8000
EXPOSE 8000
CMD ["python", "main.py"]Build the Docker image:docker build -t my_image .Run the Docker image:docker run -p 8000:8000 my_imageBuilding from SourceThefeathrpiperpackage is written in Rust, so you need to setup the Rust toolchain to build it from source. The Rust toolchain can be installed fromhere. The development is done in Rust 1.65, older version may not work.Installmaturin:pip install maturinBuild the package under thefeathrpiper_root/pythondirectory:maturin build --releaseMore information aboutmaturincan be foundhere. Please note that runningcargo buildin the top level directory won't build the Python package because the python package project is excluded from the workspace for some technical issues.Limitations and Known IssuesThePiperServiceclass supports plain HTTP only, and it doesn't support any kind of authentication.Thefeathrpipersupports Python 3.7~3.11, no support for Python 3.6 or earlier, and no support for Python 2.The package published on PyPI only support following platforms:Linux arm64Linux armv7Linux x86_64macOS x86_64/AppleSilicon universalWindows x86_64You need to build the package from source if you need to use it on other platforms.