![](/img/trans.png)
[英]Kafka producer and consumer not working properly on python with docker
[英]Kafka consumer implementation not working in Python
第一次使用 Kafka,我遇到了一個問題。
我的消費者有以下實現:
from kafka import KafkaConsumer
import config
class KafkaMessageConsumer:
def __init__(self):
self.consumer = KafkaConsumer(
bootstrap_servers=config.KAFKA_BOOTSTRAP_SERVER,
security_protocol=config.KAFKA_SECURITY_PROTOCOL,
sasl_mechanism=config.KAFKA_SASL_MECHANISM,
sasl_plain_username=config.KAFKA_USERNAME,
sasl_plain_password=config.KAFKA_PASSWORD,
value_deserializer=lambda x: json.loads(x.decode("utf-8")),
)
def receive_messages(self, topic):
self.consumer.subscribe(topics=[topic])
print(f"Subscribed to topics: {self.consumer.subscription()}")
for msg in self.consumer:
yield msg.value
if __name__ == "__main__":
consumer = KafkaMessageConsumer()
for message in consumer.receive_messages(config.KAFKA_TOPIC):
print("Received message:", message)
憑據應正確實施的位置。 我收到了訂閱該主題的消息,沒有錯誤,但是沒有生成消息,盡管我確定該主題上有要使用的消息。 我在這里缺少一些必要的配置嗎?
我不是 Python 方面的專家,但看起來您還沒有使用任何消息。 您已訂閱該主題,但您需要 poll() 以獲取消息https://kafka-python.readthedocs.io/en/master/apidoc/KafkaConsumer.html#kafka.KafkaConsumer.poll
另外,你在哪里設置主題名稱? [話題]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.