简体   繁体   English

json输入文件的logstash问题

[英]logstash issue with json input file

i have the following json in a file- 我在文件中有以下json-

{
"foo":"bar",
"spam" : "eggs"
},
{
"css":"ddq",
"eeqw": "fewq"
}

and the following conf file- 和以下conf文件-

input { 
file
{ 
   path => "/opt/logstash-1.4.2/bin/sam.json"
   type => "json"
   codec => json_lines
   start_position =>"beginning"
 }
}
output { stdout {  codec => json  } }

but when i run 但是当我跑步时

./logstash -f sample.conf

i don't get any output in stdout. 我在标准输出中没有任何输出。

but when i don't give json as codec and give type => "core2" then it seems to work. 但是当我不给json作为编解码器并给类型=>“ core2”时,它似乎可以工作。 Anyone know how i can fix it to work for json type. 任何人都知道我如何修复它以适用于json类型。

The other issue is it gives me the following output when it does give stdout- 另一个问题是,当它给出stdout-时,它给了我以下输出-

{"message":"{","@version":"1","@timestamp":"2015-07-15T02:02:02.653Z","type":"core2","host":"sjagannath","path":"/opt/logstash-1.4.2/bin/sam.json"}{"message":"\"foo\":\"bar\", ","@version":"1","@timestamp":"2015-07-15T02:02:02.654Z","type":"core2","host":"sjagannath","path":"/opt/logstash-1.4.2/bin/sam.json"}{"message":"\"spam\" : \"eggs\" ","@version":"1","@timestamp":"2015-07-15T02:02:02.655Z","type":"core2","host":"sjagannath","path":"/opt/logstash-1.4.2/bin/sam.json"}{"message":"},","@version":"1","@timestamp":"2015-07-15T02:02:02.655Z","type":"core2","host":"sjagannath","path":"/opt/logstash-1.4.2/bin/sam.json"}{"message":"{ ","@version":"1","@timestamp":"2015-07-15T02:02:02.655Z","type":"core2","host":"sjagannath","path":"/opt/logstash-1.4.2/bin/sam.json"}{"message":"\"css\":\"ddq\", ","@version":"1","@timestamp":"2015-07-15T02:02:02.656Z","type":"core2","host":"sjagannath","path":"/opt/logstash-1.4.2/bin/sam.json"}{"message":"\"eeqw\": \"fewq\"","@version":"1","@timestamp":"2015-07-15T02:02:02.656Z","type":"core2","host":"sjagannath","path":"/opt/logstash-1.4.2/bin/sam.json"}{"message":"}","@version":"1","@timestamp":"2015-07-15T02:02:02.656Z","type":"core2","host":"sjagannath","path":"/opt/logstash-1.4.2/bin/sam.json"}{"message":"","@version":"1","@timestamp":"2015-07-15T02:02:02.656Z","type":"core2","host":"sjagannath","path":"/opt/logstash-1.4.2/bin/sam.json"}

I want to know how it can be parsed the right way with the key value pairs in my input file 我想知道如何用输入文件中的键值对正确解析它

I found this and edited it to suit your purpose. 我找到了这个,并对其进行了编辑以适合您的目的。 The following config should do exactly what you want: 以下配置应完全满足您的要求:

input {   
file     {
    codec => multiline
    {
        pattern => "^\}"
        negate => true
        what => previous               
    }
    path => ["/absoute_path/json.json"]
    start_position => "beginning"
    sincedb_path => "/dev/null"
}
}

filter {
mutate    {
    replace => [ "message", "%{message}}" ]
    gsub => [ "message","\n",""]
    gsub => [ "message","},",""]
}
if [message] =~ /^{.*}$/     {
    json { source => message }
}
}

I tried your given json and it results in two events. 我尝试了给定的json,它导致了两个事件。 First with foo = bar and spam = eggs . 首先使用foo = barspam = eggs Second with css = ddq and eeqw = fewq . 其次是css = ddq eeqw = fewqeeqw = fewq

As of my understanding you want to put your complete son document on one line if you want to use the json_lines codec: 据我了解,如果您想使用json_lines编解码器,则希望将完整的子文档放在一行上:

{"foo":"bar","spam" : "eggs"}
{"css":"ddq","eeqw": "fewq"}

In your case you have a problem with the structure since you also have a ',' between the son objects. 在您的情况下,您的结构存在问题,因为子对象之间也有一个“,”。 Not the most easy way to handle it. 不是处理它的最简单方法。 SO if possible change the source to my example. 因此,如有可能,将源更改为我的示例。 If that is not possible the multiline approach might help you. 如果不可能,那么多行方法可能会为您提供帮助。 Check this for reference: input json to logstash - config issues? 检查以供参考:将json输入到logstash-配置问题?

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

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