天天看点

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}

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'

  "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'

继续阅读