ElasticSearch——自定义模板
output中配置
elasticsearch{
action => "index"
hosts => ["xxx"]
index => "http-log-logstash"
document_type => "logs"
template => "opt/http-logstash.json"
template_name => "http-log-logstash"
template_overwrite => true
}
自定义模板示例
{
"template" : "logstash-*", -------------> 匹配的索引名字
"order":1, -------------> 代表权重,如果有多个模板的时候,优先进行匹配,值越大,权重越高
"settings" : { "index.refresh_interval" : "60s" },
"mappings" : {
"_default_" : {
"_all" : { "enabled" : false },
"_source" : { "enabled" : false },
"dynamic": "strict",
"dynamic_templates" : [{
"message_field" : {
"match" : "message",
"match_mapping_type" : "string",
"mapping" : { "type" : "string", "index" : "not_analyzed" }
}
}, {
"string_fields" : {
"match" : "*",
"match_mapping_type" : "string",
"mapping" : { "type" : "string", "index" : "not_analyzed" }
}
}],
"properties" : {
"@timestamp" : { "type" : "date"},
"@version" : { "type" : "integer", "index" : "not_analyzed" },
"path" : { "type" : "string", "index" : "not_analyzed" },
"host" : { "type" : "string", "index" : "not_analyzed" },
"record_time":{"type":"date","format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"},
"method":{"type":"string","index" : "not_analyzed"},
"unionid":{"type":"string","index" : "not_analyzed"},
"user_name":{"type":"string","index" : "not_analyzed"},
"query":{"type":"string","index" : "not_analyzed"},
"ip":{ "type" : "ip"},
"webbrower":{"type":"string","index" : "not_analyzed"},
"os":{"type":"string","index" : "not_analyzed"},
"device":{"type":"string","index" : "not_analyzed"},
"ptype":{"type":"string","index" : "not_analyzed"},
"serarch_time":{"type":"date","format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"},
"have_ok":{"type":"string","index" : "not_analyzed"},
"legal":{"type":"string","index" : "not_analyzed"}
}
}
}
}
关键设置
- template for index-pattern
只有匹配 logstash-*
的索引才会应用这个模板。有时候我们会变更 Logstash 的默认索引名称,记住你也得通过 PUT 方法上传可以匹配你自定义索引名的模板。当然,我更建议的做法是,把你自定义的名字放在 "logstash-" 后面,变成 index => "logstash-custom-%{+yyyy.MM.dd}"
这样。
- refresh_interval for indexing
Elasticsearch 是一个近实时搜索引擎。它实际上是每 1 秒钟刷新一次数据。对于日志分析应用,我们用不着这么实时,所以 logstash 自带的模板修改成了 5 秒钟。你还可以根据需要继续放大这个刷新间隔以提高数据写入性能。
- multi-field with not_analyzed
Elasticsearch 会自动使用自己的默认分词器(空格,点,斜线等分割)来分析字段。分词器对于搜索和评分是非常重要的,但是大大降低了索引写入和聚合请求的性能。所以 logstash 模板定义了一种叫"多字段"(multi-field)类型的字段。这种类型会自动添加一个 ".raw" 结尾的字段,并给这个字段设置为不启用分词器。简单说,你想获取 url 字段的聚合结果的时候,不要直接用 "url" ,而是用 "url.raw" 作为字段名。
- geo_point
Elasticsearch 支持 geo_point 类型, geo distance 聚合等等。比如说,你可以请求某个 geo_point 点方圆 10 千米内数据点的总数。在 Kibana 的 bettermap 类型面板里,就会用到这个类型的数据。
- order
如果你有自己单独定制 template 的想法,很好。这时候有几种选择:
- 在 logstash/outputs/elasticsearch 配置中开启
manage_template => false
选项,然后一切自己动手; - 在 logstash/outputs/elasticsearch 配置中开启
template => "/path/to/your/tmpl.json"
选项,让 logstash 来发送你自己写的 template 文件; - 避免变更 logstash 里的配置,而是另外发送一个 template ,利用 elasticsearch 的 templates order 功能。
这个 order 功能,就是 elasticsearch 在创建一个索引的时候,如果发现这个索引同时匹配上了多个 template ,那么就会先应用 order 数值小的 template 设置,然后再应用一遍 order 数值高的作为覆盖,最终达到一个 merge 的效果。
比如,对上面这个模板已经很满意,只想修改一下 refresh_interval
,那么只需要新写一个:
{
"order" : ,
"template" : "logstash-*",
"settings" : {
"index.refresh_interval" : "20s"
}
}
然后运行以下命令即可:
curl -XPUT http://localhost:9200/_template/template_newid -d '@/path/to/your/tmpl.json'
set _source 设置为 false
假设你只关心度量结果,不是原始文件内容。比如,你可以把原始的数据存储在 MySQL ,hbase 等其他地方,从 es 中得到 id 后,去相应的数据库中进行取数据。
将节省磁盘空间并减少 IO。
“_source”:{“enabled”:false}
_all 设置为 false
假设你确切地知道你对哪个 field 做查询操作?
能实现性能提升,缩减存储。
“_all”:{“enabled”:false }
dynamic设置为 strict
假设你的数据是结构化数据。
字段设置严格,避免脏数据注入。
“dynamic”:”strict”
最新文章
- PHP实例开发(3)PHP中MVC学习之ThinkPHP
- java学习第19天(异常)
- PHP安装laravel(win+linux)
- TabHost的用法(转)
- 七参数计算正确性验证——Coord软件使用
- linux中关于php和nginx用户权限的一些东西
- PHP中用mysqli面向过程打开连接关闭mysql数据库
- LDAPserver的安装
- 全自动Web后门扫描(转)
- sublime text 3文件标题无法显示中文的解决办法
- C#中的CultureInfo类
- python学习——读取染色体长度(三、用循环或者函数求总长并获取最长染色体长度)
- Nginx+Keepalived(二)
- 【Java提高】---通过UUID、SHA-1、Base64组合加密
- Python 字典(Dictionary) 基本操作
- Android自定义相机拍照并使用CardView展示
- html5 javascript 事件练习1
- git命令无法自动补全(sles11.3)
- 函数式编程语言(functional language)
- Django后端项目---- rest framework(3)
热门文章
- 【CQOI2017】老C的方块
- 【Java 基础实例—Bank 项目1】
- body element height id small, but the backgroud color is full screen
- python+Appium自动化:app滑动操作swipe
- All-in-One Office,不容错过的办公插件
- SpringBoot + Maven + Hibernate ( 简单实现CRUD功能 )
- npm 镜像地址配置
- 【AndroidStudio-添加RecyclerView包】 AndroidStudio添加v7包中的RecyclerView
- PHP mysqli_kill() 函数
- luogu 1220 关路灯 区间dp