我在shell脚本中使用了sleep
函数来延迟,但似乎效果不佳。
首先,我将run_producer.sh
编写如下:
#!/bin/sh
./bin/kafka-avro-console-producer --broker-list localhost:9092 --topic stressInfluxDB1 \
--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"},{
"name":"desc","type":"string"}]}'
执行"keyV":{"measurement": "INFO", "id": 0, "desc": "descriptons"}
时,需要在命令行中输入类似run_producer.sh
的字符串,并且可以输入任意数量的字符串。这不是重点。
然后我写add_data.sh
,如下所示:
cnt=1;
num=20;
while [ $cnt -le $num ]
do
echo "\"keyV\":{\"measurement\": \"INFO\", \"id\": ${cnt}, \"desc\": \"descri_${cnt}\"}";
sleep 1s
((cnt++))
done | ./run_producer.sh
我想回声20次,每次回声后延迟1秒钟。因为我将这些数据发送到InfluxDB中,所以可以获得以下时间戳:
time desc id
---- ---- --
2019-03-25T04:30:16.548Z descri_1 1
2019-03-25T04:30:16.59Z descri_2 2
2019-03-25T04:30:16.594Z descri_3 3
2019-03-25T04:30:16.598Z descri_4 4
2019-03-25T04:30:16.599Z descri_5 5
2019-03-25T04:30:16.602Z descri_6 6
2019-03-25T04:30:16.604Z descri_7 7
2019-03-25T04:30:16.66Z descri_8 8
2019-03-25T04:30:17.674Z descri_9 9
2019-03-25T04:30:18.684Z descri_10 10
2019-03-25T04:30:19.701Z descri_11 11
2019-03-25T04:30:20.711Z descri_12 12
2019-03-25T04:30:21.726Z descri_13 13
2019-03-25T04:30:22.735Z descri_14 14
2019-03-25T04:30:23.751Z descri_15 15
2019-03-25T04:30:24.765Z descri_16 16
2019-03-25T04:30:25.774Z descri_17 17
2019-03-25T04:30:26.779Z descri_18 18
2019-03-25T04:30:27.794Z descri_19 19
2019-03-25T04:30:28.8Z descri_20 20
如您所见,前8条消息不会以1秒的间隔发送。
为什么会这样,我该如何解决?