[英]Consuming json values from a kafka topic and writing them and formatting them in a csv file using JQ
I am trying to write into a csv file keys and values that are in a kafka topic. 我正在尝试将cafka主题中的键和值写入一个csv文件中。 I have been able to select the keys and values that I want, but I am not able to get them separated by rows (three values per row values in rows separated by commas).
我已经能够选择所需的键和值,但无法将它们按行分开(行中的三个值用逗号分隔)。
This is an example of two json records that I consumed from my kafka topic without doing any filtering. 这是我从kafka主题中使用的两个json记录的示例,没有进行任何过滤。 The command that I used is:
我使用的命令是:
./kafka-run-class.sh kafka.tools.ConsoleConsumer --bootstrap-server kafka1.example.net:9092 --topic prod.example.v1 --max-messages 2 | jq -r '. '
{ "count": "0", "source": 3, "lastModified": "2018-03-09T21:03:54.039Z", "isBusiness": false, "countryCode": " MX", "phone": "52/4446789864" } { "count": "0", "source": 3, "lastModified": "2018-03-09T21:03:54.039Z", "isBusiness": false, "countryCode": " GB", "phone": "44/0187567846" }
I tried using this command, but each value is being put into its own row: 我尝试使用此命令,但是每个值都放在其自己的行中:
./kafka-run-class.sh kafka.tools.ConsoleConsumer --bootstrap-server kafka1.example.net:9092 --topic prod.example.v1 --max-messages 3 | jq -r ' .isBusiness, .countryCode, .phone ' > file.csv
Ideal output would be: 理想的输出为:
false, MX, 52/4446789864
false, GB, 44/0187567846
true, BE, 32/8745687645
jq -r '[.isBusiness, .countryCode, .phone] | @csv'
produces CSV: 产生CSV:
false," MX","52/4446789864"
false," GB","44/0187567846"
The filter: 过滤器:
"\(.isBusiness), \(.countryCode), \(.phone)"
produces 产生
false, MX, 52/4446789864
false, GB, 44/0187567846
You might want to "trim" the string values, eg using: 您可能想要“修剪”字符串值,例如使用:
def trim: sub("^ +";"") | sub(" +$";"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.