Elasticsearch集羣管理

一、Cluster Health

可以通過CURL命令發送REST命令,查詢集羣的健康狀態:

curl '192.168.56.101:9200/_cat/health?v'

參數說明

192.168.56.101是主機的IP地址,9200是監聽的端口號,Elasticsearch默認監聽的端口號就是9200

得到相應的結果,詳細相信請查看附件(cluster健康檢查)


可以看到集羣的名字是默認的"elasticsearch",集羣的狀態時"yellow"。各種顏色的說明如下:

1 綠色,最健康的狀態,代表所有的分片包括備份都可用

2 黃色,基本的分片可用,但是備份不可用(也可能是沒有備份)

3 紅色,部分的分片可用,表明分片有一部分損壞。此時執行查詢部分數據仍然可以查到,遇到這種情況,還是趕快解決比較好。

可以通過下面的命令,查詢節點列表 

curl '192.168.56.101:9200/_cat/nodes?v'

host       ip         heap.percent ram.percent load node.role master name   192.168.56.101 192.168.56.101 7        31      0.12 d      *    my_node_name 


二、List All Indices

在Elasticsearch中索引有兩個意思:

1 動詞的索引,表示把數據存儲到Elasticsearch中,提供搜索的過程;這期間可能正在執行一個創建搜索的過程。

2 名字的索引,它是Elasticsearch中的一個存儲類型,與數據庫類似,內部包含type字段,type中包含各種文檔。


查看所有的索引,詳情請查看附件(索引信息)

curl '192.168.56.101:9200/_cat/indices?v'

如果集羣中沒有任何的數據,結果中只會包含列的信息。


三、Create an Index

下面創建索引以及查詢的例子

curl -XPUT '192.168.56.101:9200/customer?pretty'{  "acknowledged" : true}

curl '192.168.56.101:9200/_cat/indices?v'

health index    pri rep docs.count docs.deleted store.size pri.store.size yellow customer   5   1          0            0       495b           495b

上面的結果中,customer索引的狀態是yellow,這是因爲此時雖然有5個主分片和一個備份。但是由於只是單個節點,

我們的分片還在運行中,無法動態的修改。因此當有其他的節點加入到集羣中,備份的節點會被拷貝到另一個節點中

,狀態就會變成green


四、Index and query a Document

索引裏面還有類型的概念,在索引文檔之前要先設置類型type。

執行的命令如下:

curl -XPUT '192.168.56.101:9200/customer/external/1?pretty' -d '

> {

> "name": "John Doe"

> }'

執行成功後會得到如下信息

{

  "_index" : "customer",

  "_type" : "external",

  "_id" : "1",

  "_version" : 1,

  "_shards" : {

    "total" : 2,

    "successful" : 1,

    "failed" : 0

  },

  "created" : true

}

注意2.0版本的ES在同一索引下,不同的類型,相同的字段名字,是不允許字段類型不一致的。

執行下面的命令查詢,返回信息也如下:

[root@sigmam1 config]# curl -XGET '192.168.56.101:9200/customer/external/1?pretty'

{

  "_index" : "customer",

  "_type" : "external",

  "_id" : "1",

  "_version" : 1,

  "found" : true,

  "_source" : {

    "name" : "John Doe"

  }

}

這裏會新增兩個字段 

found 描述了請求信息

_source爲之前索引時的數據


五、刪除索引

curl -XDELETE '192.168.56.101:9200/customer?pretty'

返回結果

{

  "acknowledged" : true

}


六、Summary

--創建索引

curl -XPUT '192.168.56.101:9200/customer'

--插入數據

curl -XPUT '192.168.56.101:9200/customer/external/1' -d ''

{

"name":"John Doe"

}

'

--查詢數據

curl '192.168.56.101:9200/customer/external/1'

--刪除索引

curl -XDELETE '192.168.56.101:9200/customer'


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章