logstash是基于Java的服务,各操做系统安装Java环境都可使用。
Java
https://www.java.com/zh_CN/
安装后配置好java环境变量。
logstash
最新版 https://www.elastic.co/downloads/logstash
2.3.4版 https://www.elastic.co/downloads/past-releases/logstash-2-3-4html
#输入 Input{ Jdbc{} } #加工过滤 Filter{ Json{} } #输出 Output{ elasticsearch{} }
Input: elasticsearch,file,http_poller,jdbc,log4j,rss,rabbitmq,redis,syslog,tcp,udp…等java
Filter: grok,json,mutate,split …等redis
Output: email,elasticsearch,file,http,mongodb,rabbitmq,redis,stdout,tcp,udp …等sql
配置说明地址: https://www.elastic.co/guide/en/logstash/current/input-plugins.htmlmongodb
#jdbc_demo input { jdbc { #数据库连接设置 jdbc_driver_library => "k:\k\k\sqljdbc_6.0\chs\sqljdbc42.jar" jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver" jdbc_connection_string => "jdbc:sqlserver://192.168.0.1;user=sa;password=sa;DatabaseName=EStatistic;" jdbc_user => "sa" jdbc_password => "sa" statement_filepath => "../config-demo/sqlscript/jdbc_demo.sql" #sql脚本 schedule => "* * * * *" #执行计划 record_last_run => true #记录最后运行值 use_column_value => true # tracking_column => id #要记录的字段 last_run_metadata_path => "../config-demo/log/jdbc_demo" #记录的位置 lowercase_column_names => true #将字段名所有改成小写 clean_run => false jdbc_fetch_size => 1000 #分页设置 jdbc_page_size => 1000 jdbc_paging_enabled => true } } filter { mutate { #重命名,能够将字段名改掉 rename => { "id" => "Id" "bid" => "BId" "saleconsultantid" => "SaleConsultantId" "avgreplyduration" => "AvgReplyDuration" "avgreplyratio" => "AvgReplyRatio" "avgonlineduration" => "AvgOnlineDuration" "stattime" => "StatTime" } } } output { elasticsearch { hosts => ["192.168.0.1:9200"] #es 服务地址 index => "jdbc_demo" #索引的名字 document_type => "demoinfo" #类型的名字 workers => 1 #输出时进程数 document_id=>"%{Id}" #文档的惟一ID template => "../config-demo/templates/jdbc_demo.json" #模板的路径 template_name => "jdbc_demo" #模板的名字 template_overwrite => false ##是否覆盖已存在的模板 } # stdout{ # codec => rubydebug # } }
索引模板数据库
{ "order": 1, "template": "jdbc_demo", //模板匹配规则,已经索引名匹配(eg:jdbc_demo-*,能够匹配到,jdbc_demo-1,jdbc_demo-14...) "settings": { "index.number_of_shards": 4, //分片数 "number_of_replicas": 1 //每一个分配备份数 }, "mappings": { "_default_": { "_source": { "enabled": true } }, "demoinfo": { //动态模板 若是映射后,有新的字段添加进来,而且在字段区域没有映射会按该动态模板匹配映射 "dynamic_templates": [ { "string_field": { "match": "*", "match_mapping_type": "string", //匹配到全部字符串 设置为不分词 "mapping": { "index": "not_analyzed", "type": "string" } } } ], //类型名 "_source": { "enabled": true }, "_all": { "enabled": false }, "properties": { //字段区域 "Id": { "type": "long" }, "BId": { "type": "integer" }, "SaleConsultantId": { "type": "integer" }, "AvgReplyDuration": { "type": "integer" }, "AvgReplyRatio": { "type": "double" }, "AvgOnlineDuration": { "type": "integer" }, "StatTime": { "format": "strict_date_optional_time||epoch_millis", "type": "date" } } } }, "aliases": { "logstashdemo": {} //别名,匹配到该模板的会设置一个别名 } }
Sqlserver 查询语句json
SELECT [Id] ,[BId] ,[SaleConsultantId] ,[AvgReplyDuration] ,[AvgReplyRatio] ,[AvgOnlineDuration] ,[StatTime] FROM [EStatistic].[dbo].[StatSessionBy7Day] WHERE Id>:sql_last_value --:sql_last_value是记录的最后的一个值
5.x模板String字段ruby
{"properties": { //字段区域 "NewField": { "type": "keyword", // keyword 不分词 "index": false //不创建索引 }, "NewFieldText": { "type": "text", // text分词 "index": true // 创建索引 } } }