ELK生态:Logstash读取json格式文件数据,导入到Elasticsearch

简介

  1. ELK生态之Logstash导入数据到Elasticsearch;
  2. 数据源:json格式文件,内容为json
  3. Elasticsearch和Logstash版本:5.6.1
  4. 前提环境:Elasticsearch单机或集群;Logstash客户端

实践

  • json文件内容:
{"name":"sixmonth","age":"23","sex":"男","address":"深圳","phone":"23456"}
  • logstash.conf配置:

#读取json文件
input {
  file {
    #设置json文件路径,多个文件路径可设置成数组[],模糊匹配用*
    #指定单一文件
    path => "/data/es/jsonstash-5.6.1/files/test.json"
    #指定数组文件
    #path => ["/data/es/jsonstash-5.6.1/files/test-1.json","/data/es/jsonstash-5.6.1/files/test-2.json"]
    #指定同级目录模糊匹配
    #path => "/data/es/jsonstash-5.6.1/files/test*.json"
    #指定多级目录模糊匹配
    #path => "/data/es/jsonstash-5.6.1/files/**/test*.json"
    
    start_position => "beginning"
    
    #设置编码
    codec => json {charset => "UTF-8"}
    
    #当存在多个文件的时候可使用type指定输入输出路径
    type => "json_index"
  }
}

#2.过滤格式化数据阶段
filter {
    
    mutate{
        #删除无效的字段
        remove_field => ["@version","message","host","path"]
    }
    
    #新增timestamp字段,将@timestamp时间增加8小时
    ruby { code => "event.set('timestamp', event.get('@timestamp').time.localtime + 8*60*60)" }
    
}

#3.数据输出到ES阶段
output {

    #日志输出格式,json_lines;rubydebug等
    stdout {
        codec => rubydebug
    }
    
    #输出到es
    if[type] == "json_index"{
    
        #无法解析的json不记录到elasticsearch中
        if "_jsonparsefailure" not in [tags] {
            elasticsearch {
                #es地址ip端口
                hosts => "127.0.0.1:9200"
                #索引
                index => "json_index"
                #类型
                document_type => "json_index"
            
                #覆盖模板,不需要可注释掉,通用模板下载:https://download.csdn.net/download/alan_liuyue/11241484
                #template_overwrite=>true
                #template=>"/data/es/logstash-5.6.1/template/logstash.json"        
                
            }
        }
    }
    
}
  •  补充file-input字段说明:
codec => #可选项,默认是plain,可设置其他编码方式;

discover_interval => #可选项,logstash多久检查一下path下有新文件,默认15s;

exclude => #可选项,排除path下不想监听的文件;

sincedb_path => #可选项,记录文件以及文件读取信息位置的数据文件;

sincedb_write_interval => #可选项,logstash多久写一次sincedb文件,默认15s;

stat_interval => #可选项,logstash多久检查一次被监听文件的变化,默认1s;

start_position => #可选项,表示从哪个位置读取文件数据,初次导入为:beginning,最新数据为:end

path => #必选项,配置文件路径,可定义多个,也可模糊匹配;

tags => #可选项,在数据处理过程中,由具体的插件来添加或者删除的标记;

type => #可选项,当有多个file的时候,可用于一对一匹配输入或者输出;

总结

实践是检验认识真理性的唯一标准,自己动手丰衣足食~

发布了79 篇原创文章 · 获赞 276 · 访问量 57万+

猜你喜欢

转载自blog.csdn.net/alan_liuyue/article/details/92635108