CURL的操作 
   curl是利用URL语法在命令行方式下工作的开源文件传输工具,使用curl可以简单实现常见的get/post请求。简单的认为是可以在命令行下面访问url的一个工具。在centos的默认库里面是有curl工具的,如果没有请yum安装即可。 
    curl 
    -X 指定http的请求方法 有HEAD GET POST PUT DELETE 
    -d 指定要传输的数据 
    -H 指定http请求头信息 
    浏览ES服务器 
        curl -XGET http://master:9200  <=> 在浏览器中访问 
    创建索引库 
        curl -XPUT http://master:9200/bigdata_p 
        这样就在es中创建了一个索引库bigdata_p 
     
    POST和PUT都可以用于创建,二者之间的区别: 
        PUT是幂等方法,POST不是。所以PUT用户更新,POST用于新增比较合适。 
        ES创建索引库和索引时的注意点 
              1)索引库名称必须要全部小写,不能以下划线开头,也不能包含逗号 
              2)如果没有明确指定索引数据的ID,那么es会自动生成一个随机的ID,需要使用POST参数 
            curl -XPOST http://localhost:9200/bigdata/product/ -d '{"author" : "Doug Cutting"}' 
    往索引库中新增数据 
        在具体的type里面,添加相关的document 
        curl -XPUT http://master:9200/bigdata_p/product/ -d '{"name":"hadoop", "author": "Doug Cutting", "c_version": "2.7.3"}' 
    查询某一个索引库中的数据 
            查询整个索引库:curl -XGET http://master:9200/bigdata_p/_search?pretty 
                    在url后面加上一个pretty则会对返回结果进行格式化, 
            查询某一个type:curl -XGET http://master:9200/bigdata_p/product/_search?pretty 
            查询具体的一条记录:curl -XGET http://master:9200/bigdata_p/product/1?pretty 
        查询一条索引文档中的具体的字段:curl -XGET http://master:9200/bigdata_p/product/1?_source=name&pretty 
            如果要查询多个字段,使用","进行隔开。eg. 
            curl -XGET http://master:9200/bigdata_p/product/1?_source=name,author&pretty 
        获取source所有数据 
            curl -XGET http://master:9200/bigdata_p/product/1?_source&pretty 
        根据条件进行查询 
            curl -XGET http://master:9200/bigdata_p/product/_search?q=name:hbase,hive&pretty 
    -------------------         
    ES更新 
       ES可以使用PUT或者POST对文档进行更新,如果指定ID的文档已经存在,则执行更新操作 
    注意:执行更新操作的时候,ES首先将旧的文档标记为删除状态,然后添加新的文档,旧的文 
    档不会立即消失,但是你也无法访问,ES会继续添加更多数据的时候在后台清理已经标记为删 
    除状态的文档。 
    局部更新 
       可以添加新字段或者更新已经存在字段(必须使用POST) 
        curl -XPOST http://master:9200/bigdata_p/product/2/_update -d '{"doc":{"c_version": "2.0.0", "publish_time": "2017-03-23"}}' 
        查询结果: 
        "hits" : [ { 
              "_index" : "bigdata_p", 
              "_type" : "product", 
              "_id" : "2", 
              "_score" : 0.30685282, 
              "_source" : { 
                "name" : "hbase", 
                "author" : "apache", 
                "c_version" : "2.0.0", 
                "publish_time" : "2017-03-23" 
              } 
            } ] 
    普通删除,根据主键删除 
       curl -XDELETE http://master:9200/bigdata_p/product/3/ 
    说明:如果文档存在,es属性found:true,successful:1,_version属性的值+1。 
       如果文档不存在,es属性found为false,但是版本值version依然会+1,这个就是内部 
    管理的一部分,有点像svn版本号,它保证了我们在多个节点间的不同操作的顺序被正确标记了。 
       注意:一个文档被删除之后,不会立即生效,他只是被标记为已删除。ES将会在你之后添加 
    更多索引的时候才会在后台进行删除。 
     
    批量操作-bulk 
       Bulk api可以帮助我们同时执行多个请求 
    格式: 
       action:[index|create|update|delete] 
       metadata:_index,_type,_id 
       request body:_source(删除操作不需要) 
       {action:{metadata}}\n 
       {request body}\n 
       {action:{metadata}}\n 
       {request body}\n 
      create和index的区别 
        如果数据存在,使用create操作失败,会提示文档已经存在,使用index则可以成功执行。 
         
        使用文件的方式 
            curl -XPOST/PUT http://master:9200/index/type/_bulk --data-binary @path 
            比如     
            curl -XPOST 'http://master:9200/bank/account/_bulk --data-binary @/home/uplooking/Documents/accounts.json 
        查询结果: 
            http://master:9200/bank/_search?pretty 
            { 
              "took" : 10,    ---->默认取出其中前10条记录 
              "timed_out" : false, 
              "_shards" : { 
                "total" : 5, 
                "successful" : 5, 
                "failed" : 0 
              }, 
              "hits" : { 
                "total" : 1000, ----->总共有1000条记录 
                "max_score" : 1.0, 
    可以查看一下各个索引库信息 

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

简介:

Curl工具是一种可以在命令行访问url的工具,支持get和post请求方式。-X指定http请求的方法,-d指定要传输的数据。

创建索引:

Put创建

curl -XPUThttp://localhost:9200/shb01/student/1-d'{"name":"jack","age":30,"info":"Ilove you"}'

{"_index":"shb01","_type":"student","_id":"1","_version":1,"created":true}Youhave new mail in /var/spool/mail/root

执行put后有返回值

_index索引名称

_type类型名

_version版本号

created:true表示是新创建的。

上面的命令每执行一次version就会加1,-XPUT必须制定id。

Post创建索引

curl -XPOSThttp://localhost:9200/shb01/student -d'{"name":"tom","age":21,"info":"tom"}'

{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp","_version":1,"created":true}

使用post创建索引数据,-XPOST可以指定id,此时如果存在相同数据则是修改,不指定id的话会随机生成id,且每次执行都会生成新数据。

如果需要每次执行都产生新的数据可以使用post命令且不指定id。

如果使用put命令则需要增加create,命令格式如下

curl -XPUT http://localhost:9200/shb01/student/1/_create-d '{"name":"jackk","age":31}'

curl -XPUThttp://localhost:9200/shb01/student/1?op_type=create -d'{"name":"jackk","age":31}'

以上两条命令执行时如果存在id相同的数据则会给出error信息

{"error":"DocumentAlreadyExistsException[[shb01][2][student][1]: document already exists]","status":409}

Post与put的区别

Put是等幂操作,即无论执行多少次结果都一样,例如DEL无论删除多少次索引库中的结果都一样,put只要指定了id且数据不变无论执行多少次索引库中的数据都不变,只有version会变化。

Post每次执行都会产生新数据。

查询

1:查询索引库shb01中的类型student

浏览器:http://192.168.79.131:9200/shb01/student/_search?pretty

Curl:curl -XGET http://192.168.79.131:9200/shb01/student/_search?pretty

其显示结果与浏览器一样。

2:查询文档1中的数据

http://192.168.79.131:9200/shb01/student/1?pretty

http://192.168.79.131:9200/shb01/student/1?_source&pretty

两者结果一样

http://192.168.79.131:9200/shb01/student/1?_source=name&pretty

可以通过source指定显示那些字段

3:查询所有索引库信息

浏览器:http://192.168.79.131:9200/_search?pretty

将索引库shb01和shb02的数据都显示出来。

4:根据条件查询

浏览器:http://192.168.79.131:9200/shb01/student/_search?q=name:zs&pretty

查询name为zs的数据

5:查询集群状态

Curl –XGET http://192.168.79.131:9200/_cluster/health?pretty

http://192.168.79.131:9200/_cluster/health?pretty

6:多索引,多类型查询,分页查询,超时

Curl:curl -XGET http://192.168.79.131:9200/shb01,shb02/stu,tea/_search?pretty

curl -XGET http://192.168.79.131:9200/_all/stu,tea/_search?pretty

浏览器去掉curl –XGET即可

分页

curl -XGET http://192.168.79.131:9200/shb01/stu/_search?size=2&from=0

超时

curl -XPOST http://192.168.79.131:9200/_search?_timeout=100

更新

Es

部分更新

如果文档1的字段很多而我们只需要更新其中的一两个字段则可以通过doc指定需要修改的字段其他字段则不必修改。

crul –XPUT

http:192.168.79.131:9200/shb01/student/1/_update?version=1

–d ‘{“doc”:{“name”:”updatename”}’

全量更新:

更新文档1中所有字段的内容。

curl -XPUThttp://192.168.79.131:9200/shb01/student/1 -d'{"name":"will","age":100,"info":"newonw"}'

更新流程

es会将旧的文档进行标记然后再添加新数据,旧的文档也不能再被访问,在后续添加数据时es会清理已经为删除状态的数据。

删除

删除文档并不会立即生效,只会将其标记为已删除,当后续添加更多索引时才会在后台删除。

curl -XDELETE http://192.168.79.131:9200/shb01/student/AVad05EExskBS1Rg2tdq

根据id删除,删除成功返回found:true,找不到found:false,版本号都会加1。

根据条件删除,删除索引shb01,shb02种类型student,tea中所有name为zs的文档

curl -XDELETEhttp://192.168.79.131:9200/shb01,shb02/student,tea/_query?q=name:zs

删除所有的索引库中名称为tom的文档

curl -XDELETE http://192.168.79.131:9200/_all/_query?q=name:tom

批处理

将一批数据加载入内存然后和es交互一次,一次性同时处理多个请求和redis的管道类似。

格式:

Action:index/create/delete/update

Metadata:_index/_type/_id

Create:如果数据存在则报错;index:如果数据存在仍会执行成功。

步骤:

1:在liunx下创建一个文件request1,vi request1

{"index":{"_index":"shb01","_type":"student","_id":"1"}}

{"name":"st01","age":"10","info":"st01"}

{"create":{"_index":"shb100","_type":"student","_id":"2"}}

{"name":"tea01","age":"10","info":"tea01"}

{"delete":{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp"}

{"update":{"_index":"shb02","_type":"tea","_id":"1"}}

{"doc":{"name":"zszszszs"}}

文件中

index表示操作类型

_index指定索引库,_type指定类型,_id指定操作文档

2:执行批处理命令,关键字_bulk

curl  -XPUThttp://192.168.79.131:9200/_bulk --data-binary @/usr/local/request1

注意:--data-binary@之间有空格隔开,我在实验中没有空格一直提示操作参数不对。

3:返回值

{

"took":957,"errors":false,"items":[

{"index":{"_index":"shb01","_type":"student","_id":"1","_version":12,"status":200}},

{"create":{"_index":"shb100","_type":"student","_id":"2","_version":1,"status":201}},

{"delete":{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp","_version":2,"status":200,"found":true}},

{"update":{"_index":"shb02","_type":"tea","_id":"1","_version":2,"status":200}}

]

返回信息中errors表示批处理有没有错误,注意version和status,其中shb100为新创建的索引库

下面是我第二次执行request1文件的返回信息,errors为true,表示批处理中有操作执行失败,可以看到create因为库中已有id相同的文档所以报错。但是虽然存在错误操作但其他的操作依然成功执行。这点和redis中的事务操作类似。

{

"took":22,"errors":true,"items":[

{"index":{"_index":"shb01","_type":"student","_id":"1","_version":13,"status":200}},

{"create":{"_index":"shb100","_type":"student","_id":"2","status":409,"error":"DocumentAlreadyExistsException[[shb100][3][student][2]: document already exists]"}},

{"delete":{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp","_version":1,"status":404,"found":false}},

{"update":{"_index":"shb02","_type":"tea","_id":"1","_version":3,"status":200}}

]

}

4:在命令中指定索引库和类型

创建一个文件,文件中没有配置索引库和类型

{"index":{"_id":"1"}}

{"name":"st1_1","age":"10","info":"st1_1"}

{"create":{"_id":"200"}}

{"name":"st200","age":"10","info":"st200"}

执行如下命令,在命令中指定了索引库和类型

curl  -XPUThttp://192.168.79.131:9200/shb01/student/_bulk --data-binary@/usr/local/request2

返回信息

{

"took":24,"errors":false,"items":[

{"index":{"_index":"shb01","_type":"student","_id":"1","_version":17,"status":200}},

{"create":{"_index":"shb01","_type":"student","_id":"200","_version":1,"status":201}}

]

}

5:也可以使用-XPOST替换-XPUT

ElasticSearch之CURL操作的更多相关文章

  1. ElasticSearch之CURL操作(有空再去整理)

    https://www.cnblogs.com/jing1617/p/8060421.html ElasticSearch之CURL操作 CURL的操作    curl是利用URL语法在命令行方式下工 ...

  2. elasticsearch(5) curl 操作elasticsearch

    创建索引之前可以对索引做初始化操作, 比如指定shards数量以及replicas的数量.     library为索引的名称 CURL -XPUT 'http://192.168.1.10:9200 ...

  3. elasticsearch的索引操作和文档操作总结

    参考文档:https://es.xiaoleilu.com/010_Intro/00_README.html 一.索引操作 1.查看当前节点的所有的index 查看当前节点的所有的index [roo ...

  4. Elasticsearch之CURL命令的DELETE

    也可以看我写的下面的博客 Elasticsearch之curl删除 Elasticsearch之curl删除索引库 删除,某一条数据,如下 [hadoop@master elasticsearch-] ...

  5. Elasticsearch之CURL命令的UPDATE

    对于,Elasticsearch之CURL命令的UPDATE包括局部更新和全部更新.可以去看我写的另一篇博客. Elasticsearch之更新(全部更新和局部更新) 总结: ES全部更新,使用PUT ...

  6. Elasticsearch之curl删除

    扩展下, Elasticsearch之curl删除索引库 [hadoop@djt002 elasticsearch-2.4.3]$ curl -XDELETE 'http://192.168.80.2 ...

  7. 使用curl操作openstack swift

    openstack官网有专门的开发者文档介绍如何使用curl操作swift(http://docs.openstack.org/api/openstack-object-storage/1.0/con ...

  8. ElasticSearch+Kibana 索引操作

    ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...

  9. 使用curl操作InfluxDB

    这里列举几个简单的示例代码,更多信息请参考InfluxDB官方文档: https://docs.influxdata.com/influxdb/v1.1/ 环境: CentOS6.5_x64Influ ...

随机推荐

  1. C# 后台模块 Word 模板操作

    public static string CreateWord() { //********************************************** //来自博客http://bl ...

  2. python 字典添加元素

    d = {:, :} print(d) d.update({:}) print(d)

  3. 使用git bush 生成github SSH公钥

    1 如果没有安装ssh,那么使用下面的指令 sudo apt-get install ssh 2 检查SSH公钥 cd ~/.ssh 看看存不存在.ssh,如果存在的话,掠过下一步:不存在的请看下一步 ...

  4. Java IO流-Properties

    2017-11-05 21:37:50 Properties Properties:Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其 ...

  5. K-Means & Sequential Leader Clustering

    2017-12-31 19:08:37 k-平均算法源于信号处理中的一种向量量化方法,现在则更多地作为一种聚类分析方法流行于数据挖掘领域.k-means的目的是:把样本划分到k个聚类中,使得每个点都属 ...

  6. 快速切题 sgu134.Centroid 树形dp

    134. Centroid time limit per test: 0.25 sec. memory limit per test: 4096 KB You are given an undirec ...

  7. L187 DKK2

    Why can millions of hairs grow from our heads, and yet our palms手掌 and the soles of our feet are as ...

  8. 使用file_get_contents提交http post

    以前使用curl获取需要登陆内容的文章,但其实,自5.0开始,使用file_get_contents就可以完成.(前提是开启了allow_url_fopen),下面以一个简单的例子说明一下:1.先看一 ...

  9. react中findDOMNode

    在使用react过程中,大家有时会那么这里的findDomNode是做什么的呢? import { findDomNode } from 'react-dom'; 简单来说是用来得到实际Dom的,因为 ...

  10. 新书《Cocos2dx 3.x 3D图形学渲染技术讲解》问世

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...