kafka-console-producer 和 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38701179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:22:40  来源:igfitidea点击:

kafka-console-producer and bash script

bashapache-kafka

提问by scala

Want to send some message by bash script.

想通过 bash 脚本发送一些消息。

  bin/zookeeper-server-start.sh config/zookeeper.properties > zookeeper.log 2>&1 &
sleep 2

bin/kafka-server-start.sh config/server.properties > server.log 2>&1 &
sleep 2

#Create topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic 
sleep 2

#Get topics list
echo "kafka has next topics:"
bin/kafka-topics.sh --list --zookeeper localhost:2181

#send message
echo "will send messages:"
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic  "test 1"

Kafka started well. And I can send message by the producer console

卡夫卡开局不错。我可以通过生产者控制台发送消息

 bin/kafka-console-producer.sh --broker-list localhost:9092 --topic  "test 1"

But I can't send message into bash script. How can I send it by bash script ?

但我无法将消息发送到 bash 脚本中。如何通过 bash 脚本发送它?

Thanks.

谢谢。

回答by serejja

Try doing this:

尝试这样做:

echo "test 1" | bin/kafka-console-producer.sh --broker-list localhost:9092 --topic 

or this:

或这个:

cat file.txt | bin/kafka-console-producer.sh --broker-list localhost:9092 --topic 

回答by AtulyaB

You can also do the the below. Ignore the sleep parameter.

您还可以执行以下操作。忽略睡眠参数。

for x in {1..100}; do echo $x; sleep 2; done | path/to/bin/kafka-console-producer --broker-list <brk:port> --topic <topic_name>

回答by Pedro Silva

This also works:

这也有效:

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic "my-topic" < file.txt

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic "my-topic" < file.txt