使用Curl命令来与ES集群进行交互

释放双眼,带上耳机,听听看~!

当我们的ES集群搭建完成以后,我怎么能看到集群中各个节点状态以及主节点和健康情况呢,如下讲解使用curl命令来与ES集群进行交互、分别有查询主节点情况、集群状态、以及创建索引查看索引、查看分片以及对ES集群进行查询请求等操作。

语法讲解

RESTful API:
        curl  -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
        -X:指定请求方式
            
        <VERB>:GET,POST,PUT,DELETE   一般请求为GET、提交变更为POST、上传文件为PUT、删除操作为DELETE
            
        <PROTOCOL>:协议,一般为HTTP协议
                
        <HOST>:主机,可为主机的IP地址或Hostname
            
        <PORT>:主机端口
            
        <PATH>:路径,主机端口后的路径,如下几个路径:/_cat, /_search, /_cluster   /_cat路径覆盖了大多数的信息内容,/_search为搜索所有的索引和文档类型
            
        <QUERY_STRING>:查询字符串匹配规则
            
        -d:指定主体内容
            
        <BODY>:json格式的请求主体

_cat路径所支持的查询内容

curl -XGET 'http://10.150.30.246:9201/_cat'  
=^.^=
/_cat/allocation                    #查看数据分配
/_cat/shards                        #查看分片
/_cat/shards/{index}
/_cat/master                        #查看主节点信息
/_cat/nodes                         #查看ES节点信息
/_cat/tasks                         #查看各节点任务
/_cat/indices                       #查看ES集群中的索引状态
/_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/thread_pool/{thread_pools}
/_cat/plugins                       #查看插件
/_cat/fielddata                     #查看文件数据
/_cat/fielddata/{fields}
/_cat/nodeattrs 
/_cat/repositories                  #查看ES集群存储库
/_cat/snapshots/{repository}
/_cat/templates                     #查看ES集群模版

查看ES节点信息

curl -XGET 'http://10.150.30.246:9201/_cat/nodes?v'     #使用?v是可以人性化显示
ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
10.150.30.246            8          71   0    0.00    0.02     0.05 mi        *      ES-master-10.150.30.246
10.150.55.94             2          65   0    0.00    0.01     0.05 mi        -      ES-master-10.150.55.94
10.150.30.241            9          71   0    0.15    0.06     0.06 di        -      ES-Data-10.150.30.241
10.150.30.242            8          71   0    0.00    0.01     0.05 i         -      ES-Client-10.150.30.242
10.150.55.95             4          70   0    0.01    0.02     0.05 mdi       -      ES-master-10.150.55.95
10.150.55.88             9          74   0    0.00    0.01     0.05 di        -      ES-Data-10.150.55.88
10.150.55.89            10          74   0    0.00    0.01     0.05 di        -      ES-Data-10.150.55.89

查看ES主节点信息

curl -XGET 'http://10.150.30.246:9201/_cat/master?v'
id                     host          ip            node
c1kkMSrVSume5gFP7T8m0A 10.150.30.246 10.150.30.246 ES-master-10.150.30.246

查看ES集群分片信息

curl -XGET 'http://10.150.30.246:9201/_cat/shards?v'
index                shard prirep state   docs  store ip            node
.kibana_task_manager 0     p      STARTED    2 28.7kb 10.150.55.95  ES-master-10.150.55.95
.kibana_task_manager 0     r      STARTED    2 28.7kb 10.150.30.241 ES-Data-10.150.30.241
index_name           0     r      STARTED    1  4.1kb 10.150.55.95  ES-master-10.150.55.95
index_name           0     p      STARTED    1  4.1kb 10.150.55.89  ES-Data-10.150.55.89
.kibana_1            0     p      STARTED    4 48.7kb 10.150.55.95  ES-master-10.150.55.95
.kibana_1            0     r      STARTED    4 48.5kb 10.150.55.89  ES-Data-10.150.55.89
wd                   0     p      STARTED    1  4.1kb 10.150.30.241 ES-Data-10.150.30.241
wd                   0     r      STARTED    1  4.1kb 10.150.55.88  ES-Data-10.150.55.88
migu                 0     p      STARTED    1    4kb 10.150.55.89  ES-Data-10.150.55.89
migu                 0     r      STARTED    1    4kb 10.150.55.88  ES-Data-10.150.55.88
xuweiliang           0     p      STARTED    1    4kb 10.150.55.95  ES-master-10.150.55.95
xuweiliang           0     r      STARTED    1    4kb 10.150.55.88  ES-Data-10.150.55.88
pudong               0     p      STARTED    1  4.1kb 10.150.55.89  ES-Data-10.150.55.89
pudong               0     r      STARTED    1  4.1kb 10.150.30.241 ES-Data-10.150.30.241

查看ES集群索引

curl -XGET 'http://10.150.30.246:9201/_cat/indices?v'
health status index                uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   migu                 bOnIzhTsQmOPTR5nsaVmBw   1   1          1            0        8kb            4kb
green  open   xuweiliang           JK_qzwOfReCCgp-IpXhWsA   1   1          1            0      8.1kb            4kb
green  open   index_name           krb465ShROy_iAk2pOd4IA   1   1          1            0      8.3kb          4.1kb
green  open   .kibana_task_manager Nz5kSnVfSIG0fPtZQqhlZw   1   1          2            9     57.5kb         28.7kb
green  open   .kibana_1            11tOPxt8Qe6KQjhxn0PzxQ   1   1          4            7     97.2kb         48.7kb
green  open   wd                   PibYiRMSQOWPMU2pF_IuFQ   1   1          1            0      8.2kb          4.1kb
green  open   pudong               U9-lk1piQ-m85Vb3lmZFDQ   1   1          1            0      8.2kb          4.1kb

查看各节点任务

curl -XGET 'http://10.150.30.246:9201/_cat/tasks?v'  
action                         task_id                       parent_task_id                type      start_time    timestamp running_time ip            node
cluster:monitor/tasks/lists    c1kkMSrVSume5gFP7T8m0A:281050 -                             transport 1558415853561 05:17:33  1.1ms        10.150.30.246 ES-master-10.150.30.246
cluster:monitor/tasks/lists[n] rXpaYmBpSbSQycN-VT-Org:427552 c1kkMSrVSume5gFP7T8m0A:281050 netty     1558415853499 05:17:33  538micros    10.150.30.242 ES-Client-10.150.30.242
cluster:monitor/tasks/lists[n] EZf679rMQv6hZ7tniN5fNQ:562919 c1kkMSrVSume5gFP7T8m0A:281050 netty     1558415853535 05:17:33  117.8micros  10.150.55.95  ES-master-10.150.55.95
cluster:monitor/tasks/lists[n] ZZL7p0rkRC-Nu2cwqm_52Q:577782 c1kkMSrVSume5gFP7T8m0A:281050 netty     1558415853537 05:17:33  128.1micros  10.150.55.89  ES-Data-10.150.55.89
cluster:monitor/tasks/lists[n] 0O5Bm6GISHGQIDeTfYcU3Q:495263 c1kkMSrVSume5gFP7T8m0A:281050 netty     1558415853541 05:17:33  163.5micros  10.150.55.88  ES-Data-10.150.55.88
cluster:monitor/tasks/lists[n] c1kkMSrVSume5gFP7T8m0A:281051 c1kkMSrVSume5gFP7T8m0A:281050 direct    1558415853562 05:17:33  616.7micros  10.150.30.246 ES-master-10.150.30.246
cluster:monitor/tasks/lists[n] LkKWEioYTDOZCjykMbsWCw:563126 c1kkMSrVSume5gFP7T8m0A:281050 netty     1558415853589 05:17:33  106.3micros  10.150.30.241 ES-Data-10.150.30.241
cluster:monitor/tasks/lists[n] Z-vpnQ6UQsyXMm6JPt-mKQ:427973 c1kkMSrVSume5gFP7T8m0A:281050 netty     1558415853598 05:17:33  166.5micros  10.150.55.94  ES-master-10.150.55.94

查看集群分片的健康状态

curl -XGET 'http://10.150.30.246:9201/_cat/health?v'
epoch      timestamp cluster    status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1558415918 05:18:38  ES-Cluster green           7         4     14   7    0    0        0             0                  -                100.0%

创建文档

语法如下:

curl -H "Content-Type:application/json" -XPUT 'http://10.150.30.246:9201/index-name/type-name/10?pretty' -d '
> {"name": "xuwl", "age": 19, "job": "Linux", "company": "wangda" } '

命令介绍
-H:指定内容类型,这里为josn格式的文档
-X:指定http请求方式,这里为PUT上传方式
http://10.150.30.246:9201:指定一台es服务器对外的http端口
/index-name:指定文档的索引名称,必须小写
/type-name:指定文档的类型名称,必须小写
/10:文档的ID编号
?pretty:人性化创建索引
-d:指定使用JSON方式来撰写上传文档
{"name": "xuwl", "age": 19, "job": "Linux", "company": "wangda" } ':使用JSON格式来撰写上传文档内容,为键=值

创建索引名称为index-name的索引

curl -H "Content-Type:application/json" -XPUT 'http://10.150.30.246:9201/index-name/type-name/10?pretty' -d '
> {"name": "xuwl", "age": 19, "job": "Linux", "company": "wangda" } '
{
  "_index" : "index-name",
  "_type" : "type-name",
  "_id" : "10",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,                               #成功条数
    "failed" : 0                                    #失败条数
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

创建同名索引同类型名称下不同的文档编号

 curl -H "Content-Type:application/json" -XPUT 'http://10.150.30.246:9201/index-name/type-name/20?pretty' -d '
> {"name": "zhang", "age": 20, "job": "Linux", "company": "migu" } '
curl -H "Content-Type:application/json" -XPUT 'http://10.150.30.246:9201/index-name/type-name/30?pretty' -d '
> {"name": "wang", "age": 21, "job": "Linux", "company": "yinxin" } '

创建索引名称为iquanmian,文档类型为dockerc,文档编号为40的文档

 curl -H "Content-Type:application/json" -XPUT 'http://10.150.30.246:9201/iquanmian/dockerc/40?pretty' -d '
> {"name": "lisir", "age": 22, "job": "Linux", "company": "zhuowang" } '

创建索引名称为apple,文档类型为iphone,编号为50的文档

curl -H "Content-Type:application/json" -XPUT 'http://10.150.30.246:9201/apple/iphone/50?pretty' -d '
> {"name": "iphonex", "age": 23, "job": "java", "company": "apple" } '

失败案例
报错内容说,同一个索引名称下不允许出现两个文档类型

curl -H "Content-Type:application/json" -XPUT 'http://10.150.30.246:9201/apple/mac/60?pretty' -d '
> {"name": "macbookpro", "age": 24, "job": "java", "company": "apple" } '
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Rejecting mapping update to [apple] as the final mapping would have more than 1 type: [iphone, mac]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Rejecting mapping update to [apple] as the final mapping would have more than 1 type: [iphone, mac]"
  },
  "status" : 400
}

更正索引名称,重新创建一个索引名称为mac的索引

curl -H "Content-Type:application/json" -XPUT 'http://10.150.30.246:9201/mac/macbookpro/60?pretty' -d '
> {"name": "macbookpro", "age": 24, "job": "java", "company": "apple" } '

查看分片及索引
查看索引内容

我们刚新建的索引为iquanmina、index-name、mac、apple
curl -XGET 'http://10.150.30.246:9201/_cat/indices'
green open migu                 bOnIzhTsQmOPTR5nsaVmBw 1 1 1 0    8kb    4kb
green open iquanmian            lYX0Q4XZSn62Xw89hAQ1Ow 1 1 1 0  9.9kb  4.9kb        
green open xuweiliang           JK_qzwOfReCCgp-IpXhWsA 1 1 1 0  8.1kb    4kb
green open index_name           krb465ShROy_iAk2pOd4IA 1 1 1 0  8.3kb  4.1kb
green open mac                  F9Qtx5j3QPmZyL6iuTNR9Q 1 1 1 0  9.6kb  4.8kb
green open apple                SiD7vLPDRb6TNn7iqMPrHg 1 1 1 0  9.8kb  4.9kb
green open .kibana_task_manager Nz5kSnVfSIG0fPtZQqhlZw 1 1 2 9 57.5kb 28.7kb
green open index-name           SQ1VApC-TCykJPtFuH4hBw 1 1 3 0 28.3kb 14.1kb
green open .kibana_1            11tOPxt8Qe6KQjhxn0PzxQ 1 1 4 7 97.2kb 48.7kb
green open wd                   PibYiRMSQOWPMU2pF_IuFQ 1 1 1 0  8.2kb  4.1kb
green open pudong               U9-lk1piQ-m85Vb3lmZFDQ 1 1 1 0  8.2kb  4.1kb

查看分片内容

新建索引后自动生成的分片为mac、iquanmian、apple、index-name,每个分片都有主分片和副本分片
 curl -XGET 'http://10.150.30.246:9201/_cat/shards'
.kibana_task_manager 0 p STARTED 2 28.7kb 10.150.55.95  ES-master-10.150.55.95
.kibana_task_manager 0 r STARTED 2 28.7kb 10.150.30.241 ES-Data-10.150.30.241
xuweiliang           0 p STARTED 1    4kb 10.150.55.95  ES-master-10.150.55.95
xuweiliang           0 r STARTED 1    4kb 10.150.55.88  ES-Data-10.150.55.88
mac                  0 p STARTED 1  4.8kb 10.150.55.95  ES-master-10.150.55.95
mac                  0 r STARTED 1  4.8kb 10.150.55.89  ES-Data-10.150.55.89
wd                   0 p STARTED 1  4.1kb 10.150.30.241 ES-Data-10.150.30.241
wd                   0 r STARTED 1  4.1kb 10.150.55.88  ES-Data-10.150.55.88
iquanmian            0 p STARTED 1  4.9kb 10.150.55.95  ES-master-10.150.55.95
iquanmian            0 r STARTED 1  4.9kb 10.150.55.89  ES-Data-10.150.55.89
pudong               0 p STARTED 1  4.1kb 10.150.55.89  ES-Data-10.150.55.89
pudong               0 r STARTED 1  4.1kb 10.150.30.241 ES-Data-10.150.30.241
apple                0 p STARTED 1  4.8kb 10.150.30.241 ES-Data-10.150.30.241
apple                0 r STARTED 1  4.8kb 10.150.55.88  ES-Data-10.150.55.88
index_name           0 r STARTED 1  4.1kb 10.150.55.95  ES-master-10.150.55.95
index_name           0 p STARTED 1  4.1kb 10.150.55.89  ES-Data-10.150.55.89
index-name           0 p STARTED 3 14.1kb 10.150.30.241 ES-Data-10.150.30.241
index-name           0 r STARTED 3 14.1kb 10.150.55.88  ES-Data-10.150.55.88
.kibana_1            0 p STARTED 4 48.7kb 10.150.55.95  ES-master-10.150.55.95
.kibana_1            0 r STARTED 4 48.5kb 10.150.55.89  ES-Data-10.150.55.89
migu                 0 p STARTED 1    4kb 10.150.55.89  ES-Data-10.150.55.89
migu                 0 r STARTED 1    4kb 10.150.55.88  ES-Data-10.150.55.88

_search搜索匹配文档内容

语法如下:

curl -XGET 'http://10.150.30.246:9201/_search?q="Linux"&pretty'

    q="xx":q等于我们要搜索的字段内容,此内容一定要为文档内的value值,否则搜索会为空
    &pretty:为特定语法

在整个集群的索引中搜索有Linux相关的文档内容

curl -XGET 'http://10.150.30.246:9201/_search?q="Linux"&pretty'

在指定索引中搜索有java相关的文档内容

#/apple/为索引名称,在apple索引中搜索有关java的文档内容
curl -XGET 'http://10.150.30.246:9201/apple/_search?q="java"&pretty' 

#在mac索引中搜索有java字段的文档
curl -XGET 'http://10.150.30.246:9201/mac/_search?q="java"&pretty'  

_cluster查询指令

#查询集群的健康状况
curl -XGET 'http://10.150.30.246:9201/_cluster/health?pretty=true'   

#查看集群的状态
curl -XGET 'http://10.150.30.246:9201/_cluster/stats?pretty=true'

示例健康状况

curl -XGET 'http://10.150.30.246:9201/_cluster/health?pretty=true'
{
  "cluster_name" : "ES-Cluster",
  "status" : "green",                   #集群状态green,无分片故障
  "timed_out" : false,
  "number_of_nodes" : 7,                #集群节点数量
  "number_of_data_nodes" : 4,           #集群数据节点数量
  "active_primary_shards" : 11,         #主分片数量
  "active_shards" : 22,                 #所有分片数量
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "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" : 100.0
}

高级查询

指定搜索多个索引内的java字段
在索引名称字段以英文逗号隔开索引名称,可以指定多个

curl -XGET 'http://10.150.30.246:9201/mac,apple/_search?q="java"&pretty'

指定搜索以m开头和a开头的索引并在其索引中搜索java字段

curl -XGET 'http://10.150.30.246:9201/m*,a*/_search?q="java"&pretty'

指定索引以及指定文档类型在搜索指定的java字段

curl -XGET 'http://10.150.30.246:9201/mac/macbookpro/_search?q="java"&pretty'

指定索引以及指定文档类型和指定文档ID查找单个文档

curl -XGET 'http://10.150.30.246:9201/mac/macbookpro/60?pretty'
{
  "_index" : "mac",                     #文档索引名称
  "_type" : "macbookpro",               #文档类型名称
  "_id" : "60",                         #文档ID
  "_version" : 1,                       #文档版本号,如果对此文档进行了修改,此版本号会迭代加1
  "_seq_no" : 0,
  "_primary_term" : 1,                  
  "found" : true,                       #找到为真
  "_source" : {                         #文档的原始内容
    "name" : "macbookpro",
    "age" : 24,
    "job" : "java",
    "company" : "apple"
  }
}

人已赞赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索