3、ES常用命令

一、ES常用命令

1、查询命令

1.1、查看集群健康状态:

curl -k -u user:password https://127.0.0.1:9200/_cluster/health?v
查看集群的健康信息,主要是索引相关信息

1.2、查看节点状态

curl -k -u user:password https://127.0.0.1:9200/_nodes/stats?v
上面的命令包含了大量的信息,主要有各节点索引状态、节点资源占用(内存、cpu、磁盘...)、jvm使用情况、各项缓存大小、各种线程状态、。

1.3、查看热点线程:

curl -k -u user:password http://127.0.0.1:9200/_nodes/hot_threads?v
查看es集群中消耗cpu比较多的线程

1.4、查看模板:

curl -k -u user:password https://127.0.0.1:9200/_template?pretty
查看集群中存在的模板,后面可以接具体的模板名称 

2、操作命令

2.1、导入模板:

curl -XPUT -k -u user:password https://127.0.0.1:9200/_template/logstash -d @logstash.json
将本地模板文件导入es集群中,或者直接接json字符串 

2.2、创建索引

curl -k -u user:password -XPUT https://localhost:9200/indexname -d '{settings : {index : {number_of_shards : 5, number_of_replicas : 1 }}}'
number_of_shards代表索引的分片数
number_of_replicas代表副本数 

2.3、重命名索引

curl -k -u use:password-XPOST 'https://localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d'
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}'
虽然这个命令是重命名,但是实际的操作是先拷贝再删除 

2.4、关闭索引

curl -k -u user:password -XPOST https://localhost:9200/indexname/_close
Indexname这里可以写具体的索引名称,也可以使用通配符,但是如果有一个索引无法执行成功,整个操作就不会执行 

2.5、打开索引

curl -k -u user:password -XPOST https://localhost:9200/indexname/_open
同上 

2.5、删除索引

curl -k -u user:password -XDELETE https://localhost:9200/indexname
同上 

2.6、清理索引缓存

curl -s -k -u user:password https://localhost:9200/indexname/_cache/clear?field_data=true
这个命令感觉没什么用,对于jvm占用较多时并不能有效回收内存,官方的说法是使用这条命令只是将数据标记为可以回收 

2.7、修改最大获取数

curl -k -u user:password -XPUT https://localhost:9200/nginx-jr-*/_settings -d '{"index":{"max_result_window":"100000"}}'
通过es接口调用获取数据时,默认最大只能一次获取10000条数据,可以通过这条命令改大这个值 

2.8、关闭索引重分配

curl -XPUT -u user:admin -k 'https://localhost:9200/_cluster/settings' -d '{
"transient" : {
"cluster.routing.allocation.enable" : "none"
}
}'
重启es节点时最好先执行这个命令,防止集群将一个节点踢出后,索引开始重新分布和迁移 

2.9、打开索引重分

curl -XPUT -u user:admin -k 'https://localhost:9200/_cluster/settings' -d '{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}' 

2.10、修改索引刷新时间

curl -XPUT 127.0.0.1:9200/_settings -d '{"index" : {"refresh_interval" : "60s"}}'
新的数据写入es后不是立马就能查到,需要等索引刷新后才能查找到。默认是1s,但是对es的压力会比较大,可以根据实际情况更改。 

2.11、关闭索引刷新

curl -XPUT 127.0.0.1:9200/_settings -d '{"index" : {"refresh_interval" : "-1"}}'
在处理集群异常时,可以将索引刷新先关闭,减小es的压力 

2.12、增大段合并速度

curl -XPUT http://127.0.0.1:9200/_cluster/settings -d '{"persistent":{"indices.store.throttle.max_bytes_per_sec" : "80mb"}}'
如果es采用的一些快速存储介质(比如ssd),可以根据实际情况增大这个值 
 

2.13、修改索引副本数

curl -XPUT -u user:password -k https://localhost:9200/index_name/_settings -d '{"index" : {"number_of_replicas" : 0}}'
将index_name改为想要修改的索引名
number_of_replicas后面接副本数,0表示没有副本

2.14、增加索引分布速度


curl -XPUT -uadmin -k 'https://localhost:9200/_cluster/settings' -d '{
"transient" : {
"indices.recovery.max_bytes_per_sec": "500mb",
"cluster.routing.allocation.node_initial_primaries_recoveries": 25,
"cluster.routing.allocation.node_concurrent_recoveries": 10,
"cluster.routing.allocation.cluster_concurrent_rebalance": 10,
"indices.recovery.concurrent_streams": 25
}
}' 

2.15、重定位分片

curl -XPOST -k -u user:password 'http://localhost:9200/_cluster/reroute' -d '{"commands" : [ {"allocate" : {"index" : "index_name","shard" : "shard_id","node" : "nodename","allow_primary" : true}}]}'
将上面命令中index_name、shard_id、nodename换成对应的索引名称、分片id和节点名称

二、cat命令

Cat命令是es提供的一组查询api,几乎集群的所有的信息都可以通过其查到,同时提供了十分详细的命令帮助 
# curl -u user:password -k https://127.0.0.1:9200/_cat?help
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}

上面的每一个子接口也都可以用?help来查看帮助,比如:
# curl -u user:password -k https://127.0.0.1:9200/_cat/allocation?help
shards | s | number of shards on node 
disk.indices | di,diskIndices | disk used by ES indices 
disk.used | du,diskUsed | disk used (total, not just ES)
disk.avail | da,diskAvail | disk available 
disk.total | dt,diskTotal | total capacity of all volumes 
disk.percent | dp,diskPercent | percent disk used 
host | h | host of node 
ip | | ip of node 
node | n | name of node 
第一列是全名,第二列是缩写,第三列为解释
例子:
# curl -u user:password -k https://127.0.0.1:9200/_cat/allocation
如果需要看到每列代表什么,可以加上?v
# curl -u user:password -k https://127.0.0.1:9200/_cat/allocation?v
如果需要看某些指定的值,可以加上?h=x,xx
# curl -u user:paswword -k https://127.0.0.1:9200/_cat/allocation?h=ip,n,h,s

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值