[英]Python Confluent-kafka DeserializingConsumer is not reading the messages from Kafka topic, even though the messages are present
我有一個帶有 avro 模式的主題,我通過 Python 代碼生成消息並且它工作得很好。 當我使用來自 CLI 的消息時,我可以成功使用它們而不會出錯。
當我嘗試通過 Python 代碼使用時,它打印“無”,基本上它嘗試讀取但沒有讀取,我嘗試打印偏移量並拋出“-1001”。
該方法旨在讀取所有最新消息,創建包含這些消息的列表並返回列表。
注意:- 我也嘗試過使用 usinng 'enable.auto.commit' = True,但它不起作用,因此將其從我的配置中刪除。
requirement.txt 中的庫 = confluent-kafka[avro]>=1.4.2
conf = {
'bootstrap.servers': 'dummyvalue',
'security.protocol': 'dummyvalue',
'sasl.mechanism': 'PLAIN',
'sasl.username': 'dummyvalue',
'sasl.password': 'dummyvalue',
'session.timeout.ms': 45000,
'schema.registry.url': 'dummyvalue',
'basic.auth.credentials.source': 'dummyvalue',
'basic.auth.user.info': 'dummyvalue',
'use.latest.version': True
}
schema_registry_conf = {
'url': conf['schema.registry.url'],
'basic.auth.user.info': conf['basic.auth.user.info']
}
def _set_consumer_config(self, conf, avro_deserializer):
consumer_conf = self._popSchemaRegistryParamsFromConfig(conf)
#above method will remove unnecessary configs from main conf dictionary so consumer_conf has only relevant properties
consumer_conf['value.deserializer'] = avro_deserializer
consumer_conf['group.id'] = "python_example"
consumer_conf['auto.offset.reset'] = 'latest'
return consumer_conf
def get_list_of_unconsumed_msgs(self, topic):
text_file = open('avro schema file path')
avro_schema = text_file.read()
schema_registry_client = SchemaRegistryClient(schema_registry_conf)
avro_deserializer = AvroDeserializer(schema_registry_client,avro_schema)
consumer = DeserializingConsumer(self._set_consumer_config(conf, avro_deserializer))
consumer.subscribe([topic])
messages = []
polling_count = 5
while polling_count >= 1:
try:
print(consumer.position([TopicPartition(topic, 0)]))
print(f"Consumer Committed {consumer.committed([TopicPartition(topic, 0)])}")
print(f"Consumer Assignment {consumer.assignment()}")
msg = consumer.poll(3.0)
if msg is None:
polling_count = polling_count - 1
continue
elif msg.error():
print('error: {}'.format(msg.error()))
else:
messages.append([msg.value()])
except SerializerError as e:
# Report malformed record, discard results, continue polling
print("Message deserialization failed {}".format(e))
consumer.close()
return messages
def main():
msg = {}
topic_name = "aa_automation_test"
msg = obj.get_list_of_unconsumed_msgs(topic)
print(f"Received Message as :- {msg}")
打印語句的輸出:
[Prints an empty list, for debugging I have printed offset and it throws -1001]
[TopicPartition{topic=aa_automation_test,partition=0,offset=-1001,error=None}]
Consumer Committed [TopicPartition{topic=aa_automation_test,partition=0,offset=-1001,error=None}]
Consumer Assignment []
Received Message as :- []
您看到的是默認值。
在您實際poll
並連接到代理之前,不會有任何數據。
Kafka 自己跟蹤未提交的偏移量。 您不需要在您的應用程序中實現該邏輯。
如果您在最后打印一個空列表,從您所顯示的內容來看,這意味着您已經到達主題的結尾。
要一次拉 5,請參閱consume(num_messages)
要檢查分區的(結束)偏移量, get_watermark_offsets
,您可以從consumer.committed()
中減去它以查看滯后。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.