如何调试合流卡夫卡中的AvroConsumer?

2024-06-07 17:41:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从python中读取Kafka,但Receive消息为None,CLI中没有错误。 我通过putty将端口转发到目标主机,然后通过telnet测试端口,这很好。 此外,我在Debian(WSL)上使用kafkacat,它工作正常

kafkacat -C -b localhost:9092 -t topic1 -p 0 -o beginning -s avro -r http://localhost:28081

我正在使用PyCharm,我的代码在下面的文本中。如何调试

from confluent_kafka.avro import AvroConsumer
from confluent_kafka import TopicPartition
from confluent_kafka.avro.serializer import SerializerError

topics = ['topic1', 'topic2']
c = AvroConsumer({
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'mygroup',
    'auto.offset.reset': 'smallest',
    'schema.registry.url': 'http://localhost:28081',
    'api.version.request': True
})

c.subscribe(topics)
tp = TopicPartition(topics[0], 0, 0)
c.assign([tp])

while True:
    try:
        msg = c.poll(1)

    except SerializerError as e:
        print("Message deserialization failed for {}: {}".format(msg, e))
        break

    if msg is None:
        print('Message None')
        continue

    if msg.error():
        print("AvroConsumer error: {}".format(msg.error()))
        continue

    print(msg.value())

c.close()

作为


Tags: kafka端口fromimportnonelocalhostmsgerror
1条回答
网友
1楼 · 发布于 2024-06-07 17:41:49

我要做的第一件事是确保使用kafka-avro-console-consumer工具在您的主题上发送消息

然后在应用程序中,您可以尝试提高日志级别:

c = AvroConsumer({
    # ... your config here
    'log_level': 7,
    'debug': 'all',
})

您可以在这里看到不同的参数:https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md

但我相信您的问题与分配分区的方式有关。如果使用subscribe,则集群会自动将分区分配给使用者。您可以在订阅时添加回调,您可以看到哪些分区分配给了您的消费者,但您不必自己这么做。见https://docs.confluent.io/3.1.1/clients/confluent-kafka-python/index.html#confluent_kafka.Consumer.subscribe

相关问题 更多 >

    热门问题