【发布时间】:2019-11-23 02:13:36
【问题描述】:
我有一个项目要分析和可视化访问日志数据。我使用 Logstash 将数据发送到 Elasticsearch,然后使用 Kibana 可视化一些内容。
一切都很好,直到我发现我需要路径层次分析器来显示我想要的东西。我现在有一个自定义模板 (JSON) 并更改了我的 Logstash 配置的 out 部分。但是当我索引数据时,我的模板没有被应用。
(Elasticseach 和 Logstash 的 5.2 版本,无法更新,因为这是我工作的地方正在使用的版本)。
我的 JSON 文件有效。就输入和过滤器而言,我的 Logstash 配置也很好。我想我在输出中犯了一个错误。
我已经尝试将 manage_template 设置为 false。为了它,我还尝试了 template_overwrite => "false"。
我尝试先创建索引(Kibana 开发工具),然后再填充它。我创建了索引模板,然后创建了索引。这样我的模板就被应用了,当我创建索引模式时,一切似乎都是正确的。然后我索引了我的一个日志文件。我最终遇到了 Courier Fetch Error。 http://localhost:9200/_all/_mapping?pretty=1 向我展示了在索引我的数据时使用默认模板而不是我的自定义模板。与添加自定义模板之前没有什么不同。
我在网上搜索并阅读了我可以在 stackoverflow 和弹性论坛上找到的关于未应用自定义模板的所有内容。我尝试了那里提供的所有解决方案,这就是为什么我最终选择在本地保存的自定义模板并在我的 logstash 输出中提供路径。但我现在完全没有主意了。
这是我的 logstash 配置的输出:
output {
elasticsearch {
hosts => ["localhost:9200"]
template => "/etc/logstash/conf.d/template.json"
index => "beam-%{+YYYY.MM.dd}"
manage_template => "true"
template_overwrite => "true"
document_type => "beamlogs"
}
stdout {
codec => rubydebug
}
}
这是我的自定义模板:
{
"template": "beam_custom",
"index_patterns": "beam-*",
"order" : 5,
"settings": {
"number_of_shards": 1,
"analysis": {
"analyzer": {
"custom_path_tree": {
"tokenizer": "custom_hierarchy"
},
"custom_path_tree_reversed": {
"tokenizer": "custom_hierarchy_reversed"
}
},
"tokenizer": {
"custom_hierarchy": {
"type": "path_hierarchy",
"delimiter": "/"
},
"custom_hierarchy_reversed": {
"type": "path_hierarchy",
"delimiter": "/",
"reverse": "true"
}
}
}
},
"mappings": {
"beamlogs": {
"properties": {
"object": {
"type": "text",
"fields": {
"tree": {
"type": "text",
"analyzer": "custom_path_tree"
},
"tree_reversed": {
"type": "text",
"analyzer": "custom_path_tree_reversed"
}
}
},
"referral": {
"type": "text",
"fields": {
"tree": {
"type": "text",
"analyzer": "custom_path_tree"
},
"tree_reversed": {
"type": "text",
"analyzer": "custom_path_tree_reversed"
}
}
},
"@timestamp" : {
"type" : "date"
},
"action" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"datetime" : {
"type" : "date",
"format": "time_no_millis",
"fields" : {
"keyword" : {
"type": "keyword"
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"info" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"message" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"page" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"path" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"result" : {
"type" : "long"
},
"s_direct" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"s_limit" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"s_mobile" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"s_terms" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
},
"size" : {
"type" : "long"
},
"sort" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
}
}
}
}
}
索引我的数据后,这是我使用http://localhost:9200/_all/_mapping?pretty=1 得到的一部分
"datetime" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"object" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
datetime 不应具有文本类型。但更糟糕的是,甚至没有创建像 objet.tree 这样的字段。
我真的不关心日期时间的错误映射,但我需要让路径层次结构分析器工作。我只是不知道该怎么办了。
所以。我刚刚尝试的是在 Kibana 中创建索引模板。
PUT _template/beam_custom
/followed by what is in my template.json
然后我检查了是否创建了模板。
GET _template/beam_custom
输出是这样的:
{
"beam_custom": {
"order": 100,
"template": "beam_custom",
"settings": {
"index": {
"analysis": {
"analyzer": {
"custom_path_tree_reversed": {
"tokenizer": "custom_hierarchy_reversed"
},
"custom_path_tree": {
"tokenizer": "custom_hierarchy"
}
},
"tokenizer": {
"custom_hierarchy": {
"type": "path_hierarchy",
"delimiter": "/"
},
...
所以我想创建模板是可行的。
然后我创建了一个索引
PUT beam-2019-07-15
但是当我检查索引时,我得到了这个:
{
"beam-2019.07.15": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1563044670605",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "rGzplctSQDmrI_NSlt47hQ",
"version": {
"created": "5061699"
},
"provided_name": "beam-2019.07.15"
}
}
}
}
不应该识别索引模式吗?我认为这是问题的核心。我以为我的模板会被使用,而输出应该是这样的:
{
"beam-2019.07.15": {
"aliases": {},
"mappings": {
"logs": {
"properties": {
"@timestamp": {
"type": "date"
},
"action": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},...
为什么它不能识别模式?
【问题讨论】: