Elasticsearch之批量操作bulk
1、bulk相當(dāng)于數(shù)據(jù)庫里的bash操作。
2、引入批量操作bulk,提高工作效率,你想啊,一批一批添加與一條一條添加,誰快?
3、bulk API可以幫助我們同時執(zhí)行多個請求
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的區(qū)別
如果數(shù)據(jù)存在,使用create操作失敗,會提示文檔已經(jīng)存在,使用index則可以成功執(zhí)行。
7、bulk一次最大處理多少數(shù)據(jù)量?
bulk會把將要處理的數(shù)據(jù)載入內(nèi)存中,所以數(shù)據(jù)量是有限制的,最佳的數(shù)據(jù)量不是一個確定的數(shù)值,它取決于你的硬件,你的文檔大小以及復(fù)雜性,你的索引以及搜索的負(fù)載。
一般建議是1000-5000個文檔,如果你的文檔很大,可以適當(dāng)減少隊(duì)列,大小建議是5-15MB,默認(rèn)不能超過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,去看官網(wǎng)就是)在Linux里,有無后綴沒區(qū)別。
[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目錄下,執(zhí)行下面命令
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
這個,自行去測試!
官網(wǎng)
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
總結(jié)
以上是生活随笔為你收集整理的Elasticsearch之批量操作bulk的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python求数字平均值_python
- 下一篇: python牛顿迭代法求平方根_牛顿迭代