Skip to main content

An easy to use kafka consumer that uses the confluent kafka library, it runs concurrently with your flask server

Project description

Flask Kafka

:warning: Breaking Changes: Dropping kafka-python library in favour of confluent-kafka due it's support & documentation

This is an easy to use utility to help Flask developers to implement microservices that interact with Kafka. This library has been inspired by two other similar libraries :-

After looking around the web and on Github, I was not able to find a lot of content on how to consume from a Kafka topic using the Kafka framework. From what I found, I was able to come up with this library by borrowing from the above libraries. They both had a little of what I wanted so I combined them to come up with this one.

I hope you find this useful.

Features

  • Doesn't block process
  • Configure by config.py
  • Support comsuming from topic by decorator

Installation

This project has been commited to Pypi, can be installed by pip:

$ pip install flask-kafka

Simple example

from flask import Flask, request

from flask_kafka import FlaskKafka
app = Flask(__name__)
app.config["KAFKA_CONFIG"] = {'bootstrap.servers': 'localhost:9092',
                              'group.id': 'foo',
                              'enable.auto.commit': 'false',
                              'auto.offset.reset': 'earliest'}

bus = FlaskKafka()
bus.init_app(app)

# curl http://localhost:5004/publish/test-topic?key=foo&value=bar

@app.route('/publish/<topic>', methods=["get"])
def publish(topic):
    qstr = request.args.to_dict()
    key = qstr['key']
    value = qstr['value']
    publisher = bus.get_producer()
    publisher.produce(topic, key=key, value=value)
    publisher.poll(1)
    return "Published to {} => {} : {}".format(topic, key, value)

@bus.handle('test-topic')
def test_topic_handler(consumer,msg):
    print("Consumed event from topic {topic}: key = {key:12} value = {value:12}".format(
            topic=msg.topic(), key=msg.key().decode('utf-8'), value=msg.value().decode('utf-8')))


# or
# bus.add_topic_handler("test-topic", lambda consumer, msg: print(msg.value()))

if __name__ == '__main__':
    bus.run()
    app.run(debug=True, port=5004, use_reloader=False)

Special Thanks

License

MIT License

Copyright (c) 2019 Nimrod Kevin Maina

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-kafka-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

flask_kafka-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file flask-kafka-0.1.0.tar.gz.

File metadata

  • Download URL: flask-kafka-0.1.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for flask-kafka-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1a0b84ec4eb099be56c8bfde0a79957db818ae56f0485413c0cd94e3943f5f7f
MD5 fbdee3337236f03dabf6220174ecff92
BLAKE2b-256 63fe43b714cb2cee71dfeff01d23b11de6970f540659e5d4b96482882d4a6fe2

See more details on using hashes here.

File details

Details for the file flask_kafka-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: flask_kafka-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for flask_kafka-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0d04281b0add214dc7497ec6583e88f5b1561385ce48ea6f68926620c7f0901
MD5 411ebbdf920132dd146b87d5c46475b3
BLAKE2b-256 99622448185cca873eb8e8f27d5dda413adfaa31f24cf64fbaf07403cd8d29df

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page