Logstash 修改 output elasticsearch 的 Data type

栏目: 后端 · 发布时间: 7年前

内容简介:Logstash 預設 output elasticsearch 會使用 dynamic template 去建立 index value,如果沒有特別設定,所有的 data 都會從 String 轉成 Text 的類型。若要對該 data type 執行特殊的資料處理,或者是圖表呈現的話純 String 格式就無法呈現 (或畫圖),如 IP Range query 則需要轉換成 IP type 才能使用 10.0.0.0/8 這樣的 query,否則只能使用一般 NOT client : 10.* 等字

Logstash 預設 output elasticsearch 會使用 dynamic template 去建立 index value,如果沒有特別設定,所有的 data 都會從 String 轉成 Text 的類型。

若要對該 data type 執行特殊的資料處理,或者是圖表呈現的話純 String 格式就無法呈現 (或畫圖),如 IP Range query 則需要轉換成 IP type 才能使用 10.0.0.0/8 這樣的 query,否則只能使用一般 NOT client : 10.* 等字串型別的查詢。

如果你已經有 Kibana,則可以善用 Dev Tool 來查詢 template index,執行 Query 就可以看到

GET /_template/logstash-index-template

一般正常狀況下,應該像這樣 (6.4.x):

{
  "logstash-index-template": {
    "order": 0,
    "index_patterns": [
      ".logstash"
    ],
    "settings": {
      "index": {
        "number_of_shards": "1",
        "auto_expand_replicas": "0-1",
        "codec": "best_compression"
      }
    },
    "mappings": {
      "doc": {
        "_meta": {
          "logstash-version": "6.4.0"
        },
        "dynamic": "strict",
        "properties": {
          "description": {
            "type": "text"
          },
          "last_modified": {
            "type": "date"
          },
          "pipeline_metadata": {
            "properties": {
              "version": {
                "type": "short"
              },
              "type": {
                "type": "keyword"
              }
            }
          },
          "pipeline": {
            "type": "text"
          },
          "pipeline_settings": {
            "dynamic": false,
            "type": "object"
          },
          "username": {
            "type": "keyword"
          },
          "metadata": {
            "type": "object",
            "dynamic": false
          }
        }
      }
    },
    "aliases": {}
  }
}

從 logstash 餵進來的 data type 沒有被 template 定義到的都會是 Text 格式。

要定義 template 可以從 logstash 或者 elasticsearch 下手,為了維護方便所以選擇用 logstash 這邊統一處理掉

$ sudo mkdir /etc/logstash/template.d/elasticsearch-template-with-clientip.json
$ tee /etc/logstash/template.d/elasticsearch-template-with-clientip.json <<EOF
{
  "index_patterns" : "logstash-*",
  "version" : 1,
  "settings" : {
    "index.refresh_interval" : "5s"
  },
  "mappings" : {
    "doc" : {
      "properties" : {
        "clientip": {
          "type": "ip",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}
EOF

簡單的來說要先 follow template 格式,從 properties 定義一個 clientip field 的 type 為 ip。

然後 output elasticsearch 的地方指定 template 檔案,template_overwrite 要開啟才會覆蓋預設的 template。

output {
  elasticsearch {
    hosts => "elasticsearch.sys.104dc.com:9200"
    template => "/etc/logstash/template.d/elasticsearch-template-with-clientip.json"
    template_overwrite => true
  }
}

Logstash 重啟後看 log 可以看到 template 被 install 到 elasticsearch,位置是 _template/logstash

[2018-10-02T13:09:01,715][INFO ][logstash.outputs.elasticsearch] Attempting to install template {:manage_template=>{“index_patterns”=>”logstash-*”, “version”=>1, “settings”=>{“index.refresh_interval”=>”5s”}, “mappings”=>{“doc”=>{“properties”=>{“clientip”=>{“type”=>”ip”, “fields”=>{“keyword”=>{“type”=>”keyword”, “ignore_above”=>256}}}}}}}}  [2018-10-02T13:09:01,744][INFO ][logstash.outputs.elasticsearch] Installing elasticsearch template to _template/logstash

再用 Kibana 的 Dev Tool 驗證一下新建立的 index,就可以看到自定義的 template

GET /_template/logstash

** 既有的 index 不會生效,必須 rebuild 或 new index。


以上所述就是小编给大家介绍的《Logstash 修改 output elasticsearch 的 Data type》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

微信小程序

微信小程序

牛建兵 / 清华大学出版社 / 2017-9-1 / 49.00

微信小程序从满城风雨到掀起创业热潮,再到一直不断地迭代更新,其每一次更新都会增加新能力,让使用变得越来越方便,同时也增加一批进军小程序的队伍。微信小程序作为现在互联网中的热点,它给创业者、投资者、程序员带来了福音。对创业者来说,小程序能大大降低他们的创业成本,同时也降低了投资者的投资风险。 本书注重实战,通过具体的案例分析,讲解微信小程序的市场状况、人员分工、小程序需求设计、小程序体验设计、......一起来看看 《微信小程序》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具