1.bulk是什么?
bulk是可以通过API批量执行创建索引,新增数据,更新数据和删除数据到elasticsearch。
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"} }
从上面的代码可以看出,每一条处理记录包含两行(delete除外),第一行是要执行的动作是什么和要操作的索引,类型,ID;第二行是具体操作的操作数据。
2.bulk的注意事项
因为是使用REST API进行数据的提交,因此一次处理的文件大小不宜过大,建议在20MB一下。
3.elasticsearch-tools
3.1官方网站
3.2安装步骤
elasticsearch-tools依赖nodejs
[root@cloud14 ~]# yum -y install npm
//如果出现no package npm available问题
//执行 rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@cloud14 ~]# npm install -g elasticsearch-tools
3.3导出mapping
es-export-mappings --url http://172.16.12.53:9200 --index user_index --file /root/user.mapping.json
3.4导入mapping
es-import-mappings --url http://192.168.22.189:9200 --file ~/user.mapping.json
3.5导出bulk
es-export-bulk --url http://172.16.12.53:9200 --index user_index --file ~/user.bulk.json --body '{"query":{"range":{"loginTime":{"gte":"2017-02-28T00:00:00.000Z","lte":"2017-03-01T00:00:00.000Z"}}}}'
3.6导入bulk
es-import-bulk --url http://192.168.22.189:9200 --file ~/user.bulk.json
3.7score字段处理
因为使用query的查询条件,每一条记录的第一行都会有score字段
{"index":{"_index":"user_index","_type":"user","_id":"145111327","_score":0}}
可以使用sed进行正则匹配和替换
sed -i 's/,"_score":0//g' `grep -rl ',"_score":0' ~/user.bulk.json`
3.8总结
elasticsearch-tools的官方提供了大量的配置参数,可以基本满足简单的导出导入功能.
同时经过测试发现,elasticsearch-tools的导入功能对bulk文件大小也没有限制,因为它默认每次提交20000行.
如果你需要GB级的数据导入导出,那么推荐你使用elasticsearch-tools.