我看到了文档。 https://kafka-python.readthedocs.io/en/master/usage.html
我尝试多个消费者。 但这不起作用吗? 以及它是如何工作的?
怎么了?
consumer1 = KafkaConsumer(
bootstrap_servers=['localhost:9092'],
#auto_offset_reset='earliest' , # 'earliest',
#enable_auto_commit= False ,
group_id='sr',
value_deserializer=lambda x: loads(x.decode('utf-8')))
consumer1.subscribe("numtest")
consumer2 = KafkaConsumer(
bootstrap_servers=['localhost:9092'],
#auto_offset_reset='earliest' , # 'earliest',
#enable_auto_commit= False ,
group_id='sr',
value_deserializer=lambda x: loads(x.decode('utf-8')))
consumer2.subscribe("numtest")
producer = KafkaProducer(bootstrap_servers=['localhost:9092'],
key_serializer = None ,
value_serializer=lambda x:
dumps(x).encode('utf-8'))
def on_send_success(record_metadata):
print("topic : {} , partition : {} , offset : {}".\
format( record_metadata.topic , record_metadata.partition , record_metadata.offset))
for msg , message in zip(consumer1 , consumer2) :
print("="*50)
print ("topic=%s partition=%d offset=%d: key=%s value=%s" % (message.topic, message.partition,
message.offset, message.key,
message.value))
print ("topic=%s partition=%d offset=%d: key=%s value=%s" % (msg.topic, msg.partition,
msg.offset, msg.key,
msg.value))
key = str(message.offset) + " " + str(msg.offset)
producer.send('output', value= { key : key } ).add_callback(on_send_success)
print("="*50)
实际上,我想让两台计算机对模型的表示进行ex输入并合并两个结果。
相反,合并时必须保持相同的偏移量。
之后,我想发送结果输出主题。
在output
主题中,我的预期结果是这样的:
{offset = 1:offest = 1},{offset = 2:offest = 2},....
请帮助我!我解决不了
答案 0 :(得分:0)
您能否进一步说明您的预期结果是什么?
如果您要尝试
consumer1 = KafkaConsumer( bootstrap_servers=['localhost:9092'], #auto_offset_reset='earliest' , # 'earliest', #enable_auto_commit= False , group_id='sr', value_deserializer=lambda x: loads(x.decode('utf-8'))) consumer1.subscribe("numtest") consumer2 = KafkaConsumer( bootstrap_servers=['localhost:9092'], #auto_offset_reset='earliest' , # 'earliest', #enable_auto_commit= False , group_id='sr', value_deserializer=lambda x: loads(x.decode('utf-8'))) consumer2.subscribe("numtest")