Bulk API实现批量操作
Bulk 批量操作
(1) 比如,我這里,在$ES_HOME里,新建一文件,命名為requests。(這里為什么命名為request,去看官網就是)在Linux里,有無后綴沒區別。
| 1 2 3 4 5 6 7 8 9 10 | [root@backup01 elasticsearch-6.8.6]#?vi requests ?? {"index":{"_index":"my_store","_type":"my_index","_id":"11"}} {"price":10,"productID":"1111"} {"index":{"_index":"my_store","_type":"my_index","_id":"12"}} {"price":20,"productID":"1112"} {"index":{"_index":"my_store","_type":"my_index","_id":"13"}} {"price":30,"productID":"1113"} {"index":{"_index":"my_store","_type":"my_index","_id":"14"}} {"price":40,"productID":"1114"} |
(2)執行命令
| 1 2 3 4 5 | curl? -PUT??'192.168.80.200:9200/_bulk'???--data-binary @requests; ? 或 ? ? curl? -XPOST??'192.168.80.200:9200/_bulk'???--data-binary @requests; |
?
?
bulk的格式: {action:{metadata}}\n {requstbody}\n (請求體)action:(行為),包含create(文檔不存在時創建)、update(更新文檔)、index(創建新文檔或替換已用文檔)、delete(刪除一個文檔)。 create和index的區別:如果數據存在,使用create操作失敗,會提示文檔已存在,使用index則可以成功執行。 metadata:(行為操作的具體索引信息),需要指明數據的_index、_type、_id。 示例: {"delete":{"_index":"lib","_type":"user","_id":"1"}}批量添加
POST /lib2/books/_bulk {"index":{"_id":1}} \\行為:索引信息 {"title":"Java","price","55"} \\請求體 {"index":{"_id":2}} {"title":"Html5","price","45"} {"index":{"_id":3}} {"title":"Php","price","35"}` {"index":{"_id":4}} {"title":"Python","price","50"}//返回結果 {"took": 60,"error": false //請求是否出錯,返回false、具體的錯誤"items": [//操作過的文檔的具體信息{"index":{"_index": "lib","_type": "user","_id": "1","_version": 1,"result": "created", //返回請求結果"_shards": {"total": 1,"successful": 1,"failed": 0},"_seq_no": 0,"_primary_trem": 1"status": 200}},...] }批量刪除?
刪除的批量操作不需要請求體
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的Bulk API实现批量操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: error while loading
- 下一篇: python 打开文件,读取文件内容