首页 技术 正文
技术 2022年11月9日
0 收藏 909 点赞 3,301 浏览 2503 个字

公司ELK系统目前的设置是每月自动将日志信息记录至新的索引中,将日志数据按月分索引保存,在扩展的ELK架构中,利Logstash对接rabbitmq,获取日志消息,自动持久化至Elasticsearch。而Elasticsearch存在一个关键问题就是索引的设置及字段的属性指定,最常见的问题就是,某个字段我们并不希望ES对其进行分词,但如果使用自动模板创建索引,那么默认是所有string类型的字段都会被分词的,因此必须要显式指定字段的not_analyzed属性,其它的比如控制索引的备份数,分片数等,也可以通过模板的套用来实现,并且模板可以通过通配符进行模糊匹配,即可对满足某一通配符的所有新建索引均套用统一模板,不需要为每个索引都建立模板。但也有一点局限性需要注意:模板在设置生效后,仅对ES集群中新建立的索引生效,而对已存在的索引及时索引名满足模板的匹配规则,也不会生效,因此如果需要改变现有索引的mapping信息,仍需要在正确的mapping基础上建立新的索引,并将数据从原索引拷贝至新索引,变更新索引别名为原索引这种方式来实现(改方法适用当前ES版本(1.7+~2.4+)),也许未来会有索引的直接迁移方案。


方案选择:

方案一:可对logstash配置output参数:

如下所示:

这种方案在logstash中指定模板文件,由logstash将template写入ES集群;

方案二:直接将template写入ES集群

通过ES提供的API,将JSON格式的template写入目标ES集群的_template路径,对新生成的所有符合过滤规则的索引直接套用该模板。


这里我直接选择了方案二,因为不想修改logstash的docker镜像中的配置文件…

模板的结构大致分四块吧:

第一部分:通用设置,主要是模板匹配索引的过滤规则,影响该模板对哪些索引生效;

第二部分:settings:配置索引的公共参数,比如索引的replicas,以及分片数shards等参数;

第三部分:mappings:最重要的一部分,在这部分中配置每个type下的每个field的相关属性,比如field类型(string,long,date等等),是否分词,是否在内存中缓存等等属性都在这部分配置;

第四部分:aliases:索引别名,索引别名可用在索引数据迁移等用途上。

典型的一个template如下所示:

{        "template": "ld.log-*",        "order":0,        "settings": {            "index.number_of_replicas": "1",            "index.number_of_shards": "5"        },        "mappings": {            "logs": {                "properties": {                    "@timestamp": {                        "type": "date",                        "format": "strict_date_optional_time||epoch_millis"                    },                    "@version": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "Exp": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "Guid": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "LogLevel": {                        "type": "long"                    },                    "LogTime": {                        "type": "date",                        "format": "strict_date_optional_time||epoch_millis"                    },                    "LoggerName": {                        "index": "not_analyzed",                        "type": "string"                    },                    "Message": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "ProcessId": {                        "type": "long"                    },                    "StackTrace": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "ThreadId": {                        "type": "long"                    },                    "exp": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "logLevel": {                        "type": "long"                    },                    "logTime": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "loggerName": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "message": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "processId": {                        "type": "long"                    },                    "tags": {                        "doc_values": true,                        "index": "not_analyzed",                        "type": "string"                    },                    "threadId": {                        "type": "long"                    }                }            }        },        "aliases": {        }}

在这个JSON中可以清楚地看到四个部分,并且对string类型的fields设置了不进行默认分词信息。

将这个JSON内容作为PUT请求的BODY(如果是修改template则发送POST请求)发送至目标ES集群(需要指定template的名字,如下例的log_template):

成功后将返回如下信息:

{   "acknowledged": true}

现在可以通过对集群_template目录直接发送请求(也可以加上template名称)来查看集群当前的模板信息:


按照如上步骤,template正常添加至ES集群之后,在新增索引之前,会自动与模板进行匹配,如果满足过滤条件,即可套用目标模板来新建索引。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289