logstash导出ElasticSearch数据到CSV及同步两套ES的数据研究

 

一、安装

参考:https://www.cnblogs.com/cpy-devops/p/9287531.html

首先保证系统安装了java 1.8以上版本

tar –zxvf /home/logstash-7.7.0.tar.gz -C /usr/local

进入/usr/local/logstash-7.7.0/bin目录下

二、配置文件

创建文件 convert_csv.conf

input{
      elasticsearch {
         hosts => ["127.0.0.1:9200"]    #要导出来的es服务器的地址
         index => "es的数据index值"      
     }
}
output{
    file {

     filed => [””,””,””]    #filed字段选择,如下图展示_source中的信息,带有下划线的字段不需要选择
#path为指定文件路径
      path => "/home/csv.csv"
    }

}

 

image

三、执行

./logstash -f convert_csv.conf

同步两套ES数据

只需要将上述output修改对应的es的output即可

output {

    elasticsearch {
        #全文检索服务
        hosts => "localhost:9200"
        #索引(数据库)
        index => "zl_dev"
        #类型(数据库表)
        document_type => "%{type}"
        #主键(防止重复)
        document_id => "%{id}"
    }
}

 

实时同步es数据

官方帮助文档:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-elasticsearch.html

需要插件 Elasticsearch input plugin

input { # Read all documents from Elasticsearch matching the given query elasticsearch

{ hosts => "localhost"

  query => '{ "query": { "match": { "statuscode": 200 } }, "sort": [ "_doc" ] }' } }

这里query的学习,请参考https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

我参考了下 https://www.cnblogs.com/-fengmu/p/13074152.html

可以按照时间 每天进行数据采集

 

{
    "query": {
        "match": {
            "fileddate":"2020-06-22"   #根据fileddate字段,取得日期2020-06-22数据
        }
    }
}

 

计划增加 输入读取选项 schedule index 

input { # Read all documents from Elasticsearch matching the given query elasticsearch { hosts => "localhost" index => "按照时间索引传入" query => '{ "query": { "match": { "statuscode": 200 } }, "sort": [ "_doc" ] }' schedule => "* * 1 * *" #每天凌晨1点执行 #任务调度 (分、时、天、月、年,全部为*默认含义为每分钟都更新) } }

输出与上述输出到es一致即可

 

 

 

 

posted @ 2020-06-15 13:50  Tim&Blog  阅读(3485)  评论(0编辑  收藏  举报