I'm a beginner in Kafka and trying to consume the latest unconsumed or un processed messages on a topic and below is the function I came up with.
It works fine but have a logical problem though, it is returning the last consumed message again and again eventhough there are no new messages in the topic.
Ideally, I'm trying to get only the latest unconsumed messages, if nothing new, just return stating -- "No new messages in the topic. All the messages are already consumed.
" I tried various options, like setting offset to earliest and latest but nothing worked.
I'm kind of stumped and any guidance will be of great help .
In my case, the last consumed offset is 140 and I would like to process messages from 141 onwards, but, if 141 hasn't arrived, my function still returns 140's message.
I'm using confluent_kafka.
from confluent_kafka import Consumer, TopicPartition
def get_topic_latest_offset_message(topic, broker, kafka_group="example-topic"):
none_config = {}
c = Consumer({"bootstrap.servers": broker, "group.id": kafka_group, "auto.offset.reset": "latest"})
_ , high = c.get_watermark_offsets(TopicPartition(topic, 0), timeout=5)
c.assign([TopicPartition(topic, 0, high - 1)])
message = c.poll(timeout=5)
c.close()
if message is None:
# keep calling until you get a not null message
get_topic_latest_offset(topic, broker, kafka_group)
elif message.error():
raise TopicFetchError(topic)
return json.loads(message.value().decode("utf-8"))