0

I am trying to implement an exactly-once concept using confluent kafka library. But I couldn't succeed. Here is my producer code:

from confluent_kafka import Producer, SerializingProducer
import socket, json, random, time

conf = {'bootstrap.servers': "localhost:9092,localhost:9093,localhost:9094",
        'client.id': socket.gethostname(),
       'message.send.max.retries':2,
       'enable.idempotence':True,
       'isolation.level':'read_committed'}

producer = Producer(conf)

producer.produce("test_topic2", key="pc", value="ok")
producer.flush()

Here is my consumer code:

from confluent_kafka import Consumer, TopicPartition

conf = {'bootstrap.servers': 'localhost:9092,localhost:9093,localhost:9094',
        'group.id': "foo2",
        'auto.offset.reset': 'earliest',
       'enable.idempotence':True,
       'isolation.level':'read_committed'}

consumer = Consumer(conf)

topic = "test_topic2"
#consumer.subscribe([topic],on_assign=my_on_assign)
consumer.subscribe([topic])
# Poll for new messages from Kafka and print them.
try:
    while True:
        msg = consumer.poll()
        if msg is None:
            print("Waiting...")
        elif msg.error():
            print("ERROR: %s".format(msg.error()))
        else:
            # Extract the (optional) key and value, and print.
            print(f"{msg.topic()} key = {msg.key()} value = {msg.value().decode('utf-8'):12}\
            {msg.partition()} {msg.offset()} ")
except KeyboardInterrupt:
    pass
finally:
    # Leave group and commit final offsets
    consumer.close()

And this is my output(respectively topic name, key name, value name, partition and offset):

test_topic2 key = b'pc' value = ok 2 3

test_topic2 key = b'pc' value = ok 2 4

As I know when I send the same key and value it needs to be arranged to show only one of them. Here for example I need to only get the message which offset is 3. So my question is am I implementing wrong architecture on my code or I understand the concept wrongly ? Thanks in advance.

0 Answers0