Provides the interfaces of writing Python User Defined Functions and Sinks for NumaFlow.
Project description
Python SDK for Numaflow
This SDK provides the interface for writing UDFs and UDSinks in Python.
Installation
Install the package using pip.
pip install pynumaflow
Build locally
This project uses Poetry for dependency management and packaging. To build the package locally, run the following command from the root of the project.
make setup
To run unit tests:
make test
To format code style using black and ruff:
make lint
Setup pre-commit hooks:
pre-commit install
Implement a User Defined Function (UDF)
Map
from pynumaflow.function import Messages, Message, Datum, Server
def my_handler(keys: list[str], datum: Datum) -> Messages:
val = datum.value
_ = datum.event_time
_ = datum.watermark
return Messages(Message(value=val, keys=keys))
if __name__ == "__main__":
grpc_server = Server(map_handler=my_handler)
grpc_server.start()
MapT - Map with event time assignment capability
In addition to the regular Map function, MapT supports assigning a new event time to the message. MapT is only supported at source vertex to enable (a) early data filtering and (b) watermark assignment by extracting new event time from the message payload.
from datetime import datetime
from pynumaflow.function import MessageTs, MessageT, Datum, Server
def mapt_handler(keys: list[str], datum: Datum) -> MessageTs:
val = datum.value
new_event_time = datetime.now()
_ = datum.watermark
message_t_s = MessageTs(MessageT(val, event_time=new_event_time, keys=keys))
return message_t_s
if __name__ == "__main__":
grpc_server = Server(mapt_handler=mapt_handler)
grpc_server.start()
Reduce
import aiorun
from typing import Iterator, List
from pynumaflow.function import Messages, Message, Datum, Metadata, AsyncServer
async def my_handler(
keys: List[str], datums: Iterator[Datum], md: Metadata
) -> Messages:
interval_window = md.interval_window
counter = 0
async for _ in datums:
counter += 1
msg = (
f"counter:{counter} interval_window_start:{interval_window.start} "
f"interval_window_end:{interval_window.end}"
)
return Messages(Message(str.encode(msg), keys))
if __name__ == "__main__":
grpc_server = AsyncServer(reduce_handler=my_handler)
aiorun.run(grpc_server.start())
Sample Image
A sample UDF Dockerfile is provided under examples.
Implement a User Defined Sink (UDSink)
from typing import Iterator
from pynumaflow.sink import Datum, Responses, Response, Sink
def my_handler(datums: Iterator[Datum]) -> Responses:
responses = Responses()
for msg in datums:
print("User Defined Sink", msg.value.decode("utf-8"))
responses.append(Response.as_success(msg.id))
return responses
if __name__ == "__main__":
grpc_server = Sink(my_handler)
grpc_server.start()
Sample Image
A sample UDSink Dockerfile is provided under examples.
Datum Metadata
The Datum object contains the message payload and metadata. Currently, there are two fields in metadata: the message ID, the message delivery count to indicate how many times the message has been delivered. You can use these metadata to implement customized logic. For example,
...
def my_handler(keys: list[str], datum: Datum) -> Messages:
num_delivered = datum.metadata.num_delivered
# Choose to do specific actions, if the message delivery count reaches a certain threshold.
if num_delivered > 3:
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pynumaflow-0.4.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 354e44bff87578bb43df43e70d326d82be1f75280dfdaad6f8ae3d46608e886b |
|
MD5 | 7e051ef802544d6ecd777efa4885016b |
|
BLAKE2b-256 | 40b0b8070bc9708d88585bf691c7c5293f1fd65c3bab3c396a26da3ffe0aafc7 |