ElasticSearch之CURL操作


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM