es常用curl命令

Posted lovely可爱欧辰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es常用curl命令相关的知识,希望对你有一定的参考价值。

【说明】仅记录实验室测试过程,不作为官方文档使用,可能会有很多地方未能验证,因此无法进行技术兜底,需使用方多加验证测试,涉及到高危需走变更。目前测试版本均为651及以前版本,命令样例基于安全模式,如果是在非安全模式下,将命令中的参数“--tlsv1.2 --negotiate -k -u : 去除,并将https修改为http即可。

端口:

EsMaster: 24148

EsNode1: 24100

EsNode2: 24102


 

_cluster命令   

查询集群状态命令:
curl -XGET -
–tlsv1.2 –negotiate -k -u : "https://ip:port/_cluster/health?pretty"

查询ES全局状态
curl -XGET –tlsv1.2 –negotiate -k -u : 'https://ip:port/_cluster/stats?pretty'

解释分片,查看分片状态

curl -XGET --negotiate -k -u : "https://127.0.0.1:24100/_cluster/allocation/explain?pretty" -H 'Content-Type:application/json' -d '

    "index": "indexname",
    "shard": 17,
    "primary": true
'

重新分配主分片

curl -XPOST --tlsv1.2 --negotiate -k -u : "https://127.0.0.1:24148/_cluster/reroute?pretty" -H 'Content-Type:application/json' -d '

    "commands": [

       "allocate_stale_primary":

           "index": "index1",

           "shard": 2,

           "node": "EsNode1@189.39.172.103",

                        "accept_data_loss":true

 

       

    ]

'

 

 

_cat命令   

 


查询所有索引:
curl -XGET --tlsv1.2--negotiate -k -u : "https://ip:port/_cat/indices?pretty"

查询ES实例列表
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/nodes/?v'

查看_cat/nodes接口的帮助信息: 
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/nodes?help'

查询所有esnode的指定参数列的值,包括堆内存等
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/nodes?v&h=http,dt,du,dup,hm,hc,hp,fdm,fdc,fdp,cpu'

 

查询es集群是否健康
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/health?v'

查询es集群index的整体数据量
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/indices?v'

查询集群线程池使用情况
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/thread_pool?v'


查询集群bulk操作的线程数量
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/thread_pool/bulk?v'
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/thread_pool/bulk?v&h=node_name,name,active,queue,rejected,c'


查询主Master实例
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/master/?v'

查询每个实例上的分片个数,用于判断分片是否均匀
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/allocation?v'

查询_cat接口支持的所有的命令列表
curl -XGET --tlsv1.2 --negotiate -k -u : 'https://ip:port/_cat/'

查看分片未分配的原因
curl -XGET --tlsv1.2 --negotiate -k -u : "https://127.0.0.1:24100/_cat/shards?v&h=index,shard,prirep,state,unassigned.reason" 

 

 

_nodes命令   


查询指定ES实例的jvm参数:
curl -XGET –tlsv1.2 –negotiate -k -u : 'https://ip:port/_nodes/EsNode1*/stats/jvm?pretty'
curl -XGET –tlsv1.2 –negotiate -k -u : 'https://ip:port/_nodes/EsNode1@12.40.16.156/stats/jvm?pretty'

 

 

索引操作   


关闭索引
curl -XPOST --tlsv1.2 --negotiate -k -u : 'https://ip:port/my_index/_close?pretty'

打开索引
curl -XPOST --tlsv1.2  --negotiate -k -u : 'https://ip:port/my_index/_open?pretty'


删除索引,命令最后为索引名称,只有该索引的创建用户才可以删除索引
curl -XDELETE –tlsv1.2 –negotiate -k -u : 'https://ip:port/test_huawei'

查询索引mapping和settings

curl -XPOST--tlsv1.2  --negotiate -k -u : 'https://ip:port/my_index_name?pretty'

查询索引settings

curl -XPOST--tlsv1.2  --negotiate -k -u : 'https://ip:port/my_index_name/_settings?pretty'

修改索引刷新时间:

curl -XPUT --negotiate -k -u : 'https://ip:port/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'"refresh_interval" : "60s"'

修改translog文件保留时长,默认为12小时

curl -XPUT --negotiate -k -u : 'https://ip:port/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'"index.translog.retention.age" : "30m"'

设置索引副本:
curl -XPUT --negotiate -k -u : 'https://ip:port/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'"number_of_replicas" : 1'

强制flush

curl -XPOST --negotiate --tlsv1.2 -k -u :  'https://ip:port/myindex/_flush'

强制refresh

curl -XPOST --negotiate --tlsv1.2 -k -u :  'https://ip:port/myindex/_refresh'

强制执行段合并

curl -XPOST --tlsv1.2 --negotiate -k -v -u : 'https://ip:httpport/myindex-001/_forcemerge?only_expunge_deletes=false&max_num_segments=1&flush=true&pretty'

 

配置控制段合并的refresh、merge线程数等
curl -XPUT  --tlsv1.2  --negotiate -k -u : "https://ip:port/my_index/_settings?pretty" -H 'Content-Type: application/json' -d'

"refresh_interval": "360s",
"merge":
"scheduler":
"max_merge_count" : "100",
"max_thread_count" : "1"
,
"policy":
"segments_per_tier" : "100",
"floor_segment" : "1m",
"max_merged_segment" : "2g"


'


设置索引的刷新时间和translog配置参数

注意:设置translog参数,必须先关闭索引,设置完成后再打开

*代表设置所有索引,如果要设置具体某个索引,可以将*替换为具体的索引名称

curl -XPUT --tlsv1.2 --negotiate -k -v -u : "https://ip:httpport/*/_settings" -H 'Content-Type: application/json' -d'

"index":
"refresh_interval" : "60s",
"translog":
"flush_threshold_size": "1GB",
"sync_interval": "120s",
"durability": "async"


'


 

手动分片迁移操作

curl -XPOST --tlsv1.2 --negotiate -k -v -u :  'https://ip:httpport/_cluster/reroute?pretty' -H 'Content-Type:application/json' -d '
"commands" : [
"move" :
"index" : "info-test", "shard" : 3,
"from_node" : "EsNode3@189.39.172.103",
"to_node" : "EsNode2@189.39.172.103"

]
'

 

限制每个索引在每个实例上的分片个数

curl -XPUT --tlsv1.2 --negotiate -k -v -u : 'https://http:httpport/myindex/_settings?pretty' -H 'Content-Type:application/json' -d '"index.routing.allocation.total_shards_per_node":"2"'
 

Shrink操作

1目前集群默认设置的是同一台主机不能存放同一主副本分片,需先在后台关闭该设置。防止分片自动均衡,关闭分片分配。

curl -XPUT 'http://127.0.0.1:24100/_cluster/settings?pretty'-H 'Content-Type:application/json' -d '"transient":"cluster.routing.allocation.same_shard.host":false,"cluster.routing.allocation.enable":"none"'

2将索引标记为只读,且所有的分片副本都迁移到实例名为EsNode1@IP的实例上。注意:所有的分片副本不指定索引的全部分片,无论主分片还是副本分片,任意一个就行。

curl -XPUT "http://127.0.0.1:24100/jjj.pm_20190224/_settings" -H 'Content-Type: application/json' -d'

"settings":    

"index.routing.allocation.require._name": "EsNode1@IP",

"index.blocks.write": true

 

'

3执行完上述命令之后,分片开始迁移,执行下列命令,查看分片是否迁移完成。

curl –XGET http://127.0.0.1:24100/_cat/shards?v | grep jjj.pm_20190224

4分片转移完成之后,查看集群是否green

curl -XGET http://127.0.0.1:24100/_cluster/health?pretty

5等待分片迁移完成之后,集群green后,我们就可以执行Shrink操作了。

curl -XPOST "http://127.0.0.1:24100/jjj.pm_20190224/_shrink/ jjj.pm_20190224_new" -H 'Content-Type: application/json' -d'

"settings":

"index.number_of_replicas": 1,

"index.number_of_shards": 41,

"index.codec": "best_compression"

'

以上代码将创建含有一个41个主分片和一个副本分片的目的索引jjj.pm_20190224_new

6删除原来的索引

curl -XDELETE http://127.0.0.1:24100/jjj.pm_20190224?pretty

7并为新建的索引设置别名,别名设置为原来索引名称。

curl -XPOST 'http://127.0.0.1:24100/_aliases?pretty' -H 'Content-Type: application/json' -d'

"actions":

["add":"index": "jjj.pm_20190224_new","alias":"jjj.pm_20190224"]'

8查看集群状态,green之后,还原参数。

curl -XPUT 'http://127.0.0.1:24100/_cluster/settings?pretty'-H 'Content-Type:application/json' -d

'"transient":"cluster.routing.allocation.same_shard.host":true,"cluster.routing.allocation.enable":"all"'

 

 

_template操作   


查看模板
curl -XGET –tlsv1.2 –negotiate -k -u : "https://ip:port/_template/template_1?pretty"

设置/修改模板
curl -XPUT –tlsv1.2 –negotiate -k -u : "https://ip:port/_template/template_1" -H 'Content-Type: application/json' -d ' "template" : "*", "order" : 0, "settings" : "number_of_shards" : 1 , "mappings" : "type1" : "_source" : "enabled" : false '

 

SQL插件常用用法   

curl -XPOST –tlsv1.2 –negotiate -k -u : 'https://ip:port/_sql?pretty' -H "Content-Type: application/json"  -d 'SELECT * FROM test_index'

如上所示,三个地方需要注意:

1.        SQL语句使用 ”-XPOST”

2.        Ip:port后需要跟 ”_sql”

3.        -d后直接跟SQL语句

全局查询:curl -XPOST –tlsv1.2 –negotiate -k -u : 'https://ip:port/_sql?pretty' -H "Content-Type: application/json"  -d 'SELECT * FROM test_index'

单关键字查询:curl -XPOST –tlsv1.2 –negotiate -k -u : 'https://ip:port/_sql?pretty' -H "Content-Type: application/json"  -d 'SELECT * FROM test_index where content="美国"'

多关键字查询:

curl -XPOST –tlsv1.2 –negotiate -k -u : 'https://ip:port/_sql?pretty' -H "Content-Type: application/json"  -d 'SELECT * FROM test_index where content="美国" and content="中国"'

curl -XPOST –tlsv1.2 –negotiate -k -u : 'https://ip:port/_sql?pretty' -H "Content-Type: application/json"  -d 'SELECT * FROM test_index where content="美国" or content="中国"'

单字段排序:

curl -XPOST –tlsv1.2 –negotiate -k -u : 'https://ip:port/_sql?pretty' -H "Content-Type: application/json"  -d 'SELECT * FROM test_index where content="美国" or content="中国" ORDER BY _score DESC'

多字段排序:

curl -XPOST –tlsv1.2 –negotiate -k -u : 'https://ip:port/_sql?pretty' -H "Content-Type: application/json"  -d 'SELECT * FROM test_index where (content1="美国" or content1="中国") ORDER BY content2 ASC, _score DESC'

模糊查询:

curl -XPOST –tlsv1.2 –negotiate -k -u : 'https://ip:port/_sql?pretty' -H "Content-Type: application/json"  -d 'SELECT * FROM test_index where content1 like "喀*湖"'

限制输出个数:

curl -XPOST –tlsv1.2 –negotiate -k -u : 'https://ip:port/_sql?pretty' -H "Content-Type: application/json"  -d 'SELECT * FROM test_index limit 2' 

 

解释分片命令   

curl -XGET --negotiate -k -u : "https://127.0.0.1:24100/_cluster/allocation/explain?pretty" -d '
"index": "indexname",
"shard": 17,
"primary": true
'

 

bulk操作   

读取文件夹下的文件,通过bulk写入:

for file_each in `ls`
do
curl -XPOST 
–tlsv1.2 --negotiate -k -v -u : 'https://127.0.0.1:24100/_bulk?pretty' -H 'Content-Type:application/json' --data-binary @$file_each
done

以上是关于es常用curl命令的主要内容,如果未能解决你的问题,请参考以下文章

es常用curl命令

es常用curl命令

es集群问题排查常用命令参数解析

es集群问题排查常用命令参数解析

查询es curl命令记录

ES 搜索—— 常用查询语句