1.1.3 探索你的集群(Exploring Your Cluster)

探索你的集群(Exploring Your Cluster)  原文链接  翻译:AbnerGong

THE REST API
既然我发已经启动了我们的节点,下一步是理解如何与它交流。幸运的是,Elasticsearch提供了一个非常全面并且强大的REST API使得你能用它与你的集群交互(interact)。使用API可以做的事情包括:
1. 检查你的集群,节点,和索引健康(health),状态以及统计结果(statistics)
2. 管理你的集群,节点和索引数据以及元数据
3. 执行CRUD(Create,Read,Update,and Delete)并且针对你的索引搜索操作(search operations)
4. 执行高级搜索操作诸如paging, sorting, filtering, scripting, aggregations以及其他。



【注意】关于Rest API的访问工具  作者:AbnerGong

接下来的测试全部是通过Rest API与集群进行交互的,而访问Rest API用什么工具呢?下面教程的样例使用的都是Linux中的curl
如果你和我一样使用Windows,那么推荐使用Sense编辑器,这是官方推荐的编辑器,虽然官方文档上比较麻烦而且装完我还找不到,但是幸运的是Chrome浏览器有Sense插件(感谢huangfox的博文),能够很容易地安装,下文中官方文档使用curl的地方,我会补充sense的语句,便于使用sense的人学习。

如果你要使用各种编程语言来访问,那么请参考Elasticsearch客户端,它支持Java, JavaScript, Groovy, .NET, PHP, Perl, Python, Ruby。



集群健康(Cluster Health) 原文链接  翻译:AbnerGong

我们开始基本的健康检查,我们可以用它看到我们的集群正在如何做事情。我们将用要curl做这个,但是你也可以用任何工具,允许HTTP/REST calls的都可以。我们假设我们仍然在启动Elasticsearch相同的节点上,并打开另一个命令行窗口
为了检查集群健康,我们将要使用_cat API。记得之前我们的节点HTTP端点在9200可用:

#如果你使用curl请使用第一句:
curl 'localhost:9200/_cat/health?v'  #使用curl

#如果你使用sense请使用下一句:地址localhost:9200写在Server中故省略,后文不再赘述
GET _cat/health?v #使用sense

返回结果是:

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign
1394735289 14:28:09  elasticsearch green           1         1      0   0    0    0        0

我们能看到我们的集群名称为”elasticsearch”是绿色状态开启
无论何时我们请求集群健康,我们可能得到绿、黄、红。绿意味着一切正常(集群具有完全功能fully functional),黄色意味着所有数据可用但是一些碎片还没分配(集群具有完全功能),而红色意味着一些数据不可用因为一些原因(for whatever reason)。注意到即使集群是红色,它仍然是有部分功能的(即它将会继续提供搜索请求服务在某些碎片上)但是你将会可能需要修复它ASAP因为你有丢失的数据。

继续看上述反馈信息(respone),我们能看到总共1个节点并且我们有0个碎片因为我们还没有数据在里面。注意到因为我们正在使用默认集群名而且因为Elasticsearch使用单播网络发现(unicast network),默认寻找同一台电脑中的其他节点,有可能你启动其他节点在你的电脑上,然后把它们添加到一个单独的集群中。在这种情形下(in this scenario),你可能看到多于1个节点在上述反馈中。

我们也能得到一个在我们的集群中的节点列表用如下方法:

curl 'localhost:9200/_cat/nodes?v'  #使用curl
或
GET _cat/nodes?v #使用Sense

以及这个反馈信息:

host         ip        heap.percent ram.percent load node.role master name
mwubuntu1    127.0.1.1            8           4 0.00 d         *      New Goblin

这里我们能看到我们的一个节点叫做”New Goblin”,这是当前在我们集群中的单个节点。



列出所有索引(List All Indices)  原文链接  翻译:AbnerGong

现在来一览我们的索引:

curl 'localhost:9200/_cat/indices?v' #使用curl
或
GET _cat/indices?v  #使用Sense

反馈信息是:

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

简单地意味着我们在集群中还没有节点



创建一个索引(Create an Index) 原文链接  翻译:AbnerGong

现在让我们放一些东西到我们的customer索引中。记得之前为了索引一个文档我们必须告诉Elasticsearch在这个索引中它应该是什么类型的。

让我们索引一个简单的customer文档到customer索引中,”外部”类型,其ID为1:
我们的JSON文档:{“name”:”John Doe}

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "John Doe"
}'    #使用curl,这里-d是post data的意思
或
POST customer/external/1?pretty
{
  "name": "John Doe"
}   #使用sense,用PUT和POST貌似都行尚不清楚

然后得到返回结果(response)为:

{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "created" : true
}

根据如上结果,我们能看到一个想的顾客文档成功的在索引和外部类型中创建了。文档也有内部id为1是我们索引的时候指定的。

我们要注意到Elasticsearch不需要你去明确地创建一个索引在你能索引文档到里面以前。在前面的杨立忠,Elasticsearch将会自动地创建customer索引如果它之前(beforehand)从来没存在过。

我们现在取回我们刚刚索引的文档:

curl -XGET 'localhost:9200/customer/external/1?pretty' #使用curl
或
GET customer/external/1?pretty #使用Sense

返回结果为:

{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "found" : true, 
  "_source" : { 
    "name": "John Doe" 
  }
}

这里没有什么特别的地方(Nothing out of the ordinary here),只有一个域found表示我们找到一个请求ID为1的文档和另一个域_source,返回了我们在上一步索引过的全部JSON文档



索引并查询一个文档(Index and Query a Document)  原文链接  翻译:AbnerGong

现在让我们放一些东西到我们的customer索引中。记得之前为了索引一个文档我们必须告诉Elasticsearch在这个索引中它应该是什么类型的。

让我们索引一个简单的customer文档到customer索引中,”外部”类型,其ID为1:
我们的JSON文档:{“name”:”John Doe}

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "John Doe"
}' #使用curl
或
POST customer/external/1?pretty
{
  "name": "John Doe"
} #使用Sense

然后得到返回结果(response)为:

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "John Doe"
}'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "created" : true
}

根据如上结果,我们能看到一个想的顾客文档成功的在索引和外部类型中创建了。文档也有内部id为1是我们索引的时候指定的。

我们要注意到Elasticsearch不需要你去明确地创建一个索引在你能索引文档到里面以前。在前面的杨立忠,Elasticsearch将会自动地创建customer索引如果它之前(beforehand)从来没存在过。

我们现在取回我们刚刚索引的文档:

curl -XGET 'localhost:9200/customer/external/1?pretty'

返回结果为:

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

这里没有什么特别的地方(Nothing out of the ordinary here),只有一个域found表示我们找到一个请求ID为1的文档和另一个域_source,返回了我们在上一步索引过的全部JSON文档



删除一个索引(Delete an Index)  原文链接  翻译:AbnerGong

现在我们删除刚刚创建的索引然后再列出所有索引:

curl -XDELETE 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'
或
DELETE customer?pretty #用Sense
GET _cat/indices?v  #用Sense

返回结果为:

curl -XDELETE 'localhost:9200/customer?pretty'
{
  "acknowledged" : true
}
curl 'localhost:9200/_cat/indices?v'
health index pri rep docs.count docs.deleted store.size pri.store.size

意味着索引已经被成功删除,我们现在回到集群什么都没有的初始状态
在我们继续(move on)前,让我们再细看(take a closer look)一下我们迄今为止已经学过的所有API命令:

curl -XPUT 'localhost:9200/customer'
curl -XPUT 'localhost:9200/customer/external/1' -d '
{
  "name": "John Doe"
}'
curl 'localhost:9200/customer/external/1'
curl -XDELETE 'localhost:9200/customer'

或Sense命令

GET _cat/indices?v #获取所有索引信息
PUT customer #创建索引
PUT customer/external/1?pretty #插入数据到某索引/某类/某序号
{
  "name": "John Doe"
}
GET customer/external/1 #取回数据

如果我们仔细地学习了如上命令,我们就能看到我们如何访问Elasticsearch中的数据的模式。那种模式可以总结为如下形式:

curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>

这个REST访问模式是普遍(pervasive throughout)适用于所有API命令的,如果你能记住它,掌握Elasticsearch就会有一个好的开始(have a good head start).

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值