Logstash是處理動態文件一個很好的工具。
下面是一個使用logstash導入您的JSON文件到elasticsearch方式:
配置文件:
{"foo":"bar", "bar": "foo"}
{"hello":"world", "goodnight": "moon"}
注意JSON必須是:JSON文件的
input
{
file
{
path => ["/path/to/json/file"]
start_position => "beginning"
sincedb_path => "/dev/null"
exclude => "*.gz"
}
}
filter
{
mutate
{
replace => [ "message", "%{message}" ]
gsub => [ 'message','\n','']
}
if [message] =~ /^{.*}$/
{
json { source => message }
}
}
output
{
elasticsearch {
protocol => "http"
codec => json
host => "localhost"
index => "json"
embedded => true
}
stdout { codec => rubydebug }
}
例子在一行中。如果你想解析多行json文件,請替換配置文件中的相關字段:
input
{
file
{
codec => multiline
{
pattern => '^\{'
negate => true
what => previous
}
path => ["/opt/mount/ELK/json/*.json"]
start_position => "beginning"
sincedb_path => "/dev/null"
exclude => "*.gz"
}
}
filter
{
mutate
{
replace => [ "message", "%{message}}" ]
gsub => [ 'message','\n','']
}
if [message] =~ /^{.*}$/
{
json { source => message }
}
}
我們說logstash無法處理大量帶有單個JSON條目的文件嗎?否則,現在您必須確定是否存在超過1行,使用curl,否則導入到Logstash中。當然,單行文件與Logstash的觀點沒有什麼不同? – 2014-10-09 18:49:36
我對logstash和json文件沒有提及。我只是說,如果logstash無法做到你想做的事情,並且如果數據已經在JSON中,那麼做logstash的做法是相當簡單的。 – 2014-10-10 23:53:55