I have used Kafka Streams in Java. I could not find similar API in python. Do Apache Kafka support stream processing in python?
Kafka Streams is only available as a JVM library, but there are a few comparable Python implementations of it
- robinhood/faust (Not maintained as of 2020, but was forked)
- wintincode/winton-kafka-streams (appears not to be maintained)
fluvii
(see discussion)- bytewax
In theory, you could try playing with Jython or Py4j to work with the JVM implementation, but probably would require more work than necessary.
Outside of those options, you can also try Apache Beam, Flink or Spark, but they each require an external cluster scheduler to scale out (and also require a Java installation).
If you are okay with HTTP methods, then running a KSQLDB instance (again, requiring Java for that server) and invoking its REST interface from Python with the built-in SQL functions can work. However, building your own functions there will requiring writing JVM compiled code, last I checked.
If none of those options are suitable, then you're stuck with the basic consumer/producer methods.
If you are using Apache Spark, you can use Kafka as producer and Spark Structured Streaming as consumer. No need to rely on 3rd part libraries like Faust.
To consume Kafka data streams in Spark, use the Structured Streaming + Kafka Integration Guide.
Keep in mind that you will have to append spark-sql-kafka
package when using spark-submit
:
spark-submit --packages org.apache.spark:spark-sql-kafka-0-10_2.12:3.0.1 StructuredStreaming.py
This solution has been tested with Spark 3.0.1 and Kafka 2.7.0 with PySpark.
This resource can also be useful.
Previously KStrame python API was not available but now its available with new KStream python library https://pypi.org/project/kstreams/
Features:
- Produce events
- Consumer events with Streams
- Prometheus metrics and custom monitoring
- TestClient
- Custom Serialization and Deserialization
- Easy to integrate with any async framework. No tied to any library!!
- Yield events from streams
- Store (kafka streams pattern)
- Stream Join
- Windowing
There is a relatively new library called FastStream:
FastStream is a powerful and easy-to-use Python framework for building asynchronous services interacting with event streams such as Apache Kafka, RabbitMQ, NATS and Redis.
It looks really good and quite simple to use, although I haven't personally used it yet. It is in constant development and supports more brokers besides Kafka (like RabbitMQ).
© 2022 - 2024 — McMap. All rights reserved.