在 Linux 下向 Kafka 发送(生产)消息,最常见的是使用 Kafka 自带的命令行工具。下面按最常用方式一步步说明。
/opt/kafka 或 /usr/local/kafka)test)# 启动 Zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties
# 启动 Kafka
bin/kafka-server-start.sh config/server.properties
bin/kafka-topics.sh \
--create \
--topic test \
--bootstrap-server localhost:9092 \
--partitions 1 \
--replication-factor 1
bin/kafka-console-producer.sh \
--topic test \
--bootstrap-server localhost:9092
然后直接输入消息:
hello kafka
this is a message
按 Ctrl + C 退出。
echo "hello kafka" | bin/kafka-console-producer.sh \
--topic test \
--bootstrap-server localhost:9092
bin/kafka-console-producer.sh \
--topic test \
--bootstrap-server localhost:9092 < messages.txt
bin/kafka-console-consumer.sh \
--topic test \
--bootstrap-server localhost:9092 \
--from-beginning
bin/kafka-console-producer.sh \
--topic test \
--bootstrap-server localhost:9092 \
--property "parse.key=true" \
--property "key.separator=:"
输入格式:
user1:hello
user2:world
kafka-pythonsarama如果你愿意,可以告诉我:
我可以给你更精确的命令。