A place to hold mainly reading notes, and some technical stuff occasionally. 这里主要是一些读书笔记、感悟;还有部分技术相关的内容。
目录[-]
前面安装运行了ElasticSearch
,在实际操作之前,先准备好数据,实际中一般是通过Logstash
等插件实现其他数据库到ElasticSearch
的同步,这里的演示采用直接导入的方式。
ElasticSearch官方有个数据集,文件内容的格式以行分隔:
每两行为一组,第一行指定索引id(也可为空),第二行为实际的数据体。
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
...省略
以下提供两种方法批量导入数据到ES的bank
索引:Postman
与Kibana
。通过ES提供的_bulk
API完成批量导入。
首先在Postman
中新建 POST
请求:localhost:9200/bank/_bulk
, 请求体Body下选择binary
二进制, 然后Select File
选择对应的json
文件, 最后点击Send发送请求即可,见下图。
依次启动ElasticSearch、Kibana,Kibana
默认启动在5601
端口,打开(Dev Tools)开发工具。
左侧为请求编辑区,右侧为对应的响应。在左侧输入:
POST bank/_bulk
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
...省略
点击运行,Kibana
中的执行结果为:
在Postman
中发送Post
请求:localhost:9200/bank/_search,请求体:
{
"query":{
"match_all": {}
}
}
得到bank
索引中所有1000条数据,表明批量导入操作成功:
有时候会有批量删除的需求,尤其是在测试或演示时。
仅删除数据,保留索引以及映射。
在Postman
中发送Post
请求:localhost:9200/bank/_delete_by_query,请求体:
{
"query":{
"match_all": {}
}
}
在Postman
中发送Delete
请求:localhost:9200/bank,无参数。
Notes: 删除索引为高危操作,谨慎使用!!😎
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!