ElasticSearch實戰系列十: ElasticSearch冷熱分離架構

前言

本文主要介紹ElasticSearch冷熱分離架構以及實現。html

冷熱分離架構介紹

冷熱分離是目前ES很是火的一個架構,它充分的利用的集羣機器的優劣來實現資源的調度分配。ES集羣的索引寫入及查詢速度主要依賴於磁盤的IO速度,冷熱數據分離的關鍵點爲使用固態磁盤存儲數據。若所有使用固態,成本太高,且存放冷數據較爲浪費,於是使用普通機械磁盤與固態磁盤混搭,可作到資源充分利用,性能大幅提高的目標。所以咱們能夠將實時數據(5天內)存儲到熱節點中,歷史數據(5天前)的存儲到冷節點中,而且能夠利用ES自身的特性,根據時間將熱節點的數據遷移到冷節點中,這裏由於咱們是按天創建索引庫,所以數據遷移會更加的方便。java

架構圖:
在這裏插入圖片描述node

一個例子

使用冷熱分離的時候,咱們須要將索引庫創建在熱節點中,等到必定的時間時間在將該索引庫遷移冷節點中。所以這裏咱們須要更加熱節點的量來進行設置分片數。
好比,咱們擁有6個熱節點,9個冷節點,索引庫的主分片的數據量500G左右,那麼該索引庫創建18個分片而且都在在熱節點中,此時該索引庫的分片的分佈是,熱節點:18,冷節點0;等到該數據不是熱數據以後,將該索引庫的分片所有遷移到冷節點中,索引庫的分片的分佈是, 熱節點:0,冷節點18。git

單個索引庫熱冷節點分片分佈示例:github

時間 索引庫名稱 熱節點分片數量 冷節點分片數量
20190707 TEST_20190703 18 0
20190708 TEST_20190703 0 18

最終實現效果圖,這裏我用cerebro界面截圖來表示
cerebro示例圖:
寫入ES索引庫中,分片分佈在熱節點中
在這裏插入圖片描述
過了一段時間以後進行了遷移,分片數據遷移到了冷節點:
在這裏插入圖片描述bootstrap

更多ElasticSearch的相關介紹能夠查看個人這篇博文:http://www.javashuo.com/article/p-tehpwwqw-er.html服務器

ElasticSearch冷熱分離架構實現

ElasticSearch冷熱分離架構是一種思想,其實現原理是使用ElasticSearch的路由完成,在data節點設置對應的路由,而後在建立索引庫時指定分佈到那些服務器,過一段時間以後,根據業務要求在將這些索引庫的數據進行遷移到其餘data節點中。架構

ElasticSearch節點配置

這裏須要改變的節點爲data節點,其餘的節點配置無需更改。這裏我就用之前寫的ElasticSearch實戰系列一: ElasticSearch集羣+Kibana安裝教程裏面的配置進行更改。app

data節點的elasticsearch.yml原配置:elasticsearch

cluster.name: pancm
node.name: data1
path.data: /home/elk/datanode/data
path.logs: /home/elk/datanode/logs
network.host: 0.0.0.0
network.publish_host: 192.169.0.23
transport.tcp.port: 9300
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.169.0.23:9301","192.169.0.24:9301","192.169.0.25:9301"]
node.master: false
node.data: true
node.ingest: false 
index.number_of_shards: 5
index.number_of_replicas: 1
discovery.zen.minimum_master_nodes: 1
bootstrap.memory_lock: true
http.max_content_length: 1024mb

相比普通的data節點, 主要是增長了這兩個配置:

node.attr.rack: r1
node.attr.box_type: hot

熱節點配置示例:

cluster.name: pancm
node.name: data1
path.data: /home/elk/datanode/data
path.logs: /home/elk/datanode/logs
network.host: 0.0.0.0
network.publish_host: 192.169.0.23
transport.tcp.port: 9300
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.169.0.23:9301","192.169.0.24:9301","192.169.0.25:9301"]
node.master: false
node.data: true
node.ingest: false 
index.number_of_shards: 5
index.number_of_replicas: 1
discovery.zen.minimum_master_nodes: 1
bootstrap.memory_lock: true
http.max_content_length: 1024mb
node.attr.rack: r1
node.attr.box_type: hot

冷節點配置大致相同,就是後面的值進行更改

node.attr.rack: r9
node.attr.box_type: cool

冷節點配置示例:

cluster.name: pancm
node.name: data1
path.data: /home/elk/datanode/data
path.logs: /home/elk/datanode/logs
network.host: 0.0.0.0
network.publish_host: 192.169.0.23
transport.tcp.port: 9300
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.169.0.23:9301","192.169.0.24:9301","192.169.0.25:9301"]
node.master: false
node.data: true
node.ingest: false 
index.number_of_shards: 5
index.number_of_replicas: 1
discovery.zen.minimum_master_nodes: 1
bootstrap.memory_lock: true
http.max_content_length: 1024mb
node.attr.rack: r1
node.attr.box_type: hot

ElasticSearch索引庫設置

在建立索引庫的時候須要指定默認索引庫的分片歸屬,若是沒有指定,就會根據ElasticSearch默認進行均勻分佈。這裏咱們將索引庫默認建立到hot節點中,知足業務條件以後在使用命令或代碼將該索引庫設置到冷節點中。

索引示例:

PUT TEST_20190717
{
  "index":"TEST_20190717",
  "settings": {
    "number_of_shards" :18,
    "number_of_replicas" : 1,
    "refresh_interval" : "10s",
    "index.routing.allocation.require.box_type":"hot"
  },
"mappings": {
    "mt_task_hh": {
      "properties": {
        "accttype": {
          "type": "byte"
        },
....

}
}

索引庫冷節點設置

根據業務要求,咱們能夠對索引庫的數據進行遷移,使用dsl語句在kibana上執行或者使用java代碼實現均可以。

dsl語句:

PUT TEST_20190717/_settings
{
  
    "index.routing.allocation.require.box_type":"cool"
  
}

java代碼實現:

public static void setCool(String index) throws IOException {
        RestClient restClient = null;
        try {
            Objects.requireNonNull(index, "index is not null");
            restClient = client.getLowLevelClient();
            String source = "{\"index.routing.allocation.require.box_type\": \"%s\"}";
            source = String.format(source, "cool");
            HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
            restClient.performRequest("PUT", "/" + index + "/_settings", Collections.<String, String>emptyMap(), entity);
        } catch (IOException e) {
            throw e;
        } finally {
            if (restClient != null) {
                restClient.close();
            }
        }
    }

完整代碼地址: https://github.com/xuwujing/java-study/tree/master/src/main/java/com/pancm/elasticsearch

其它

其實這篇文章本應來講在2019年就完成了初稿,可是由於其餘的事情一直耽擱,好在查看草稿是發現了,因而便補了。由於時隔過久,細節上相比以前的文章有必定的差距。不過好在是寫出來了,之後的話寫文章的話仍是儘早,否則後面就忘了。目前ElasticSearch實戰系列已經寫了10篇了,雖然中間的間隔有點久,後面我會慢慢的更新這個系列,儘可能把本身所學所感悟的寫出來,若有寫的很差,但願可以指出討論。

ElasticSearch實戰系列:

音樂推薦

原創不易,若是感受不錯,但願給個推薦!您的支持是我寫做的最大動力!
版權聲明:
做者:虛無境
博客園出處:http://www.cnblogs.com/xuwujing
CSDN出處:http://blog.csdn.net/qazwsxpcm
掘金出處:https://juejin.im/user/5ae45d5bf265da0b8a6761e4    
我的博客出處:http://www.panchengming.com

相關文章
相關標籤/搜索