[英]How to input a JSON file (array form) in Logstash?
I tried many things in order to input my JSON file (array form), I don't have any results so far我尝试了很多东西来输入我的 JSON 文件(数组形式),到目前为止我没有任何结果
input{
file{
path=> "/usr/share/logs/Docs.json"
#codec=> "json" tried
#codec=> json {charset => "ISO-8859-1"} tried
#codec => json{ } tried
start_position=> "beginning"
sincedb_path=> "/dev/null"
}
}
filter{
json{
source=> "message"
}
}
output{
stdout{
codec=> "json"
}
elasticsearch{
hosts=> ["http://es1:9200"]
index=> "index_abc"
}
}
JSON file format (all on the same line): JSON 文件格式(都在同一行):
[{"id":1,"something":"text1"},{"id":2,"something":"text2"},{"id":3,"something":"text3"}]
If you could me I would appreciate it very much.如果可以的话,我将不胜感激。
Problem solved, it was because of the encoding, I used the jq utility in order to transform my JSON file to the right format (for Logstash), which is:问题解决了,这是因为编码,我使用jq实用程序将我的 JSON 文件转换为正确的格式(对于 Logstash),即:
{"id":1,"something":"text1"}
{"id":2,"something":"text2"}
{"id":3,"something":"text3"}
and I didn't notice at first but jq changed the encoding of my file during the transformation, it ended up in UTF-16 LE.起初我没有注意到,但是 jq 在转换过程中更改了我的文件的编码,它最终以 UTF-16 LE 结束。 So, I changed (and saved) the encoding to UTF-8 with VS Code, and it worked fine afterwards.
因此,我使用 VS Code 将编码更改(并保存)为 UTF-8,之后它运行良好。
My updated code that works now:我现在可以使用的更新代码:
input{
file{
path=> "/usr/share/logs/Docs_formatted.json"
codec=> "json"
start_position=> "beginning"
sincedb_path=> "/dev/null"
}
}
filter{}
output{
stdout{}
elasticsearch{
hosts=> ["http://es1:9200"]
index=> "index_abc"
}
}
For those who are interested, I used this command line in order to transform my JSON file to the right format (Windows command line):对于那些感兴趣的人,我使用此命令行将我的 JSON 文件转换为正确的格式(Windows 命令行):
type Docs.json | jq -c '.[]' > Docs_formatted.json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.