curl 命令操作ES

curl介绍

curl其实是一种用URL语法,它是一种传输数据工具,是通过命令来进行工作的。Curl在很多的操作系统中被使用,其中包括Unix、和Linux,除此之外,也有DOS和Win64等的版本。curl 命令是利用 url 在命令行下进行工作的传输工具,它支持包括 file、ftp、ftps、http、https、imap、imaps、ldap、ldaps、mqtt、pop、pop3s、rtmp、rtmps、rtsp、scp、sftp、smb、smbs、smtp、smtps、telnet 和 tftp 等协议。

curl使用

基本语法:curl [option] [url]

option的参数使用我们可以使用 curl -h 命令查看帮助

-o, --output <file> 写入到文件,而不是输出到stdout

-O 写入到文件,文件名和远程文件一样

-L 跟随网站的跳转

-x, --proxy [protocol://][user:pwd@]host[:port] 使用代理

-v 打印过程

--trace <file> debug写入到文件,很详细包括二进制数据交换,file使用 - 表示打印到stdout

-c <file> 将服务器设置的cookie写入到文件

-b <data> 发送cookie,从 string/file 获取

-A <name> 发送 User-Agent <name> 到服务器

-e <url> 指定 Referer : <url> , 仿造referer,服务器会以为你是从 url 点击某个链接过来的

-H <header/@file> 将自定义标头传递到服务器

-X <command> 指定请求方法,不带任何参数的请求默认get方法

-s Silent mode 无声模式

-S Show error even when -s is used 即使使用 -s 也打印错误

-i 打印服务器回应的http标头

-I 只打印标头

-k 使用ssl时,允许不安全的服务器连接。跳过ssl检测

-d <data> http post data,使用post方法发送表单,自动添加标头Content-Type : application/x-www-form-urlencoded

-F <name=content> 指定 multipart MIME data , 可以上传二进制文件,自动添加Content-Type: multipart/form-data

-G 把 post data 放进 url 并使用 get 请求,与-d配合

-u <user:password> 指定服务器用户和密码

-T <file> 上传文件,使用 put 请求

curl命令操作ES

使用curl请求elasticsearch查询信息格式如下:

curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
    
VERB
适当的 HTTP 方法 或 谓词 : GET、POST、PUT、HEAD 或者 DELETE。

PROTOCOL
http 或者 https(如果你在 Elasticsearch 前面有一个https 代理)

HOST
Elasticsearch 集群中任意节点的主机名,或者用 localhost 代表本地机器上的节点。

PORT
运行 Elasticsearch HTTP 服务的端口号,默认是 9200 。

PATH
API 的终端路径(例如 _count 将返回集群中文档数量)。Path 可能包含多个组件,例如:_cluster/stats 和 _nodes/stats/jvm 。

QUERY_STRING
任意可选的查询字符串参数 (例如 ?pretty 将格式化地输出 JSON 返回值,使其更容易阅读)

BODY
一个 JSON 格式的请求体 (如果请求需要的话)

检查ES是否启动成功

curl http://localhost:9200
{
  "name" : "Myhost",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "abtEL4GKRfulSwTfJ0wX5Q",
  "version" : {
    "number" : "7.2.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "508c38a",
    "build_date" : "2019-06-20T15:54:18.811730Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

命令帮助

curl 'localhost:9200/_cat'

查看所有的 Index (v是用来要求结果返回表头)

curl 'localhost:9200/_cat/indices?v'

关闭索引库(支持批量)

curl 'localhost:9200/my-index*/_close'

打开索引库(支持批量)

curl 'localhost:9200/my-index*/_open'

检查集群状态

curl 'localhost:9200/_cat/health?v'

查看es集群配置

curl -X GET "localhost:9200/_cluster/settings?pretty"

查看指定索引库下文档数量

curl 'localhost:9200/_cat/count/my_book?v'

通配符查询某类索引库

curl 'localhost:9200/_cat/indices/my_*?v'

查看索引库的别名配置

curl 'localhost:9200/_aliases'

查看索引库的mapping配置

curl 'localhost:9200/my_book/_mapping'

查看索引库的mapping配置(格式化展示,pretty参数表示让结果以json格式输出展示)

curl 'localhost:9200/my_book/_mapping?pretty'

查看索引库的全量数据

curl 'localhost:9200/my_book/_search?pretty'

查看索引分布情况
curl -X GET "localhost:9200/_cat/shards?v=true&pretty"

查看索引库的某一条文档数据

curl 'localhost:9200/my_book/1001?pretty'

根据条件搜索文档

curl -X POST "localhost:9200/alert*/_search?pretty" -H 'Content-Type:application/json' -d '{"query":{"match":{"_id":"66616090260607"}}}'

修改ES最大分片数
curl -XPUT -H "Content-Type:application/json" http://localhost:9200/_cluster/settings -d '{"transient":{"cluster":{"max_shards_per_node":10000}}}'

删除索引库

curl -X DELETE "localhost:9200/my_test_index01"

根据条件删除文索引库数据,以下以id为例

curl -X POST "localhost:9200/my_test_index01/_delete_by_query?pretty" -H 'Content-Type:application/json' -d '{"query":{"match":{"_id":"1001"}}}'
清空索引库

curl -X POST "localhost:9200/my_test_index01/_delete_by_query?pretty" -H 'Content-Type:application/json' -d '{"query":{"match_all":{}}}'
修改索引库字段内容

curl -X POST "localhost:9200/my_test_index01/_update/1001?pretty" -H 'Content-Type:application/json' -d '{"doc":{"name":"蓝闪"}'

es维护篇

`
查看当前es线程组状态

curl -XGET "localhost:9200/_nodes/stats?pretty"

列出集群范围的设置(明确定义)

curl "localhost:9200/_cluster/settings"

以平面格式列出集群范围的设置(明确定义)

curl "localhost:9200/_cluster/settings?flat_settings&pretty"

列出集群范围的设置(包括默认值)

curl "localhost:9200/_cluster/settings?include_defaults&pretty"

以平面格式列出集群范围的设置(包括默认值)

curl "localhost:9200/_cluster/settings?include_defaults&flat_settings&pretty"

对熔断器状态进行监控

curl -XGET "localhost:9200/_nodes/stats/breaker?pretty"

查看每个数据节点上的分片数(shards),以及每个数据节点磁盘剩余

curl -XGET "localhost:9200/_cat/allocation?v"

获得每个节点的当前堆内存使用率

curl -XGET "localhost:9200/_cat/nodes?v=true&h=name,node*,heap*"

查看每个数据节点上被 fielddata 所使用的堆内存大小API

curl -XGET "localhost:9200/_cat/fielddata?v"

fielddata-按索引级别使用

curl -XGET "localhost:9200/_stats/fielddata?fields=*&pretty"

fielddata-按节点级别使用

curl -XGET "localhost:9200/_nodes/stats/indices/fielddata?fields=*&pretty"

fielddata-按索引节点级别使用

curl -XGET "localhost:9200/_nodes/stats/indices/fielddata?level=indices&fields=*&pretty"

查看指定索引segment数量

curl -XGET "localhost:9200/_cat/segments/test_segment?v&h=shard,segment,size,size.memory

查看一个node上所有segment占用的memory总和

curl -XGET "localhost:9200/_cat/nodes?v&h=name,port,sm&pretty"

查看集群内每个索引 Segment 的整体情况和合并情况

curl -XGET "localhost:9200/_cat/indices/?s=segmentsMemory:desc&v&h=index,segmentsCount,segmentsMemory,memoryTotal,mergesCurrent,mergesCurrentDocs,storeSize"

监控某个索引部分内存占用情况

curl -XGET "localhost:9200/_cat/indices/test1?h=*memory*&format=json&pretty"

监控到节点的部分内存使用情况和缓存命中情况

curl -XGET "localhost:9200/_cat/nodes?h=name,*heap*,*memory*,*Cache*&format=json&pretty"

查看es当前JVM各块内存的使用情况
curl -X GET "localhost:9200/_cat/nodes?v&h=id,ip,port,r,ramPercent,ramCurrent,heapMax,heapCurrent,fielddataMemory,queryCacheMemory,requestCacheMemory,segmentsMemory"

ES的JVM heap按使用场景分为可GC部分和常驻部分。 可GC部分内存会随着GC操作而被回收; 常驻部分不会被GC,通常使用LRU策略来进行淘汰; 内存占用情况如下图:
image

posted @ 2022-12-16 16:31  忘崽牛仔  阅读(2750)  评论(0编辑  收藏  举报