简体   繁体   English

无法向 Kafka Python 中的主题发送消息

[英]Unable to send messages to Topic in Kafka Python

I have a producer code where am sending messages to Kafka.我有一个生产者代码,用于向 Kafka 发送消息。 I was able to send messages till yesterday.直到昨天我都能发送消息。 From today am unable to send messages.从今天起,我无法发送消息。 Not sure if its version compatible issue.不确定其版本是否兼容问题。 No any failures or error messages, code gets executed, but its not sending messsages.没有任何失败或错误消息,代码被执行,但它不发送消息。

Below are the python module versions,以下是 python 模块版本,
kafka-python==2.0.1
Python 3.8.2

Below is my code,下面是我的代码,

from kafka import KafkaProducer
import logging
logging.basicConfig(level=logging.INFO)

producer = KafkaProducer(bootstrap_servers='127.0.0.1:9092')
producer.send('Jim_Topic', b'Message from PyCharm')
producer.send('Jim_Topic', key=b'message-two', value=b'This is Kafka-Python')

Any suggestion would be helpful.任何建议都会有所帮助。

I tries logging the behavior as well, but no idea why producer gets closed,我也尝试记录行为,但不知道为什么生产者被关闭,

INFO:kafka.conn:<BrokerConnection node_id=bootstrap-0 host=127.0.0.1:9092 <connecting> [IPv4 ('127.0.0.1', 9092)]>: connecting to 127.0.0.1:9092 [('127.0.0.1', 9092) IPv4]
INFO:kafka.conn:<BrokerConnection node_id=bootstrap-0 host=127.0.0.1:9092 <connecting> [IPv4 ('127.0.0.1', 9092)]>: Connection complete.
INFO:kafka.producer.kafka:Closing the Kafka producer with 0 secs timeout.
INFO:kafka.producer.kafka:Proceeding to force close the producer since pending requests could not be completed within timeout 0.
INFO:kafka.producer.kafka:Kafka producer closed

Process finished with exit code 0

Adding producer.flush() at the end helped me to resolve the issues.最后添加producer.flush()帮助我解决了这些问题。 Any outstanding messages will be flushed (delivered) before actually committing the transaction在实际提交事务之前,任何未完成的消息都将被刷新(传递)

from kafka import KafkaProducer
import logging
logging.basicConfig(level=logging.INFO)

producer = KafkaProducer(bootstrap_servers='127.0.0.1:9092')
producer.send('Jim_Topic', b'Message from PyCharm')
producer.send('Jim_Topic', key=b'message-two', value=b'This is Kafka-Python')
producer.flush()

Can you try the following code.你可以试试下面的代码。 I have picked it up from the kafka-python documentation and tried it on my local Kafka instance我从 kafka-python 文档中选择了它,并在我本地的 Kafka 实例上进行了尝试

from kafka import KafkaProducer
import json
def on_send_success(record_metadata):
    print(record_metadata.topic)
    print(record_metadata.partition)
    print(record_metadata.offset)


def on_send_error(excp):
    log.error('I am an errback', exc_info=excp)


if __name__ == "__main__":
    target_topic = "Jim_Topic"
    producer = KafkaProducer(bootstrap_servers=['127.0.0.1:9092'],retries=5, key_serializer=lambda x: json.dumps(x).encode("ascii"), value_serializer=lambda x: json.dumps(x).encode("ascii"))
    messages = [{key:"message_one",value:"Message from PyCharm"},{key:"message_two",value:"This is Kafka-Python"}]
    for msg in messages:
      producer.send(target_topic,msg).add_callback(on_send_success).add_errback(on_send_error)

    producer.flush(timeout=10) # this forcibly sends any messages that are stuck.
    producer.close(timeout=5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 kafka-python 检索主题中的消息 - retrieve messages in a topic using kafka-python 带有 python 的 Kafka:如何将主题发送到 postgreSQL? - Kafka with python: How to send topic to postgreSQL? Python Kafka生产者无法写入主题 - Python Kafka producer unable to write to topic 由于 KafkaTimeoutError,无法使用 kafka-python 从 django 应用程序向 kafka 发送消息 - Unable to send messages to kafka from django application using kafka-python due to KafkaTimeoutError 无法使用 Apache Beam 将消息从源 kafka 主题推送到目标 kafka 主题 - Unable to push messages from source kafka topic to destination kafka topic using Apache beam 盒子里的Kafka:无法从主机发送消息 - Kafka in a box: unable to send messages from host Python如何删除Kafka主题下的所有消息 - Python how to delete all messages under a Kafka topic Python Confluent-kafka DeserializingConsumer 没有从 Kafka 主题中读取消息,即使消息存在 - Python Confluent-kafka DeserializingConsumer is not reading the messages from Kafka topic, even though the messages are present 未收到来自 Kafka 主题的消息 - Not receiving messages from Kafka Topic 无法使用python-kafka使用消息 - Unable to consume messages using python-kafka
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM