feat: add python producer consumer example

This commit is contained in:
2026-01-25 01:27:46 +00:00
parent 8741182110
commit f8d7806ae1
5 changed files with 90 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
from kafka import KafkaConsumer
topic_name = "test-topic"
group_id = "test-group"
consumer = KafkaConsumer(
topic_name,
bootstrap_servers='localhost:9092', # advertised listener
auto_offset_reset='earliest', # start from the beginning if no offset is committed
enable_auto_commit=True, # read = commit, this is True by default
# who am I in the consumer group, without it you'll always read from beginning
# as kafka does not know if you have read before as you had not name for yourself
group_id=group_id
)
try:
for message in consumer:
# message is of type ConsumerRecord and data is in value which is bytes
message = message.value.decode('utf-8')
print(f"Received: {message}")
except KeyboardInterrupt:
print("Stopping consumer...")
finally:
consumer.close()