一、概述
Elasticsearch 提供了一系列集群管理 API,可以用来管理集群状态、节点、分片等信息。本文将介绍 Elasticsearch 集群管理 API 的使用,包括集群状态 API、节点信息 API、索引管理 API、分片管理 API 等。
二、集群状态 API
获取集群健康状态
通过 _cat/health API 可以获取集群的健康状态,包括集群名称、状态、节点数、分片数等信息。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 获取集群健康状态
health_res = es.cat.health()
print(health_res)
输出结果如下:
yellow open my_index 1 1 1 0 4.7kb 2.3kb
yellow open my_index_2 1 1 1 0 4.7kb 2.3kb
yellow open my_index_3 1 1 1 0 4.7kb 2.3kb
获取集群节点信息
通过 _cat/nodes API 可以获取集群的节点信息,包括节点 ID、名称、IP 地址、角色等信息。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 获取集群节点信息
nodes_res = es.cat.nodes()
print(nodes_res)
输出结果如下:
127.0.0.1 16 57 8 0.24 0.15 0.13 cdhilmrstw - node-1
127.0.0.1 16 57 8 0.24 0.15 0.13 cdhilmrstw * node-2
三、索引管理 API
创建索引
通过 indices.create() API 可以创建一个新的索引,需要指定索引名称和索引设置等信息。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 创建索引
mapping = {
"properties": {
"name": {"type": "keyword"},
"age": {"type": "integer"}
}
}
es.indices.create(index="my_index", body={"mappings": mapping})
删除索引
通过 indices.delete() API 可以删除一个索引。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 删除索引
es.indices.delete(index="my_index")
查看索引列表
通过 _cat/indices API 可以查看当前集群中的所有索引。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 查看索引列表
indices_res = es.cat.indices()
print(indices_res)
输出结果如下:
yellow open my_index 1 1 1 0 4.7kb 2.3
四、分片管理 API
查看分片状态
通过 _cat/shards API 可以查看当前集群中所有索引的分片状态。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 查看分片状态
shards_res = es.cat.shards()
print(shards_res)
输出结果如下:
my_index 0 p STARTED 1 1.8kb 127.0.0.1 node-1
my_index 0 r UNASSIGNED
my_index_2 0 p STARTED 1 1.8kb 127.0.0.1 node-1
my_index_2 0 r UNASSIGNED
my_index_3 0 p STARTED 1 1.8kb 127.0.0.1 node-1
my_index_3 0 r UNASSIGNED
手动分配分片
通过 cluster.reroute() API 可以手动分配分片,需要指定目标节点和分片信息等。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 手动分配分片
body = {
"commands": [
{
"allocate": {
"index": "my_index",
"shard": 0,
"node": "node-1",
"allow_primary": True
}
}
]
}
es.cluster.reroute(body=body)
暂停和恢复分片分配
通过 cluster.routing.allocation.enable 配置项可以控制是否允许分片分配。将其设置为 none 可以暂停分配,设置为 all 可以恢复分配。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 暂停分片分配
es.cluster.put_settings(body={"transient": {"cluster.routing.allocation.enable": "none"}})
# 恢复分片分配
es.cluster.put_settings(body={"transient": {"cluster.routing.allocation.enable": "all"}})
五、节点管理 API
添加节点
通过 nodes.hot_threads() API 可以获取当前节点的热点线程信息,包括线程 ID、名称、状态、运行时间等。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 获取节点热点线程信息
hot_threads_res = es.nodes.hot_threads(node_id="_local")
print(hot_threads_res)
删除节点
通过 nodes.shutdown() API 可以关闭一个节点。
from elasticsearch import Elasticsearch
# 创建连接对象
es = Elasticsearch()
# 关闭节点
es.nodes.shutdown(node_id="_local")
小结
本文介绍了 Elasticsearch 集群管理 API 的使用,包括集群状态 API、节点信息 API、索引管理 API、分片管理 API 等。这些 API 可以用来管理集群状态、节点、分片等信息,对于保证集群高可用、提高搜索性能等方面有很大的作用。在使用时需要注意安全性,防止恶意操作对集群造成损害