Skip to main content

A small example package

Project description

Yet another kafka consumer!

Installation

To use the consumer you can add it as a dependency to your project.

PIP

python -m pip install --user cb-kafka-consumer

Pipfile

[packages]
cb-kafka-consumer = "~=0.1.0"

An example decoder / handler

Make sure to use the right values to initiate CBKafkaConsumer. It is necessary to pass the right decoder callback to decode the received raw message. An example json decoder is provided below.

from cb_kafka_consumer.src.consumer import CBKafkaConsumer, Message
import json
import logging


class MyConsumer:
    def __init__(self):
        self.__consumer = CBKafkaConsumer('127.0.0.1:9092', 'my-group-id', 'my-topic', self.__handler, self.__decoder)
        self.__consumer.start()

    def __handler(self, msg: Message):
        print(msg.get_offset(), msg.msg)
        self.__consumer.commit(msg)

    def __decoder(self, raw_msg: str):
        try:
            return json.loads(raw_msg)
        except ValueError:
            logging.error(f'Message cannot be parsed\n{raw_msg}')
            return None

Commit policy

There are two different approaches dealing with commit policy.

  • auto_commit=True

You can use auto_commit=True when initiating the consumer to instruct it to automatically commit received messages right after they have been handed over to the handler callback. When using auto_commit, the handler callback is not expected to explicitly call the commit method of consumer object.

  • auto_commit=False (default behavior)

If auto_commit is not specified or set to False the consumer will only commit messages right before the item in the sequence where it's not committed (handling may have probably failed). To further demonstrate this let's assume we the consumer has received messages with the following offsets:

1, 2, 3, 4, 5

The consumer now hands over the received messages to the handler callback. Now let's say the callback processes message #1 and #2 successfully and commits these two messages but fails to process #3. Next, messages #4 and #5 are successfully processed and committed. The consumer will only commit message #1 and #2 and will not commit succeeded messages until commit is called with message #3 and will only then move onward.

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

cb_kafka_consumer-0.1.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

cb_kafka_consumer-0.1.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file cb_kafka_consumer-0.1.2.tar.gz.

File metadata

  • Download URL: cb_kafka_consumer-0.1.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for cb_kafka_consumer-0.1.2.tar.gz
Algorithm Hash digest
SHA256 105b827ec9f48ecf503f5a9dd4f13acdb59c00c76295a1f2d475a6ce63325adf
MD5 7da0906b2e76adb33db177d8e509ce96
BLAKE2b-256 5ac26cee554947fb7be30dd2cd841f633633cce18bb1a15e7644a005372fd1a5

See more details on using hashes here.

File details

Details for the file cb_kafka_consumer-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for cb_kafka_consumer-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 24987fcf11ee51cb13f06f1e745903050c2c7d1e1e4f3e118c0fb765582f5c0d
MD5 410bee64451138f0a09c9dcd2dc4572b
BLAKE2b-256 674814cd07fb32060f9860aa3630c695420543df667f1b77f78f51e4a39372d9

See more details on using hashes here.

Supported by

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