Bulk API-批量执行

来源:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

简介

Bulk API用于执行批量创建索引和删除数据操作。

工具

一些工具可以帮助使用者执行bulk请求:
Perl:
源网址404
Python:
http://elasticsearch-py.readthedocs.io/en/master/helpers.html

使用

(1)api格式:
/_bulk
(2)数据格式:

action_and_meta_data\n
optional_source\n
action_and_meta_data\n
optional_source\n
....
action_and_meta_data\n
optional_source\n

注意:数据必须以/n结束

(3)可以使用的操作
index:需要在下一行中输入数据源
create:需要在下一行中输入数据源
delete:不需要数据源
update:需要部分doc,upsert 和script 操作在下一行中出现。

如果给出的数据是文本,那么必须使用–data-binary 而不能使用简写的-d。-d不保留新行。例如:

$ cat requests
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
$ curl -s -XPOST localhost:9200/_bulk --data-binary "@requests"; echo
{"took":7, "errors": false, "items":[{"index":{"_index":"test","_type":"type1","_id":"1","_version":1,"result":"created","forced_refresh":false}}]}

正因为_bulk是以/n作为分隔符,所以需要确保json动作和json数据不是格式化的(格式化打印的会带着很多/n)。正确的_bulk命令的使用方式:

POST _bulk
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

/_bulk命令可以这样使用:
- /_bulk:需要指定_index,_type
- /{index}/_bulk:需要指定_type
- /{index}/{type}/_bulk

shell脚本批量导入

_bulk接口虽然可以将数据批量导入,但是需要的格式比较复杂,在没有工具的情况下如果将大量数据建立索引还是很麻烦的。这时候,我们可以使用shell脚本直接执行数据导入:

(1) 准备好一个json文件命名为:myfile.json
(2)执行脚本:
while read line
do
curl -XPOST ‘http://localhost:9200/< indexname>/< typeofdoc>/’ -d “$line”
done < myfile.json

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值