Skip to main content

Actions Status PyPI Bytewax User Guide

Bytewax

Python Stateful Stream Processing Framework

Bytewax is a Python framework that simplifies event and stream processing. Because Bytewax couples the stream and event processing capabilities of Flink, Spark, and Kafka Streams with the friendly and familiar interface of Python, you can re-use the Python libraries you already know and love. Connect data sources, run stateful transformations and write to various different downstream systems with built-in connectors or existing Python libraries.

Screen Shot 2022-10-07 at 2 22 49 PM

How it all works

Bytewax is a Python framework and Rust distributed processing engine that uses a dataflow computational model to provide parallelizable stream processing and event processing capabilities similar to Flink, Spark, and Kafka Streams. You can use Bytewax for a variety of workloads from moving data à la Kafka Connect style all the way to advanced online machine learning workloads. Bytewax is not limited to streaming applications but excels anywhere that data can be distributed at the input and output.

Bytewax has an accompanying command line interface, waxctl, which supports the deployment of dataflows on cloud vms or kuberentes. You can download it here.


Getting Started with Bytewax

pip install bytewax

Install waxctl

A Bytewax dataflow is Python code that will represent an input, a series of processing steps, and an output. The inputs could range from a Kafka stream to a WebSocket and the outputs could vary from a data lake to a key-value store.

from bytewax.dataflow import Dataflow
from bytewax.inputs import KafkaInputConfig
from bytewax.outputs import ManualOutputConfig

# Bytewax has input and output helpers for common input and output data sources
# but you can also create your own with the ManualOutputConfig.

At a high-level, the dataflow compute model is one in which a program execution is conceptualized as data flowing through a series of operator-based steps. Operators like map and filter are the processing primitives of Bytewax. Each of them gives you a “shape” of data transformation, and you give them regular Python functions to customize them to a specific task you need. See the documentation for a list of the available operators

import json


def deserialize(key_bytes__payload_bytes):
    key_bytes, payload_bytes = key_bytes__payload_bytes
    key = json.loads(key_bytes) if key_bytes else None
    event_data = json.loads(payload_bytes) if payload_bytes else None
    return event_data["user_id"], event_data


def anonymize_email(user_id__event_data):
    user_id, event_data = user_id__event_data
    event_data["email"] = "@".join(["******", event_data["email"].split("@")[-1]])
    return user_id, event_data


def remove_bytewax(user_id__event_data):
    user_id, event_data = user_id__event_data
    return "bytewax" not in event_data["email"]


flow = Dataflow()
flow.input("inp", KafkaInputConfig(brokers=["localhost:9092"], topic="web_events"))
flow.map(deserialize)
flow.map(anonymize_email)
flow.filter(remove_bytewax)

Bytewax is a stateful stream processing framework, which means that some operations remember information across multiple events. Windows and aggregations are also stateful, and can be reconstructed in the event of failure. Bytewax can be configured with different state recovery mechanisms to durably persist state in order to recover from failure.

There are multiple stateful operators available like reduce, stateful_map and fold_window. The complete list can be found in the API documentation for all operators. Below we use the fold_window operator with a tumbling window based on system time to gather events and calculate the number of times events have occurred on a per-user basis.

import datetime
from collections import defaultdict

from bytewax.window import TumblingWindowConfig, SystemClockConfig

cc = SystemClockConfig()
wc = TumblingWindowConfig(length=datetime.timedelta(seconds=5))


def build():
    return defaultdict(lambda: 0)


def count_events(results, event):
    results[event["type"]] += 1
    return results


flow.fold_window("session_state_recovery", cc, wc, build, count_events)

Output mechanisms in Bytewax are managed in the capture operator. There are a number of helpers that allow you to easily connect and write to other systems (output docs). If there isn’t a helper built, it is easy to build a custom version, which we will do below. Similar the input, Bytewax output can be parallelized and the client connection will occur on the worker.

import json

import psycopg2


def output_builder(worker_index, worker_count):
    # create the connection at the worker level
    conn = psycopg2.connect("dbname=website user=bytewax")
    conn.set_session(autocommit=True)
    cur = conn.cursor()

    def write_to_postgres(user_id__user_data):
        user_id, user_data = user_id__user_data
        query_string = """
                    INSERT INTO events (user_id, data)
                    VALUES (%s, %s)
                    ON CONFLICT (user_id)
                    DO
                        UPDATE SET data = %s;"""
        cur.execute(
            query_string, (user_id, json.dumps(user_data), json.dumps(user_data))
        )

    return write_to_postgres


flow.capture(ManualOutputConfig(output_builder))

Bytewax dataflows can be executed on a single host with multiple Python processes, or on multiple hosts. Below is an example of running bytewax across multiple hosts. When processing data in a distributed fashion, Bytewax will ensure that all items with the same key are routed to the same host.

if __name__ == "__main__":
    addresses = ["localhost:2101"]

    cluster_main(flow, addresses=addresses, proc_id=0, worker_count_per_proc=2)

Deploying and Scaling

Bytewax can be run on a local machine or remote machine, just like a regular Python script.

python my_dataflow.py

It can also be run in a Docker container as described further in the documentation.

Kubernetes

The recommended way to run dataflows at scale is to leverage the kubernetes ecosystem. To help manage deployment, we built waxctl, which allows you to easily deploy dataflows that will run at huge scale across multiple compute nodes.

waxctl df deploy my_dataflow.py --name my-dataflow

Why Bytewax?

At a high level, Bytewax provides a few major benefits:

  • The operators in Bytewax are largely “data-parallel”, meaning they can operate on independent parts of the data concurrently.
  • Bytewax offers the ability to express higher-level control constructs, like iteration.
  • Bytewax allows you to develop and run your code locally, and then easily scale that code to multiple workers or processes without changes.
  • Bytewax can be used in both a streaming and batch context
  • Ability to leverage the Python ecosystem directly

Community

Slack Is the main forum for communication and discussion.

GitHub Issues is reserved only for actual issues. Please use the slack community for discussions.

Code of Conduct

Usage

Install the latest release with pip:

pip install bytewax

Building From Source

To build a specific branch, you will need to use Maturin and have Rust installed on your machine. Once those have been installed run

maturin develop -E dev

Important: If you are testing with a maturin built version from source, you should use maturin build --release since maturin develop will be slower.

More Examples

For a more complete example, and documentation on the available operators, check out the User Guide and the Examples

For an exhaustive list of examples, checkout the /examples folder

License

Bytewax is licensed under the Apache-2.0 license.

Contributing

Contributions are welcome! This community and project would not be what it is without the contributors. All contributions, from bug reports to new features, are welcome and encouraged. Please view the contribution guidelines before getting started.



With ❤️ Bytewax

Metadata

Release files for bytewax 0.15.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for bytewax 0.15.1
File
bytewax-0.15.1-cp310-cp310-manylinux_2_31_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.31+ x86-64 Details
bytewax-0.15.1-cp310-cp310-manylinux_2_27_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.27+ x86-64 Details
bytewax-0.15.1-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
bytewax-0.15.1-cp310-cp310-macosx_10_7_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.7+ x86-64 Details
bytewax-0.15.1-cp39-cp39-manylinux_2_31_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.31+ x86-64 Details
bytewax-0.15.1-cp39-cp39-manylinux_2_27_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.27+ x86-64 Details
bytewax-0.15.1-cp39-cp39-macosx_11_0_arm64.whl CPython 3.9 CPython 3.9 macOS 11.0+ ARM64 Details
bytewax-0.15.1-cp39-cp39-macosx_10_7_x86_64.whl CPython 3.9 CPython 3.9 macOS 10.7+ x86-64 Details
bytewax-0.15.1-cp38-cp38-manylinux_2_31_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.31+ x86-64 Details
bytewax-0.15.1-cp38-cp38-manylinux_2_27_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.27+ x86-64 Details
bytewax-0.15.1-cp38-cp38-macosx_11_0_arm64.whl CPython 3.8 CPython 3.8 macOS 11.0+ ARM64 Details
bytewax-0.15.1-cp38-cp38-macosx_10_7_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.7+ x86-64 Details
bytewax-0.15.1-cp37-cp37m-manylinux_2_31_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.31+ x86-64 Details
bytewax-0.15.1-cp37-cp37m-manylinux_2_27_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.27+ x86-64 Details
bytewax-0.15.1-cp37-cp37m-macosx_11_0_arm64.whl CPython 3.7 CPython 3.7 pymalloc macOS 11.0+ ARM64 Details
bytewax-0.15.1-cp37-cp37m-macosx_10_7_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.7+ x86-64 Details

Total release size: 125.0 MB

Release files / bytewax-0.15.1-cp310-cp310-manylinux_2_31_x86_64.whl

Download URL bytewax-0.15.1-cp310-cp310-manylinux_2_31_x86_64.whl
Size 8.9 MB
Tags CPython 3.10 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
4e1598aefcfcae77178adbd4c343448ff4ed74f2cb09ea213e40e84559cfe642
BLAKE2b-256 checksum
How to use checksums
86cf247d5e294c9a3d14f5642f1a5351510beffd08d634bc17b792dd28299dff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp310-cp310-manylinux_2_27_x86_64.whl

Download URL bytewax-0.15.1-cp310-cp310-manylinux_2_27_x86_64.whl
Size 8.8 MB
Tags CPython 3.10 Linux glibc 2.27+ x86-64
SHA-256 checksum
How to use checksums
b209c4062167d1cf9474e4ae6a728a5f7dbf2752f05cce7f98b5bc5006898ac9
BLAKE2b-256 checksum
How to use checksums
d2157888c87d3fe48eb1ba33e682feee9aebde79a5df60d097234448f8f29039
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp310-cp310-macosx_11_0_arm64.whl

Download URL bytewax-0.15.1-cp310-cp310-macosx_11_0_arm64.whl
Size 6.5 MB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6d49ec2656381d6ac2f7eec5f5f452311acd0b6a76c57f4cfca91aba9a5289fb
BLAKE2b-256 checksum
How to use checksums
c7835a0bc530a9d4dc5611c3082ebb827178a69cd7635f4df040fdac027eaa57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp310-cp310-macosx_10_7_x86_64.whl

Download URL bytewax-0.15.1-cp310-cp310-macosx_10_7_x86_64.whl
Size 7.0 MB
Tags CPython 3.10 macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
824128e73a9dc16dfcc0d59c0a73263f51a05e229f4c35c8c339de8e8998011f
BLAKE2b-256 checksum
How to use checksums
8792c4799aa70d2e215ffb0e1abf6678b93426ab5361fc89964d0a1aae0e90d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp39-cp39-manylinux_2_31_x86_64.whl

Download URL bytewax-0.15.1-cp39-cp39-manylinux_2_31_x86_64.whl
Size 8.9 MB
Tags CPython 3.9 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
b2f5baf9a7411aeaecf6ae7937fbdf895cd6c5cde5b93ebfc6d905c385dfae40
BLAKE2b-256 checksum
How to use checksums
4bf0eaa234b8b1af840f592239f6df72076de8d1aaa2f15067da5bf4d4f15522
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp39-cp39-manylinux_2_27_x86_64.whl

Download URL bytewax-0.15.1-cp39-cp39-manylinux_2_27_x86_64.whl
Size 8.8 MB
Tags CPython 3.9 Linux glibc 2.27+ x86-64
SHA-256 checksum
How to use checksums
136d328b5b180c24f8eebae693469f2ada2bc169748bfb8f9a7ac61880022049
BLAKE2b-256 checksum
How to use checksums
49d99ddc915e4635bf2fc14f59afb1281b5c45b5f9922d3a7b6852ed8cde1ede
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp39-cp39-macosx_11_0_arm64.whl

Download URL bytewax-0.15.1-cp39-cp39-macosx_11_0_arm64.whl
Size 6.5 MB
Tags CPython 3.9 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
87f4a6267cf589863fa50f1c91080d836e05bb5fb6a8b39cc8d711c3ff86f609
BLAKE2b-256 checksum
How to use checksums
62217918ee35cae7ecac9d2d9242e8624399b6189fd73392d7e98f6f54424003
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp39-cp39-macosx_10_7_x86_64.whl

Download URL bytewax-0.15.1-cp39-cp39-macosx_10_7_x86_64.whl
Size 7.0 MB
Tags CPython 3.9 macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
6837f0442ec5910b6bdd9b95c01bc56a9582df7a907c96de7363201abf71bbb4
BLAKE2b-256 checksum
How to use checksums
7703260005864ecc6ac29b5abef431235f158809c67e7dcee63602d42eb6c186
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp38-cp38-manylinux_2_31_x86_64.whl

Download URL bytewax-0.15.1-cp38-cp38-manylinux_2_31_x86_64.whl
Size 8.9 MB
Tags CPython 3.8 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
f7b9b826b99d053c1f670af4f0fbb0996117dfe023ce67c58e9a9f92d380b744
BLAKE2b-256 checksum
How to use checksums
80bcdad0b381339d35046ccb589e2dd973c1a5e6d37a5b38616e718d097a386d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp38-cp38-manylinux_2_27_x86_64.whl

Download URL bytewax-0.15.1-cp38-cp38-manylinux_2_27_x86_64.whl
Size 8.8 MB
Tags CPython 3.8 Linux glibc 2.27+ x86-64
SHA-256 checksum
How to use checksums
1a6582b2c949ebf7bab1db58f03e24c331cae6f9e0b1be7123d7fc5819a3464d
BLAKE2b-256 checksum
How to use checksums
c9ab5744140a03229513c345721afa54f05f5562d8ca420f68f157027787daea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp38-cp38-macosx_11_0_arm64.whl

Download URL bytewax-0.15.1-cp38-cp38-macosx_11_0_arm64.whl
Size 6.5 MB
Tags CPython 3.8 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
82c98e1bfa28af9fab050fd4c0b27db24de1c081c337548588686f05a418451c
BLAKE2b-256 checksum
How to use checksums
ffe00ffdc7492ba663c51591cbd768be77f037a31742deeee90620c05d9b83f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp38-cp38-macosx_10_7_x86_64.whl

Download URL bytewax-0.15.1-cp38-cp38-macosx_10_7_x86_64.whl
Size 7.0 MB
Tags CPython 3.8 macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
827fc7d4864b23aa7450bfd18896f4bbd80055e9a4030d9d7794703a8b9e3b4e
BLAKE2b-256 checksum
How to use checksums
ed4028dce15695af745745b078048055e858be3b4dc7ae48d7d224e6f8d1e254
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp37-cp37m-manylinux_2_31_x86_64.whl

Download URL bytewax-0.15.1-cp37-cp37m-manylinux_2_31_x86_64.whl
Size 8.9 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
027b9cbe184ce05a9593adbd45e8cfdead1aba0eda7c6632fd4b5d4b9ee43ce6
BLAKE2b-256 checksum
How to use checksums
99227e2240aa65060674ba89e3323741f0dc98cc12cd83b578c42dc095fb8424
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp37-cp37m-manylinux_2_27_x86_64.whl

Download URL bytewax-0.15.1-cp37-cp37m-manylinux_2_27_x86_64.whl
Size 8.8 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.27+ x86-64
SHA-256 checksum
How to use checksums
c9f0b97bdb82f2c08b9f51fc3f762b6783dda9b7b6e85696fb2faf7fec0880ec
BLAKE2b-256 checksum
How to use checksums
8d061d81de55805ec05fb2a053ea56a5792674568ba9111c40c2f79321212826
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp37-cp37m-macosx_11_0_arm64.whl

Download URL bytewax-0.15.1-cp37-cp37m-macosx_11_0_arm64.whl
Size 6.5 MB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
3d297af98e476066ecd85a98380897f24e0dda34c96c16ca8de5cdf60de41a3d
BLAKE2b-256 checksum
How to use checksums
f272856ed56d9a26b289bada283e39a015969b53261c561bdf62915f9bf54703
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / bytewax-0.15.1-cp37-cp37m-macosx_10_7_x86_64.whl

Download URL bytewax-0.15.1-cp37-cp37m-macosx_10_7_x86_64.whl
Size 7.0 MB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
dbf9d68acc956d517743ec9cfcdfac3939c56d6b5c35807b254a17e3a17796d3
BLAKE2b-256 checksum
How to use checksums
a40986b2cf277147a53f46ac7f4884e6a43a528c62995ebb412831f7e8ec25e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page