iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 14
0
Elastic Stack on Cloud

親愛的,我把ElasticSearch上雲了系列 第 14

Day 14 Index Template And DataStream

Day 14 Index Template And DataStream


前言

昨天講解了mapping的部分,也透過實際的API request去理解如何建構以及更新mapping,另外就是透過config讓elasticsearch dynamic mapping能夠運作。今天我們來講解一個與index相關,在使用data stream前必須先建構的東西-Index Template。

Index Template

首先不免俗的,我們一樣上elastic search官方的英文說明

An index template is a way to tell Elasticsearch how to configure an index when it is created. For data streams, the index template configures the stream’s backing indices as they are created. Templates are configured prior to index creation and then when an index is created either manually or through indexing a document, the template settings are used as a basis for creating the index.

index template其實就是一個建立index的模板,這個模板可以告訴elasticsearch如何設置index。在Data Stream中,我們為了讓Data能夠從頭到尾串流,因此會透過index template來告訴elasticsearch,要建構甚麼樣的index去儲存這些Data(Documents)

其中Index Templates包含兩個要素,一是Index Templates;二是Component Templates。

  • Component Templates是用來建構Index Template,主要是設置Index中的mappings、settings以及aliases.(忘記這些名詞的可以參考前面文章)
  • 一個Index Template可以對照多個Compnent Templates

而如果一個data stream 或是 index,對應到大於一個Index Templates,會選擇優選度最高的做為適用。

Index Template實際設定及適用

我們參考Elasticsearch官網中的範例,首先建構兩個Component Templates。

建構的方式也很簡單,只要針對_component_template,以及欲取的名稱,進行PUT的request,接著在Body中寫入關於mapping、aliases以及settings的設定。

可以看到以下兩個案例,我們可以將mapping的field拆成兩個Component Template,也就是說其實Component Template可以想像成是元件化的設定,針對某些特定狀況,例如針對IP相關的mapping以及aliases。

拆成多個的好處在於,當建構新的Index Tempalte,可以透過不同組合去建構,就不需要每次都新增一個新的Component Template

PUT _component_template/component_template1
{
  "template": {
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        }
      }
    }
  }
}

PUT _component_template/other_component_template
{
  "template": {
    "mappings": {
      "properties": {
        "ip_address": {
          "type": "ip"
        }
      }
    }
  }
}

建構完可以透過GET去檢視

GET _component_template/

接著我們可以建構一個Index Template,建構方式也十分簡單如下:

PUT _index_template/template_1
{
  "index_patterns": ["te*", "bar*"],
  "template": {
    "settings": {
      "number_of_shards": 1
    },
    "mappings": {
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z yyyy"
        }
      }
    },
    "aliases": {
      "mydata": { }
    }
  },
  "priority": 200,
  "composed_of": ["component_template1", "other_component_template"],
  "version": 3,
  "_meta": {
    "description": "my custom"
  }
}

可以看到其中幾個比較重要的欄位,像是index_patterns,這部分可以透過正規表示去找到之後要mapping進的index名稱。接下來大部分是index的設定,另外就是priority部分,也就是剛剛提到,如果名稱matching到大於一個index template,就會挑選priority高的。最後是composed_of,這部分就是選擇component template,可以看到我們挑剛剛建構的那兩個,也因此這個index template,基本上是包含@timestamp,以及ip_address兩個mapping field的。

GET _index_template

response:
...
"index_templates": [
        {
            "name": "template_1",
            "index_template": {
                "index_patterns": [
                    "te*",
                    "bar*"
                ],
                "template": {
                    "settings": {
                        "index": {
                            "number_of_shards": "1"
                        }
                    },
                    "mappings": {
                        "properties": {
                            "created_at": {
                                "format": "EEE MMM dd HH:mm:ss Z yyyy",
                                "type": "date"
                            },
                            "host_name": {
                                "type": "keyword"
                            }
                        }
                    },
                    "aliases": {
                        "mydata": {}
                    }
                },
                "composed_of": [
                    "component_template1",
                    "other_component_template"
                ],
                "priority": 200,
                "version": 3,
                "_meta": {
                    "description": "my custom"
                }
            }
        },
        ...

可以看到送完request以後就建構好一個index template。

最後我們來測試一下手動新增符合pattern的index,看看會不會套到template。

首先pattern為te以及bar,因此我們建構一個叫做bar的index

PUT bar

接著來看一下他的設定

GET bar
{
    "bar": {
        "aliases": {
            "mydata": {}
        },
        "mappings": {
            "properties": {
                "@timestamp": {
                    "type": "date"
                },
                "created_at": {
                    "type": "date",
                    "format": "EEE MMM dd HH:mm:ss Z yyyy"
                },
                "host_name": {
                    "type": "keyword"
                },
                "ip_address": {
                    "type": "ip"
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1601391561039",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "o34LcfLKTsq200cUKXk04A",
                "version": {
                    "created": "7090199"
                },
                "provided_name": "bar"
            }
        }
    }
}

可以發現已經套用到剛剛template1的index template設定!


上一篇
Day 13 DataField And Mapping
下一篇
Day 15 Create DataStream
系列文
親愛的,我把ElasticSearch上雲了30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言