logstash采集规范与elasticsearch的template 、mapping 详细介绍
2018-03-14 18:39
866 查看
logstash 采集 kafka 写入es 流程规范
请尊重知识产权,博客原文地址 http://blog.csdn.net/qq1032355091/article/details/79558649本文主要介绍es 的 template和mapping 配置,通过logstash的采集流程规范,更加容易深入理解到底什么是template和mapping配置
背景知识
什么是mapping?
mapping定义了index中的每个type的每个字段的属性,主要是字段类型(integer,long,text,keyword,date,float,ip,geo)什么是template?
如果用户每次新建一个索引的时候都需要手动创建mapping非常麻烦,es内部维护了template,template定义好了mapping,只要index的名称被template匹配到,那么该index的mapping就按照template中定义的mapping自动创建。而且template中定义了index的shard分片数量、replica副本数量等等属性。logstash写入es建立mapping的3种方式:
logstash 写入 es,在es新建index的时候,新的index的mapping建立有3种方式:1. logstash内部自带默认template,通过该template去创建mapping。
2. es集群内部写入template,logstash写入的时候不需要指定template,index根据es集群内部存在的template自动匹配创建mapping。
3. logstash写入的时候通过template 属性指定本地磁盘template文件路径,logstash读取该文件来建立index的mapping。
根据实际经验,采取第二种方式,最稳定且最容易维护。
template结构
什么是动态模板?首先有2个问题,如果一个index下有新的type写入,之前没有定义怎么办?如果一个type下有新的字段写入,之前没有定义怎么办?
如果一个index下有新的type写入,之前没有定义,则根据_default_定义的属性来匹配生成。如果一个type下有新的字段写入,之前没有定义,则根据该type下的dynamic_templates来匹配生成。
template定义了要匹配的索引名,例如httpinfo-2017-01-01该索引名就会被匹配到。settings定义了索引的属性,包括分片数量、副本数量、写入flush时间间隔。
mappings定义了该index下每个type的属性,如果该index的某个type没有定义,则根据_default_定义的属性来匹配。某个type定义了各个字段的类型属性,在properties,如果有字段没有在properties中定义属性,则根据dynamic_templates中定义的动态模板指定识别新字段的属性
采集操作流程
在测试es环境中做以下操作:
1. 编写template,写入es复制附件中的template示例,修改其中标红的部分。进入测试环境kibana界面http://131.23.41.128/app/kibana#/dev_tools/console?_g=()
在dev tools界面下使用put _template/[template名]{……}命令写入es
通过命令 get_template/[template名]查看是否写入template成功。如果template编写错误重新执行put命令覆盖即可
2. 编写采集脚本,使用测试group名,采集kafka数据 ,使用rubydebug output输出,在控制台检查输出数据格式是否正确
input { kafka{ group_id=> "bmkp_test_logstash_httpinfo_test001" topics=> "httpInfo" bootstrap_servers=> "10.0.23.17:9092" key_deserializer_class=> "org.apache.kafka.common.serialization.IntegerDeserializer" value_deserializer_class=> "org.apache.kafka.common.serialization.StringDeserializer" auto_offset_reset=> "earliest" client_id=> "httpinfo2testes" type=> "httpinfo" } } fiter {……} output { if[type] == "httpinfo" { stdout { codec => rubydebug } } }
3. 在采集脚本中添加elasticsearch output,仍然使用测试group名,运行logstash,检查测试环境elasticsearch中的数据是否正常注意:elasticsearch output只需要指定hosts和index,不需要指定template相关的属性。input {
kafka{
group_id=> "bmkp_test_logstash_httpinfo_test001"
topics=> "httpInfo"
bootstrap_servers=> "10.0.23.17:9092"
key_deserializer_class=> "org.apache.kafka.common.serialization.IntegerDeserializer"
value_deserializer_class=> "org.apache.kafka.common.serialization.StringDeserializer"
auto_offset_reset=> "earliest"
client_id=> "httpinfo2testes"
type=> "httpinfo"
}
}
filter {……}
output {
if[type] == "httpinfo" {
elasticsearch {
hosts => ["10.0.23.9:9200"]
index => "httpinfo-%{+yyyy-MM-dd}"
}
stdout {
codec => rubydebug
}
}
}
l 使用get _cat/indices/[index_name] 命令检查es中是否已经建立了index
使用get /[index_name]/_search命令检查该index下是否已经写入了数据
l 使用get /httpinfo-2018-01-10/_mapping/命令检查该index的mapping是否正确
l 如果index中的数据格式不对或者mapping不对,执行delete [index_name]命令删除该index即可,然后修改问题重新采集
4. 删除调试测试产生的index数据执行delete [index_name] 命令即可。
5. 到此为止测试环境测试通过,提交logstash采集的配置文件和template.json文件给大数据组,由大数据组部署测试环境并且修改文件部署到生产环境。
生产环境中做以下操作:
1. 生产环境kibana写入template,并检查结果。2. 利用测试group名采集kafka,写入到生产es,并在生产es中检查建立的index的数据是否正确,mapping是否正确。
3. 删除生产es中测试建立的index。
4. 修改logstash采集文件的group名为正式采集group名,执行部署,完成。
请尊重知识产权,博客原文地址 http://blog.csdn.net/qq1032355091/article/details/79558649
附件:
es中的字段属性
字符串:字符串有keyword 和 text两种类型,keyword是建立精确索引的字符串,text是建立分词索引的字符串。ip类型:ip
数字类型:short,integer,long,
命名规范
template名称:template_[kafka topic],如template_httpinfo(只允许小写字母)index名称:[kafka topic]-YYYY-MM-dd,如httpinfo-2017-12-12(只允许小写字母)
type名称:[kafka topic]
logstash配置文件中的测试采集group名:bmkp_[环境名]_logstash_[topic 名]_testxxx,
如bmkp_test_logstash_httpinfo_test001、bmkp_stress_logstash_httpinfo_test001、bmkp_prod_logstash_httpinfo_test001
logstash配置文件中的正式采集group名:bmkp_[环境名]_logstash_[topic 名],
如bmkp_test_logstash_httpinfo、bmkp_stress_logstash_httpinfo、bmkp_prod_logstash_httpinfo
template 示例:
{ "order": 10, "template":"httpinfo-*", "settings": { "index": { "number_of_shards": "1", "number_of_replicas": "0", "refresh_interval": "10s" } }, "mappings": { "httpinfo": { "_all": { "enabled": false, "norms": false }, "properties": { "@timestamp": { "type": "date" }, "@version": { "type": "keyword" }, "uuid": { "type":"keyword" }, "http_type": { "type":"keyword" }, "data": { "type": "text", "fields": { "keyword": { "type":"keyword", "ignore_above": 256 } } }, "processtime": { "type":"integer" }, "method": { "type":"keyword" }, "remoteip": { "type": "ip" }, "status": { "type": "short" }, "serverdate": { "type": "date" }, "type": { "type":"keyword" }, "uri": { "type": "text", "fields": { "keyword": { "type":"keyword", "ignore_above": 256 } } } }, "dynamic_templates": [ { "longs": { "match_mapping_type":"string", "match":"l_*", "unmatch":"*_text", "mapping": { "type":"long" } } }, { "shorts": { "match_mapping_type":"string", "match":"s_*", "unmatch":"*_text", "mapping": { "type":"short" } } }, { "ips": { "match_mapping_type":"string", "match":"*ip", "unmatch":"*_text", "mapping": { "type":"ip" } } }, { "dates": { "match_mapping_type":"string", "match":"*date", "unmatch":"*_text", "mapping": { "type":"date", "format":"yyyy-MM-dd'T'HH:mm:ss.SSS" } } }, { "dates": { "match_mapping_type":"string", "match":"*time", "unmatch":"*_text", "mapping": { "type":"date", "format":"yyyy-MM-dd'T'HH:mm:ss.SSS" } } }, { "floats": { "match_mapping_type":"string", "match":"f_*", "unmatch":"*_text", "mapping": { "type":"float" } } }, { "string_fields": { "mapping": { "norms": false, "type":"keyword" }, "match_mapping_type":"string", "match": "*" } } ] }, "_default_": { "_all": babb { "norms": false, "enabled": false }, "dynamic_templates": [ { "longs": { "match_mapping_type":"string", "match":"l_*", "unmatch":"*_text", "mapping": { "type":"long" } } }, { "shorts": { "match_mapping_type":"string", "match":"s_*", "unmatch":"*_text", "mapping": { "type":"short" } } }, { "ips": { "match_mapping_type":"string", "match":"*ip", "unmatch":"*_text", "mapping": { "type":"ip" } } }, { "dates": { "match_mapping_type":"string", "match":"*date", "unmatch":"*_text", "mapping": { "type":"date", "format":"yyyy-MM-dd'T'HH:mm:ss.SSS" } } }, { "dates": { "match_mapping_type":"string", "match":"*time", "unmatch":"*_text", "mapping": { "type":"date", "format":"yyyy-MM-dd'T'HH:mm:ss.SSS" } } }, { "floats": { "match_mapping_type":"string", "match":"f_*", "unmatch":"*_text", "mapping": { "type":"float" } } }, { "string_fields": { "mapping": { "norms": false, "type":"keyword" }, "match_mapping_type":"string", "match": "*" } } ] } }, "aliases": {} }
相关文章推荐
- Elasticsearch简单使用系列--详细介绍ES的核心概念
- logstash使用template提前设置好maping同步mysql数据到Elasticsearch5.5.2
- logstash 向elasticsearch写入数据,怎样指定多个数据template
- ros中的hector_mapping节点详细介绍
- logstash 向elasticsearch写入数据,如何指定多个数据template
- 搭建ELK(ElasticSearch+Logstash+Kibana)日志分析系统(七) elasticsearch.yml配置文件详细说明
- dedecms采集功能的详细介绍(十)
- VFW视频采集详细介绍
- Elasticsearch简单使用系列--详细介绍ES的核心概念
- c++通用模板类(template class)定义实现详细介绍
- ElasticsearchTemplate的详细使用,完成多条件查询、匹配度查询等
- logstash 采集 Java log4j的日志(全面介绍)
- PHP新手 详细介绍PHP代码规范
- ELKELK(ElasticSearch, Logstash, Kibana)平台介绍
- 新一篇: Unicode字符编码规范 实例详细介绍各种字符集编码转换问题
- C++可变参数模板(variadic template)详细介绍及代码举例
- c++通用模板类(template class)定义实现详细介绍
- Django框架之模版(template)详细介绍
- ElasticsearchTemplate的详细使用,完成多条件查询、匹配度查询等
- logstash向elasticsearch写入数据,如何指定多个数据template