19.ELK实时日志分析平台之Elasticsearch REST API简介

载*请注明原始出处:http://blog.csdn.net/a464057216/article/details/50909215
2017.03.20更新:适用于elasticsearch-5.2.2

后续此博客不再更新,欢迎大家搜索关注微信公众号“测开之美”,测试开发工程师技术修炼小站,持续学习持续进步。
在这里插入图片描述

Elasticsearch提供了一系列RESTful的API,覆盖了如下功能:

  • 检查集群、节点、索引的健康度、状态和统计
  • 管理集群、节点、索引的数据及元数据
  • 对索引进行CRUD操作及查询操作
  • 执行其他高级操作如分页、排序、过滤等。

集群信息

使用_cat API可以查询集群健康,比如:

$ curl 'localhost:9200/_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 
1458099890 11:44:50  MegviiQA green           1         1      0   0    0    0        0             0                  -                100.0%

使用不提供v参数可以查询简要信息:

$ curl 'localhost:9200/_cat/health'
1458099895 11:44:55 MegviiQA green 1 1 0 0 0 0 0 0 - 100.0%

关于各个字段的解释,可以使用help参数,比如:

$ curl 'localhost:9200/_cat/health?help'
epoch                 | t,time                                   | seconds since 1970-01-01 00:00:00  
timestamp             | ts,hms,hhmmss                            | time in HH:MM:SS                   
cluster               | cl                                       | cluster name                       
status                | st                                       | health status                      
node.total            | nt,nodeTotal                             | total number of nodes              
node.data             | nd,nodeData                              | number of nodes that can store data
shards                | t,sh,shards.total,shardsTotal            | total number of shards             
pri                   | p,shards.primary,shardsPrimary           | number of primary shards           
relo                  | r,shards.relocating,shardsRelocating     | number of relocating nodes         
init                  | i,shards.initializing,shardsInitializing | number of initializing nodes       
unassign              | u,shards.unassigned,shardsUnassigned     | number of unassigned shards        
pending_tasks         | pt,pendingTasks                          | number of pending tasks            
max_task_wait_time    | mtwt,maxTaskWaitTime                     | wait time of longest task pending  
active_shards_percent | asp,activeShardsPercent                  | active number of shards in percent

集群状态的颜色分红、黄、绿三种,红色需要修复数据,黄色表示某些replica尚未被分配到其他节点但不影响整个集群的功能,绿色表明整个集群的功能正常。

查询集群的节点信息可以使用如下接口:

$ curl 'localhost:9200/_cat/nodes?v'
host      ip        heap.percent ram.percent load node.role master name                   
127.0.0.1 127.0.0.1            4          88 0.04 d         *      QA-103.6-elasticsearch

操作索引

罗列所有索引信息的接口如下:

$ curl 'localhost:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size

上面的输出,说明我们目前还没有创建过任何索引。

可以使用PUT或POST方法创建索引:

$ curl -XPUT 'localhost:9200/customer'
{"acknowledged":true}
$ curl -XPOST 'localhost:9200/seller'
{"acknowledged":true}
$ curl 'localhost:9200/_cat/indices?v'
health status index    pri rep docs.count docs.deleted store.size pri.store.size 
yellow open   customer   5   1          0            0       650b           650b 
yellow open   seller     5   1          0            0       650b           650b

从上述输出中可知,customer这个索引,有5个primary shards,有1个replica(默认的),其中不包含任何内容(document的数量是0)。新建索引的状态是yellow,因为默认为索引准备了一个备份但是没有另一个节点去分配,如果集群中加入了另一个节点,replica分配完成后,索引的状态就会使绿色的了。

向索引中添加数据的方法如下:

$ curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
> {
> "name":"lmz"
> }'

{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}

注意:索引一个document的时候不需要先创建索引,可以直接添加数据。

查询一条数据的方法如下:

$ curl -XGET 'localhost:9200/customer/external/1?pretty'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "lmz"
  }
}

删除索引可以使用如下接口:

$ curl -XDELETE 'localhost:9200/customer?pretty'
{
  "acknowledged" : true
}

查询索引可以看到索引成功删除:

$ curl -XGET 'localhost:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size

更新数据

为一个具体的(index+type+id)document赋值之后,如果使用相同的index+type+id对其进行PUT操作,会更新该document的值(version的值会加一)。

添加一个document的时候,也可以不指定id,这时Elasticsearch会随机生成一个id,这种情况必须使用POST方法,而且生成的元素的id不一定是数字形式,比如:

$ curl -XPOST 'localhost:9200/customer/external?pretty' -d '
> {
>   "name": "Jane Doe"
> }'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "AVN-lf9kG-SL-FxTq4sA",
  "_version" : 1,
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}

更新也可以使用专门的update接口,注意Elasticsearch的更新并不是一个原地操作,它只是删除原来的document然后建立一个新的而已。

$ curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc":{"name":"mars loo"}
}'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}

也可以在更新的过程中添加新的字段:

$ curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc":{"name": "mars loo", "age": 25}
}'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 3,
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}

更新操作也可以使用脚本:

~$ curl -XPOST 'localhost:8200/customer/external/1/_update?pretty' -d '
{
"script":"ctx._source.doc.age += 5" 
}'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}

其中ctx._source表示当前待更新的document。

删除一个document可以使用如下接口:

$ curl -XDELETE 'localhost:9200/customer/external/1?pretty'
{
  "found" : true,
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 4,
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}

查询这个document确认found字段为false:

$ curl -XGET 'localhost:9200/customer/external/1?pretty'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "found" : false
}

批量操作

Elasticsearch提供了进行批量操作的_bulk API,能够方便的进行批量处理,例如一次性索引两个document:

$ curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d'
> {"index":{"_id":1}}
> {"name":"mars"}
> {"index":{"_id":2}}
> {"name":"suson"}
> '
{
  "took" : 134,
  "errors" : false,
  "items" : [ {
    "index" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "1",
      "_version" : 1,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "status" : 201
    }
  }, {
    "index" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "2",
      "_version" : 1,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "status" : 201
    }
  } ]
}

在一次操作中更新_id为1的document,删除_id为2的document:

$ curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
> {"update":{"_id":1}}
> {"doc":{"name":"mars loo", "age":25}}
> {"delete":{"_id":2}}
> '
{
  "took" : 208,
  "errors" : false,
  "items" : [ {
    "update" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "1",
      "_version" : 2,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "status" : 200
    }
  }, {
    "delete" : {
      "_index" : "customer",
      "_type" : "external",
      "_id" : "2",
      "_version" : 2,
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "status" : 200,
      "found" : true
    }
  } ]
}

批量操作会按照入参顺序依次执行每个动作,即使某个动作失败了,也会继续执行剩下的动作。在最后的返回结果中,Elasticsearch会按照入参的顺序详细地给出每个动作的执行情况。

如果觉得我的文章对您有帮助,欢迎关注我(CSDN:Mars Loo的博客)或者为这篇文章点赞,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值