我正在尝试使用shell脚本生成有关我的Kafka主题的数据。
首先,我编写一个shell脚本run_producer.sh
:
#!/bin/sh
./bin/kafka-avro-console-producer --broker-list localhost:9092 --topic AAATest2 \
--property "parse.key=true" \
--property "key.separator=:" \
--property key.schema='{"type":"string"}' \
--property value.schema='{"type":"record","name":"myrecord","fields":[{"name":"measurement","type":"string"},{"name":"id","type":"int"}]}'
执行"key1":{"measurement": "INFO", "id": 1}
时,您需要在命令行中输入类似run_producer.sh
的字符串,并且可以输入任意数量的
我写了另一个脚本add_data.sh
:
#!/bin/sh
s="\"key1\":{\"measurement\": \"INFO\", \"id\": 1}"
printf "${s}\n${s}\n" | ./run_producer.sh
它可以输入两次或两次以上的字符串,只需在printf
中添加“ $ {s} \ n”,但这是有限且愚蠢的。
我想让它无休止地输入字符串,直到停止为止。我该如何使用shell脚本呢?
如果您能告诉我如何使字符串不同(不同的数据),我将不胜感激。
答案 0 :(得分:0)
您可以使用yes "$s"
为脚本生成无尽的输入。但是,“以不同的方式制作字符串”意味着什么?足以对一些随机数据使用无限循环,例如
while true; do s="\"key1\":{\"measurement\": \"INFO\", \"id\": $RANDOM}"; echo $s; done
还是您需要以其他方式对其进行修改?
man bash
:
RANDOM Each time this parameter is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.
您可以将其与其他任何内容组合,例如:"key_${RANDOM}"
或选择其他任何方法,例如this或https://gist.github.com/earthgecko/3089509