[Spark][kafka]kafka 生产者,消费者 互动例子

时间:2024-01-12 17:36:38

[Spark][kafka]kafka 生产者,消费者 互动例子

# pwd
/usr/local/kafka_2.11-0.10.0.1/bin

创建topic:
# ./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic device_status
WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.
Created topic "device_status".
#

查看topic:
# ./kafka-topics.sh --list --zookeeper localhost:2181
device_status
#

生产者生成消息:
# ./kafka-console-producer.sh --broker-list localhost:9092 --topic device_status

消费者获得消息:
# pwd
/usr/local/kafka_2.11-0.10.0.1/bin

# ./kafka-console-consumer.sh --zookeeper localhost:2181 --topic device_status --from-beginning

在生产者窗口输入:
hi, I am producer 然后按回车

在消费者窗口会看到:
hi, I am producer