I am trying to read some data of a Kafka queue using Python as shown in the below code:
from kafka import KafkaConsumer
import sys
import json
import pandas as pd
bootstrap_servers = [localhost]
topicName = 'topic'
consumer = KafkaConsumer (topicName, group_id = 'topic',bootstrap_servers = bootstrap_servers, auto_offset_reset = 'earliest')
data_list = []
for message in consumer:
print(message)
data = json.loads(message.value)
df = pd.json_normalize(data)
data_list.append(df)
This seems to be running in a loop forever unless I kill the connection. Is there a way how I can stop/come out of this loop after having read all of the messages or there are no new messages in the queue?