Elasticsearch之批量操作bulk
?
?
1、bulk相當于數據庫里的bash操作。
2、引入批量操作bulk,提高工作效率,你想啊,一批一批添加與一條一條添加,誰快?
3、bulk API可以幫助我們同時執行多個請求
4、bulk的格式:
action:index/create/update/delete
metadata:_index,_type,_id
request body:_source?(刪除操作不需要加request body)
?????????????????? { action: { metadata }}
?????????????????? { request body??????? }
5、bulk里為什么不支持get呢?
答:批量操作,里面放get操作,沒啥用!所以,官方也不支持。
6、create 和index的區別
如果數據存在,使用create操作失敗,會提示文檔已經存在,使用index則可以成功執行。
?7、bulk一次最大處理多少數據量?
bulk會把將要處理的數據載入內存中,所以數據量是有限制的,最佳的數據量不是一個確定的數值,它取決于你的硬件,你的文檔大小以及復雜性,你的索引以及搜索的負載。
一般建議是1000-5000個文檔,如果你的文檔很大,可以適當減少隊列,大小建議是5-15MB,默認不能超過100M,可以在es的配置文件(即$ES_HOME下的config下的elasticsearch.yml)中。
?
?
?
elasticsearch-.yml(中文配置詳解)
來修改這個值http.max_content_length: 100mb【不建議修改,太大的話bulk也會慢】,
https://www.elastic.co/guide/en/elasticsearch/reference/2.4/modules-http.html
?
?
?
?
?
?
?
?
?
?
?
?
?
批量操作bulk例子
(1) 比如,我這里,在$ES_HOME里,新建一文件,命名為request。(這里為什么命名為request,去看官網就是)在Linux里,有無后綴沒區別。
[hadoop@djt002 elasticsearch-2.4.3]$ pwd
/usr/local/elasticsearch/elasticsearch-2.4.3
[hadoop@djt002 elasticsearch-2.4.3]$ ll
total 56
drwxrwxr-x. 2 hadoop hadoop 4096 Feb 20 22:54 bin
drwxrwxr-x. 3 hadoop hadoop 4096 Feb 21 01:28 config
drwxrwxr-x. 3 hadoop hadoop 4096 Feb 20 22:59 data
drwxrwxr-x. 2 hadoop hadoop 4096 Feb 20 22:54 lib
-rw-rw-r--. 1 hadoop hadoop 11358 Aug 24 00:46 LICENSE.txt
drwxrwxr-x. 2 hadoop hadoop 4096 Feb 21 00:33 logs
drwxrwxr-x. 5 hadoop hadoop 4096 Dec 8 00:41 modules
-rw-rw-r--. 1 hadoop hadoop 150 Aug 24 00:46 NOTICE.txt
drwxrwxr-x. 2 hadoop hadoop 4096 Feb 20 22:59 plugins
-rw-rw-r--. 1 hadoop hadoop 8700 Aug 24 00:46 README.textile
[hadoop@djt002 elasticsearch-2.4.3]$ vim request
[hadoop@djt002 elasticsearch-2.4.3]$ more request
{"index":{"_index":"zhouls","_type":"emp","_id":"10"}}
{ "name":"jack", "age" :18}
{"index":{"_index":"zhouls","_type":"emp","_id":"11"}}
{"name":"tom", "age":27}
{"update":{"_index":"zhouls","_type":"emp", "_id":"2"}}
{"doc":{"age" :22}}
{"delete":{"_index":"zhouls","_type":"emp","_id":"1"}}
[hadoop@djt002 elasticsearch-2.4.3]$
?
?
?
?
或者
{ "index" : {"_index":"zhouls","_type":"emp","_id":"21"}}
{ "name" : "test21"}
?
例子:
{ "index" : { "_index" : "zhouls", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "index" : { "_index" : "zhouls", "_type" : "type1", "_id" : "2" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "zhouls", "_type" : "type1", "_id" : "2" } } ? ? ? (刪除操作不需要加request body) ? ? ?
{ "create" : { "_index" : "zhouls", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_index" : "zhouls", "_type" : "type1","_id" : "1" } }
{ "doc" : {"field2" : "value2"} }
?
?
?
?
?
(2)使用文件的方式
vi requests?
寫入批量操作語句。比如,下面
{"index":{"_index":"zhouls","_type":"emp","_id":"10"}}
{ "name":"jack", "age" :18}
{"index":{"_index":"zhouls","_type":"emp","_id":"11"}}
{"name":"tom", "age":27}
{"update":{"_index":"zhouls","_type":"emp", "_id":"2"}}
{"doc":{"age" :22}}
{"delete":{"_index":"zhouls","_type":"emp","_id":"1"}}
?
在$ES_HOME目錄下,執行下面命令
curl? -PUT ?'192.168.80.200:9200/_bulk' ? --data-binary @request;
或
?curl? -XPOST ?'192.168.80.200:9200/_bulk' ? --data-binary @request;
[hadoop@djt002 elasticsearch-2.4.3]$ curl -PUT '192.168.80.200:9200/_bulk' --data-binary @request;
{"took":123,"errors":true,"items":[{"index":{"_index":"zhouls","_type":"emp","_id":"10","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"status":201}},{"index":{"_index":"zhouls","_type":"emp","_id":"11","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"status":201}},{"update":{"_index":"zhouls","_type":"emp","_id":"2","status":404,"error":{"type":"document_missing_exception","reason":"[emp][2]: document missing","index":"zhouls","shard":"-1"}}},{"delete":{"_index":"zhouls","_type":"emp","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"status":404,"found":false}}]}[hadoop@djt002 elasticsearch-2.4.3]$
?
?
?
之后,再查看下。
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/1?pretty'
{
"_index" : "zhouls",
"_type" : "emp",
"_id" : "1",
"found" : false
}
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/2?pretty'
{
"_index" : "zhouls",
"_type" : "emp",
"_id" : "2",
"found" : false
}
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/11?pretty'
{
"_index" : "zhouls",
"_type" : "emp",
"_id" : "11",
"_version" : 4,
"found" : true,
"_source" : {
"name" : "tom",
"age" : 27
}
}
[hadoop@djt002 elasticsearch-2.4.3]$ curl -XGET 'http://192.168.80.200:9200/zhouls/emp/10?pretty'
{
"_index" : "zhouls",
"_type" : "emp",
"_id" : "10",
"_version" : 4,
"found" : true,
"_source" : {
"name" : "jack",
"age" : 18
}
}
?
?
?
(3)?bulk請求可以在URL中聲明/_index 或者/_index/_type
這個,自行去測試!
?
?
?
?
?
?
?
?
?
官網
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html?
轉載于:https://www.cnblogs.com/zlslch/p/6422055.html
總結
以上是生活随笔為你收集整理的Elasticsearch之批量操作bulk的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王平与武汉向芯力供应链公司有关吗?
- 下一篇: 黑豹在《黑豹2》中出现了吗?