繁体   English   中英

如何在Logstash中输入一个JSON的文件(数组形式)?

[英]How to input a JSON file (array form) in Logstash?

我尝试了很多东西来输入我的 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 文件格式(都在同一行):

[{"id":1,"something":"text1"},{"id":2,"something":"text2"},{"id":3,"something":"text3"}]

如果可以的话,我将不胜感激。

问题解决了,这是因为编码,我使用jq实用程序将我的 JSON 文件转换为正确的格式(对于 Logstash),即:

{"id":1,"something":"text1"}
{"id":2,"something":"text2"}
{"id":3,"something":"text3"}

起初我没有注意到,但是 jq 在转换过程中更改了我的文件的编码,它最终以 UTF-16 LE 结束。 因此,我使用 VS Code 将编码更改(并保存)为 UTF-8,之后它运行良好。

我现在可以使用的更新代码:

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"
    }
}

对于那些感兴趣的人,我使用此命令行将我的 JSON 文件转换为正确的格式(Windows 命令行):

type Docs.json | jq -c '.[]' > Docs_formatted.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM