开发者学堂课程【ElasticSearch 入门精讲:批量操作介绍】学习笔记,与课程紧密连接,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/631/detail/9991
批量操作介绍
ES JavaAPI 批量操作介绍 Elasticsearch 的Bulk API 允许批量提交 index 和 delete 请求, 有如下两种用法: 用法1 : BulkRequestBuilder requestBuilder = client.prepareBulk(); for(Person person : personList){ (String obj = getIndexDataFromHotspotData(person); if(obj != null){ requestBuilder.add(client.prepareIndex("test-index' , ex,String.valueOf(person.getId()))setRefresh(true).setSource(obj)); 用法2 : BulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener(){ @Override public void beforeBulk(long l, BulkRequest bulkRequest) { } @Override public void afterBulk(long l, BulkRequest bulkRequest, BulkResponse bulkResponse) { } @Override public void afterBulk(long l, BulkRequest bulkRequest, Throwable throwable) { } )).setBulkActions(10000).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)).setFlushInterval(TimeValue.timeValueSeconds(5)).build(); for(Person person : personList){ String obj = getIndexDataFromHotspotData(person); if(obj != null){ bulkProcessor.add(new IndexRequest(“test-index” ," test",String.valueOf(person.getId())).source(obj)); beforeBulk 会在批量提交之前执行,可以从 BulkRequest 中获取请求信息request.requests()或者请求数量 request.numberOfActions() 第一个 afterBulk 会在批量成功后执行,可以跟 beforeBulk 配合计算批量所需时间 第二个 afterBulk 会在批量失败后执行 在例子中,当请求超过10000个( default= 1000 )或者总大小超过1GB ( default=5MB )时,触发批量提交动作 |