【问题标题】:confluent-kafka based consumer in Python does not workPython中基于confluent-kafka的消费者不起作用
【发布时间】:2019-02-16 08:51:44
【问题描述】:

对 kafka 和 Avro 非常陌生。我遇到了一个问题,似乎无法弄清楚这里出了什么问题。我写了一个使用 Avro 作为序列化格式的 kafka 的生产者和消费者。生产者代码工作正常。在我运行kafka-avro-console-consumer 时运行该代码后,它给了我如下 -

bin/kafka-avro-console-consumer --bootstrap-server localhost:9092 --topic test --property schema.registry.url=http://127.0.0.1:8081 --from-beginning
{"name":{"string":"Hello World!"}}
{"name":{"string":"Hello World!"}}
{"name":{"string":"Hello World!"}}

但是,当我尝试使用 python 做同样的事情时(遵循这个最基本的example),我编写了以下代码 -

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


class AvroConsumerAdapter(object):

    def __init__(self, topic='test'):
        self.topic = topic
        self.consumer = AvroConsumer({'bootstrap.servers': 'localhost:9092',
                                      'schema.registry.url': 'http://127.0.0.1:8081',
                                      'group.id': 'mygroup'})
        self.consumer.subscribe([topic])

    def start_consuming(self):
        running = True
        while running:
            try:
                msg = self.consumer.poll(10)
                if msg:
                    print(msg.value())
                    if not msg.error():
                        print("Here - 1")
                        print(msg.value())
                    elif msg.error().code() != KafkaError._PARTITION_EOF:
                        print("here-2")
                        print(msg.error())
                        running = False
                    else:
                        print('Here-3')
                        print(msg.error())
            except SerializerError as e:
                print("Message deserialization failed for %s: %s" % (msg, e))
                running = False
            except Exception as ex:
                print(ex)
                running = False

        self.consumer.close()

这个客户永远呆在那里,从不打印任何东西。我不确定这里有什么问题。任何人都可以在这方面帮助我。

【问题讨论】:

    标签: python apache-kafka confluent-platform


    【解决方案1】:

    查看topic config options——如果要处理当前主题中的所有数据,则需要设置auto.offset.reset': 'smallest'。默认情况下它是largest,这意味着它只会显示生成的新数据行。您可以通过让当前的 Python 代码运行并为该主题生成新消息来验证这一点 - 您应该会看到 Python 代码拾取它们。

    【讨论】:

    • 非常感谢。这帮助我理解并克服了问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2016-12-03
    • 2016-10-16
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多