from pykafka import KafkaClient
import threading
KAFKA_HOST = "localhost:9092" # Or the address you want
client = KafkaClient(hosts = KAFKA_HOST)
topic = client.topics["test"]
with topic.get_sync_producer() as producer:
for i in range(10):
message = "Test message " + str(i)
encoded_message = message.encode("utf-8")
producer.produce(encoded_message)
1条答案
按热度按时间j13ufse21#
首先,安装“pykafka”=>pip安装pykafka
然后启动消费者(例如在终端中),运行以下命令:
您将能够看到:
测试消息0
测试消息1
测试消息2
...
测试消息9