kafka python使用者未接收消息

kafka python使用者未接收消息,python,apache-kafka,kafka-consumer-api,kafka-python,Python,Apache Kafka,Kafka Consumer Api,Kafka Python,我在使用KafaConsumer时遇到问题,无法从开头或任何其他显式偏移量读取它 为同一主题的使用者运行命令行工具时,我确实会看到带有--from start选项的消息,否则它将挂起 $ ./kafka-console-consumer.sh --zookeeper {localhost:port} --topic {topic_name} --from-beginning 如果我通过python运行它,它会挂起,我怀疑这是由不正确的使用者配置引起的 consumer = KafkaConsu

我在使用
KafaConsumer
时遇到问题,无法从开头或任何其他显式偏移量读取它

为同一主题的使用者运行命令行工具时,我确实会看到带有
--from start
选项的消息,否则它将挂起

$ ./kafka-console-consumer.sh --zookeeper {localhost:port} --topic {topic_name} --from-beginning
如果我通过python运行它,它会挂起,我怀疑这是由不正确的使用者配置引起的

consumer = KafkaConsumer(topic_name,
                     bootstrap_servers=['localhost:9092'],
                     group_id=None,
                     auto_commit_enable=False,
                     auto_offset_reset='smallest')

print "Consuming messages from the given topic"
for message in consumer:
    print "Message", message
    if message is not None:
        print message.offset, message.value

print "Quit"
输出: 使用来自给定主题的消息 (之后挂起)

我使用的是kafka python 0.9.5,代理运行的是kafka 8.2。不确定确切的问题是什么


按照dpkp的建议设置_group_id=None_uu以模拟控制台使用者的行为

控制台使用者和您发布的python使用者代码之间的区别在于python使用者使用使用者组来保存偏移量:
group\u id=“test consumer group”
。相反,如果您将组id设置为“无”,您应该会看到与控制台使用者相同的行为。

自动偏移量重置为“最早”为我解决了此问题。

自动偏移量重置为“最早”
组id=无
为我解决了此问题。

我的目标是:打印并确保偏移量符合您的期望。通过使用
position()
seek\u to\u start()
,请查看代码中的注释

我无法解释:

  • 为什么在实例化了KafkaConsumer之后,没有分配分区,这是设计原因吗?Hack around是在
    seek\u to\u beging()之前调用
    poll()
    一次
  • 为什么有时在
    seek\u to\u start()
    之后,第一次调用
    poll()
    不会返回任何数据,也不会更改偏移量

  • 代码:

    import kafka
    print(kafka.__version__)
    from kafka import KafkaProducer, KafkaConsumer
    from time import sleep
    KAFKA_URL = 'localhost:9092' # kafka broker
    KAFKA_TOPIC = 'sida3_sdtest_topic' # topic name
    
    # ASSUMING THAT the topic exist
    
    # write to the topic
    producer = KafkaProducer(bootstrap_servers=[KAFKA_URL])
    for i in range(20):
        producer.send(KAFKA_TOPIC, ('msg' + str(i)).encode() )
    producer.flush()
    
    # read from the topic
    # auto_offset_reset='earliest', # auto_offset_reset is needed when offset is not found, it's NOT what we need here
    consumer = KafkaConsumer(KAFKA_TOPIC,
    bootstrap_servers=[KAFKA_URL],
    max_poll_records=2,
    group_id='sida3'
    )
    
    # (!?) wtf, why we need this to get partitions assigned
    # AssertionError: No partitions are currently assigned if poll() is not called
    consumer.poll()
    consumer.seek_to_beginning()
    
    # also AssertionError: No partitions are currently assigned if poll() is not called
    print('partitions of the topic: ',consumer.partitions_for_topic(KAFKA_TOPIC))
    
    from kafka import TopicPartition
    print('before poll() x2: ')
    print(consumer.position(TopicPartition(KAFKA_TOPIC, 0)))
    print(consumer.position(TopicPartition(KAFKA_TOPIC, 1)))
    
    # (!?) sometimes the first call to poll() returns nothing and doesnt change the offset
    messages = consumer.poll()
    sleep(1)
    messages = consumer.poll()
    
    print('after poll() x2: ')
    print(consumer.position(TopicPartition(KAFKA_TOPIC, 0)))
    print(consumer.position(TopicPartition(KAFKA_TOPIC, 1)))
    
    print('messages: ', messages)
    

    输出

    2.0.1
    partitions of the topic:  {0, 1}
    before poll() x2: 
    0
    0
    after poll() x2: 
    0
    2
    messages:  {TopicPartition(topic='sida3_sdtest_topic', partition=1): [ConsumerRecord(topic='sida3_sdtest_topic', partition=1, offset=0, timestamp=1600335075864, timestamp_type=0, key=None, value=b'msg0', headers=[], checksum=None, serialized_key_size=-1, serialized_value_size=4, serialized_header_size=-1), ConsumerRecord(topic='sida3_sdtest_topic', partition=1, offset=1, timestamp=1600335075864, timestamp_type=0, key=None, value=b'msg1', headers=[], checksum=None, serialized_key_size=-1, serialized_value_size=4, serialized_header_size=-1)]}
    

    我遇到了同样的问题:我可以在kafka控制台中接收消息,但无法使用包
    kafka python
    使用python脚本获取消息


    最后,我想原因是我没有在我的
    producer.py
    中调用
    producer.flush()
    producer.close()
    ,这在它的
    producer.py

    中没有提到。

    我以前遇到过同样的问题,所以我在运行要测试的代码的机器上本地运行了kafka主题,我得到了UnknownHostException。我在
    hosts
    文件中添加了IP和主机名,它在卡夫卡主题和代码中都运行良好。
    似乎
    KafkaConsumer
    试图获取消息,但失败,没有引发任何异常。

    我最近下载了kafka软件包,并尝试了您的代码,它对我有效。能否显示您的
    消费者.properties
    内容文件?您可能需要设置起始偏移量…也尝试设置起始偏移量,但也没有帮助。我正在使用一个包含多个分区的主题进行测试,因此,只有当生产者没有生成足够的消息,以至于所有分区中至少有一条消息时,才会出现问题。如果所有分区都至少有一条消息,则使用者可以正常工作。此外,KafkaConsumer不会对咬我的不受支持的编解码器引发异常,因为我使用的lz4还不受使用者支持,因此它没有解码消息,也没有引发异常。是的,这是问题之一。实际问题是制作人使用lz4作为压缩类型,而python消费者不支持这种类型,他们在没有警告/错误的情况下退出了;最新版本也不应该再在压缩错误时默默失败。这种错误行为(对于单节点/分区kafka)让我旋转了很长一段时间,除非我找到了这个答案