ES常用API

目录

查看ES集群的健康状况
查看ES的设置
动态设置参数
查看ES在线的节点
查看ES的主节点
查看所有索引
查看具体某个索引
查看yellow的索引
查看red的索引
查看yellow索引的恢复情况
关闭所有yellow索引
查询索引的分片情况
查询指定索引的分片情况
查看不能分配的分片
查看所有分片的恢复情况
查看某个具体索引的分片恢复情况
查看segments内存占用情况
查看线程池
查看ES集群线程池状态
查看segment数量
查看节点情况
打开指定索引
关闭指定索引
设置total_shards_per_node
设置集群recovery数量
将副本数设置为0
将索引的shard里的多个段合并成1个段,段越少,占用的资源越少,有利于提高查询速度
使用_cat/nodes?v查看节点信息,区分哪个是当前的master,然后使用_cat/master对比. 带*的是master
以组名或姓名为前缀,创建 "前缀-1"索引,"前缀-2"索引,不加任何设置。
以组名或姓名为前缀,创建 "前缀-3"索引,3个分片,每个分片0个副本
向"前缀-2"的索引中添加一个doc,内容为{“name”:“任意”,“something”:“任意”}
查看指定索引的mapping和setting
删除"前缀-1"索引, 关闭"前缀-2"索引, 打开"前缀-2"索引
"前缀-3"动态修改副本数量为1
修改"前缀-3"索引的total_shards_per_node参数为1,查看索引setting,以及状态


  • 查看ES集群的健康状况
curl localhost:9200/_cluster/health?pretty
{
  "cluster_name" : "es",
  "status" : "yellow",                                            当前集群状态
  "timed_out" : false,
  "number_of_nodes" : 3,                                          当前集群在线的节点个数为3 
  "number_of_data_nodes" : 3,                                     在线的数据节点数
  "active_primary_shards" : 16055,                                活跃的主分片数量
  "active_shards" : 32107,                                        活跃的分片数量,包括主分片和副本。
  "relocating_shards" : 0,                                        正在移动的分片数量
  "initializing_shards" : 0,                                      正初初始化的分片个数为:0 
  "unassigned_shards" : 3,                                        未分配的分片个数为:3  
  "delayed_unassigned_shards" : 0,                                由于节点离线导致延迟分配的分片数量
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 99.99065711616318           所有活跃分片/打开的所有索引的分片总数
}
  • status字段
    • green状态:集群正常主副分片均被分配
    • yellow状态:集群主分片已经分配,但副本分片存在无法分配的情况,这时对外提供的服务是正常可用的,数据也都是完整的
    • red状态:存在不能分配的主分片

  • 查看ES的设置
curl localhost:9200/_cluster/settings?pretty
{
  "persistent" : {                   // 永久设置,重启仍然有效
    "action" : {
      "auto_create_index" : ".security,.monitoring-*,.watch*,.triggered_watches,.quota,noah*,basp*",
      "destructive_requires_name" : "false"
    }
  },
  "transient" : { }                  // 临时设置,重启失效 
}
  • 动态设置参数
临时生效:transient修改方法为:curl -XPUT 'http://localhost:9200/_cluster/settings?pretty' -d '{"transient":{"dynamic.parma":"value"}}'
永久生效:persistent修改方法为:curl -XPUT 'http://localhost:9200/_cluster/settings?pretty' -d '{"persistent":{"dynamic.parma":"value"}}'

  • 查看ES在线的节点
curl localhost:9200/_cat/nodes?v
ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
10.xx.xxx.xxx           97          99  22   18.64   11.75     9.88 mdi       *      es.wjl.cn.0
10.xx.xxx.xxx           94          98  37    7.25    7.59     7.74 mdi       -      es.ejl.cn.0
10.xx.xxx.xxx           93          88  14    8.13    9.16     9.17 mdi       -      es.wjl.cn.0

存在节点缺失的情况可用该命令查看缺失节点为哪些


  • 查看ES的主节点
curl localhost:9200/_cat/master
curl localhost:9200/_cat/master?v  // 加上 ?v 将显示字段名.
es.www.cn.0 temp1002.qqq.cn 10.xx.xxx.xxx es.wjl.cn.0

  • 查看所有索引
curl localhost:9200/_cat/indices
curl localhost:9200/_cat/indices?v  // 加上 ?v 将显示字段名.
yellow open index06  QCVD7Cudbc3cqNlG0g 5 1 1 0  5.1kb  5.1kb
green  open index200 UXEe4IEOos8BMccZXw 3 0 4 0   16kb   16kb
yellow open index01  ewNasSrnSjiqnQPWAA 3 1 1 0  5.4kb  5.4kb
  • 查看具体某个索引
curl localhost:9200/_cat/indices/{index}
curl localhost:9200/_cat/indices/{index}?v   // 加上 ?v 将显示字段名.
  • 取索引名最好加上前缀, 因为索引可以模糊匹配
curl -sXGET localhost:9200/_cat/indices/wjl*?v   // 查询前缀是wjl的所有索引
  • 查看yellow的索引
curl -sXGET "http://localhost:9200/_cat/indices?v" | grep -v close | grep yellow
  • 查看red的索引
curl -sXGET "http://localhost:9200/_cat/indices?v" | grep -v close | grep red
  • 查看yellow索引的恢复情况
curl -sXGET "http://localhost:9200/_cat/recovery?v" | grep -v done
  • 关闭所有yellow索引
curl -sXGET "http://localhost:9200/_cat/indices" | grep -v close | grep "yellow " | awk '{print $3}' | while read line; do curl -XPOST "http://localhost:9200/$line/_close" ;done

  • 查询索引的分片情况
curl localhost:9200/_cat/shards?v 
index_name    2 p STARTED         0     159b 10.95.134.147 es.wjl.cn.0
index_name    2 r STARTED         0     159b 10.95.134.149 es.wjl.cn.0
index_name    3 r STARTED         1   16.4kb 10.95.134.145 es.wjl.cn.0
  • 查询指定索引的分片情况
curl localhost:9200/_cat/shards/{index}?v 
  • 查看不能分配的分片
curl -sXGET "http://localhost:9200/_cat/shards?v" | grep UNASSIGNED

  • 查看所有分片的恢复情况
curl localhost:9200/_cat/recovery
index06 0 68ms existing_store done n/a n/a 172.16.144.80 node-1 n/a n/a 0 0 100.0% 1 0 0 100.0% 261  0 0 100.0%
index06 1 59ms existing_store done n/a n/a 172.16.144.80 node-1 n/a n/a 0 0 100.0% 1 0 0 100.0% 261  0 0 100.0%
index06 2 72ms existing_store done n/a n/a 172.16.144.80 node-1 n/a n/a 0 0 100.0% 1 0 0 100.0% 261  0 0 100.0%
  • 查看某个具体索引的分片恢复情况
curl localhost:9200/_cat/recovery/{index}

可使用该命令查看initializing分片的恢复进度


  • 查看segments内存占用情况
curl -sXGET "http://localhost:9200/_cat/nodes?h=name,segments.memory,heap.max&v"
name(es节点名)     segments.memory      heap.max
es.***.0           5.4gb                29.9gb
es.***.1           5.2gb                29.9gb

关注:segments.memory的使用是否超过堆内存的30%


  • 查看线程池
curl -sXGET "http://localhost:9200/_cat/thread_pool?v"
node_name    name                 active  queue  rejected
es.wjl.cn.0  index                    0     0        0
es.wjl.cn.0  listener                 0     0        0
  • 查看ES集群线程池状态
curl -sXGET "http://localhost:9200/_cat/thread_pool/bulk?v"
node_name  name active queue rejected
es.***.1   bulk      2     0     1749
es.***.0   bulk      0     0     1197
es.***.0   bulk      0     0      209

如果集群存在入库有延迟的情况,执行thread_poolAPI,如果reject>0,说明集群的处理能力低于入库请求,请求业务方降低入库速率。

curl -sXGET "http://localhost:9200/_cat/thread_pool/search?v"
node_name   name      active queue rejected
es.***.0    search      4     0       0
es.***.0    search     61    977    4326
es.***.1    search     61    495    3497

如果reject>0,说明集群的处理能力低于查询请求,需要降低查询速率。


  • 查看segment数量
curl -sXGET "http://localhost:9200/_cat/indices?h=index,segments.count&v"
index                       segments.count
seye-file-2019.08.20                   0
seye-udpflow-2019.08.16                0
seye-file-2019.08.18                   0
  • segment(段)一个段就是一批具有相同文件名,不同文件名后缀的lucene文件集合(如下图所示)。
    随着新数据不断进入系统,小的段会合并成大的段,以提高资源利用率。
    可以每天凌晨会对前一天的索引进行优化,将每个分片的多个段,合并成一个段。
    在这里插入图片描述

  • 查看节点情况
curl -sXGET "http://localhost:9200/_cat/nodes?h=name,uptime,segments.memory&v"
name(ES节点名)    uptime(节点启动时长)      segments.memory(segment占用内存)
es.wjl.cn.0       66.7d               93.8mb
es.wjl.cn.0       66.7d               82.3mb
es.wjl.cn.0       61.8d               112.1mb

  • 打开指定索引
curl -XPOST "http://localhost:9200/{index}/_open"
  • 关闭指定索引
curl -XPOST "http://localhost:9200/{index}/_close"

  • 设置total_shards_per_node
curl -XPUT "http://localhost:9200/{index}/_settings" -d '{"index":{"routing.allocation.total_shards_per_node":"1"}}'
  • 设置集群recovery数量
curl -sXPUT "http://localhost:9200/_cluster/settings" -d '{"transient":{"cluster.routing.allocation.node_concurrent_recoveries":"60"}}'
  • 将副本数设置为0
curl -sXPUT "http://localhost:9200/{index}/_settings" -H 'Content-Type: application/json' -d '{"index.number_of_replicas":0}'

  • 将索引的shard里的多个段合并成1个段,段越少,占用的资源越少,有利于提高查询速度
curl -XPOST 'http://localhost:9200/netops-syslog2018.07.19/_forcemerge?max_num_segments=1'
  • 使用_cat/nodes?v查看节点信息,区分哪个是当前的master,然后使用_cat/master对比. 带*的是master
curl -sXGET localhost:9200/_cat/nodes?v
curl -sXGET localhost:9200/_cat/master?v

  • 以组名或姓名为前缀,创建 "前缀-1"索引,"前缀-2"索引,不加任何设置。
curl -sXPUT localhost:9200/wjl-1
curl -sXPUT localhost:9200/wjl-2?pretty       // ?pretty: 美化输出.
[esuser@king bin]$ curl -sXPUT localhost:9200/wjl-2?pretty
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "wjl-2"
}
  • 以组名或姓名为前缀,创建 "前缀-3"索引,3个分片,每个分片0个副本
curl -sXPUT "localhost:9200/wjl-3?pretty" -H 'Context-Type: application/json' -d'
{
    "settings" :{
        "number_of_shards" :3,
        "number_of_replicas" :0
    }
}'

  • 向"前缀-2"的索引中添加一个doc,内容为{“name”:“任意”,“something”:“任意”}
curl -XPOST "http://localhost:9200/fengxiaoqing-2/content" -d '{
"name":"wjl",
"something":"This is a doc from wjl-2,叫做something"
}'

  • 查看指定索引的mapping和setting
curl -sXGET 'localhost:9200/{index}?pretty'

  • 删除"前缀-1"索引, 关闭"前缀-2"索引, 打开"前缀-2"索引
curl -sXDELETE localhost:9200/wjl-1
curl -sXPOST localhost:9200/wjl-2/_close
curl -sXPOST localhost:9200/wjl-2/_open

  • "前缀-3"动态修改副本数量为1
curl -sXPUT localhost:29200/wjl-3/_settings -d '{"index.number_of_replicas":1}'
  • 修改"前缀-3"索引的total_shards_per_node参数为1,查看索引setting,以及状态
curl -sXPUT "http://localhost:9200/wjl-3/_settings" -d '{"index.routing.allocation.total_shards_per_node":1}'

total_shards_per_node是索引级别的setting.


  • cat api 系列
curl -sXGET http://localhost:9200/_cat/allocation
curl -sXGET http://localhost:9200/_cat/plugins
curl -sXGET http://localhost:9200/_cat/nodes
curl -sXGET http://localhost:9200/_cat/indices_simple
curl -sXGET http://localhost:9200/_cat/indices_simple/{index}
curl -sXGET http://localhost:9200/_cat/fielddata
curl -sXGET http://localhost:9200/_cat/fielddata/{fields}
curl -sXGET http://localhost:9200/_cat/count
curl -sXGET http://localhost:9200/_cat/count/{index}
curl -sXGET http://localhost:9200/_cat/recovery
curl -sXGET http://localhost:9200/_cat/recovery/{index}
curl -sXGET http://localhost:9200/_cat/repositories
curl -sXGET http://localhost:9200/_cat/thread_pool
curl -sXGET http://localhost:9200/_cat/thread_pool/{thread_pools}/_cat/master
curl -sXGET http://localhost:9200/_cat/health
curl -sXGET http://localhost:9200/_cat/indices
curl -sXGET http://localhost:9200/_cat/indices/{index}
curl -sXGET http://localhost:9200/_cat/indices_ext
curl -sXGET http://localhost:9200/_cat/indices_ext/{index}
curl -sXGET http://localhost:9200/_cat/nodeattrs
curl -sXGET http://localhost:9200/_cat/tasks
curl -sXGET http://localhost:9200/_cat/segments
curl -sXGET http://localhost:9200/_cat/segments/{index}
curl -sXGET http://localhost:9200/_cat/pending_tasks
curl -sXGET http://localhost:9200/_cat/shards
curl -sXGET http://localhost:9200/_cat/shards/{index}
curl -sXGET http://localhost:9200/_cat/snapshots/{repository}
curl -sXGET http://localhost:9200/_cat/aliases
curl -sXGET http://localhost:9200/_cat/aliases/{alias}

每个命令后边跟"?help"参数,可以显示列头的含义。重点关注nodes, count, recovery, health, indices, pending_tasks, shards, alias, 需要掌握这几个api具体能查到什么信息。在定位集群问题时,会经常使用。

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值