Shellbye.github.io icon indicating copy to clipboard operation
Shellbye.github.io copied to clipboard

用Logstash导入文本文件到Elasticsearch

Open Shellbye opened this issue 6 years ago • 0 comments

安装所需软件

ES的安装参考这里 #5 ,Logstash的安装参考这里 #7

文本文件

注意这里有一个问题需要注意一下,就是如果你一直在尝试导入一个(比较小的)文件(就像我在这个demo中做的),那么logstash后面就不会继续去导入了,因为logtash会通过sincedb_path所指定的位置去记录当前文件的导入位置,如果你已经导入完成了,那么后续再导入就不会执行了(别问我怎么知道的😂)。

shellbye@localhost:~$ cat text.txt
apple $2 01
banana $4 02
book $44 03
desk $55 04

Logstash配置文件

与上一篇 #10 导入json文件不同,导入文本文件需要使用到grok这个filter,用来描述文本文件的结构。这里有一个比较方便的调试工具 grokdebug

input {
  file {
    path => "/home/shellbye/text.txt"
    start_position => "beginning"
  }
}

filter {
   grok {
    match => { "message" => "%{DATA:name} %{DATA:price} %{WORD:no}" }
  }
}

output {
    stdout {}
    elasticsearch {
        index => "demo_index002"
        hosts => ["localhost:9200"]
    }
}

执行

/path/to/bin/logstash -f /home/shellbye/f.conf

Shellbye avatar Apr 27 '18 05:04 Shellbye