Skip to main content

A python library to easily create kafka producer and consumer

Reason this release was yanked:

license was updated to MIT

Project description

Heizer

A python library to easily create kafka producer and consumer

Install

pip install heizer

Setup

Use docker-compose.yaml file to start kafka service

docker-compose up -d

Sample

Producer

from heizer import HeizerConfig, HeizerTopic, producer

config = HeizerConfig(
    {
        "bootstrap.servers": "localhost:9092",
        "group.id": "default",
        "auto.offset.reset": "earliest",
    }
)

my_topics = [
    HeizerTopic(name="my.topic1", partitions=[0]),
    HeizerTopic(name="my.topic2", partitions=[0, 1]),
]


@producer(
    topics=my_topics,
    config=config,
)
def my_producer(my_name: str):
    return {
        "name": my_name
    }


if __name__ == "__main__":
    my_producer("Jack")
    my_producer("Alice")

Consumer

from heizer import HeizerConfig, HeizerTopic, consumer, producer
from confluent_kafka import Message
import json

config = HeizerConfig(
    {
        "bootstrap.servers": "localhost:9092",
        "group.id": "default",
        "auto.offset.reset": "earliest",
    }
)

topics = [HeizerTopic(name="my.topic1")]


@producer(
    topics=topics,
    config=config
)
def produce_data(status: str, result: str):
    return {
        "status": status,
        "result": result,
    }


# Heizer expects consumer stopper func return Bool type result
# For this example, consumer will stop and return value if 
# `status` is `success` in msg
# If there is no stopper func, consumer will keep running forever
def stopper(msg: Message):
    data = json.loads(msg.value().decode("utf-8"))
    if data["status"] == "success":
        return True

    return False


@consumer(
    topics=topics,
    config=config,
    stopper=stopper,
)
def consume_data(msg):
    data = json.loads(msg.value().decode("utf-8"))
    print(data)
    return data["result"]


if __name__ == "__main__":
    produce_data("start", "1")
    produce_data("loading", "2")
    produce_data("success", "3")
    produce_data("postprocess", "4")

    result = consume_data()

    print("Expected Result:", result)

After you executed this code block, you will see those output on your terminal

{'status': 'start', 'result': '1'}
{'status': 'loading', 'result': '2'}
{'status': 'success', 'result': '3'}

Expected Result: 3

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

heizer-0.1.0.tar.gz (657.7 kB view details)

Uploaded Source

Built Distribution

heizer-0.1.0-py3-none-any.whl (30.6 kB view details)

Uploaded Python 3

File details

Details for the file heizer-0.1.0.tar.gz.

File metadata

  • Download URL: heizer-0.1.0.tar.gz
  • Upload date:
  • Size: 657.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.12

File hashes

Hashes for heizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ea68fcf859ed2761faaad88dee8da65c3a478c858ebd0863bbb35ad8e47e594e
MD5 537981de8f9961f93c6d7e114f3e54cb
BLAKE2b-256 378a95e599d1229da83b0d9970f208798e7248502888c9e7976df0f98056243b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: heizer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 30.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.12

File hashes

Hashes for heizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a3c5de233a7dd2a738e75c3f850f9fb089bd222813128e982402915dc9f32dd
MD5 5f516ba104c08c880616e707e4fa7e26
BLAKE2b-256 d4df88bb694a3904b965edf99e9ca25b88287a878e565514e61834058bf60700

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