我已经用python创建了带有主题的kafka生产者-消费者消息传递。如何对queue执行相同的操作,以便消息只发送给单个消费者。
这是我的制片人代码
# Import KafkaProducer from Kafka library
from kafka import KafkaProducer
# Define server with port
bootstrap_servers = ['localhost:9092']
# Define topic name where the message will publish
topicName = 'First_Topic'
# Initialize producer variable
producer = KafkaProducer(bootstrap_servers = bootstrap_servers)
i=1
for i in range(100):
# Publish text in defined topic
message_data = input("Enter message ")
producer.send(topicName,str.encode(message_data) )
# Print message
print("Message Sent")
i=i+1
这是我的消费代码。
from kafka import KafkaConsumer
consumer = KafkaConsumer(bootstrap_servers=['localhost:9092'], auto_offset_reset='earliest')
consumer.subscribe(['First_Topic'])
for msg in consumer:
print("Message from Producer on Topic - "+msg.topic+":"+ msg.value.decode())
暂无答案!
目前还没有任何答案,快来回答吧!