在Logstash中使用任意键解析JSON对象
考虑以下示例输出的子集: 键在Logstash中使用任意键解析JSON对象,json,logstash,Json,Logstash,考虑以下示例输出的子集: 键“hg.nginx.org”和“track.nginx.org”非常随意,我想将它们解析为对Elasticsearch有意义的东西。换句话说,“服务器区域”下的每个键都应转换为单独的事件。因此,Logstash应发出以下事件: [ { "timestamp": 1516053885198, "server_zone": "hg.nginx.org", ... // Data for "hg.nginx.org"
“hg.nginx.org”
和“track.nginx.org”
非常随意,我想将它们解析为对Elasticsearch有意义的东西。换句话说,“服务器区域”
下的每个键都应转换为单独的事件。因此,Logstash应发出以下事件:
[
{
"timestamp": 1516053885198,
"server_zone": "hg.nginx.org",
... // Data for "hg.nginx.org"
},
{
"timestamp": 1516053885198,
"server_zone": "trac.nginx.org",
... // Data for "trac.nginx.org"
}
]
执行此操作的最佳方法是什么?您可以尝试使用ruby过滤器。获取服务器区域并使用要包含的键值对创建新对象。从我的头顶上看,下面这样的东西应该会起作用。显然,然后需要将对象映射到索引中的字段。根据您的自定义格式更改剪接,即根据需要构建阵列或对象
filter {
ruby {
code => " time = event.get('timestamp')
myArr = []
event.to_hash.select {|k,v| ['server_zones'].include?(k)}.each do |key,value|
myCustomObject = {}
#map the key value pairs into myCustomObject
myCustomObject[timestamp] = time
myCustomObject[key] = value
myArr.push(myCustomObject) #you'd probably move this out based on nesting level
end
map['my_indexed_field'] = myArr
"
}
}
在输出部分,使用rubydebug进行错误调试
output {
stdout { codec => rubydebug }
}
output {
stdout { codec => rubydebug }
}