1. 程式人生 > >Elasticsearch的Bulk API使用

Elasticsearch的Bulk API使用

1、Elasticsearch的Bulk API允許批量提交index和delete請求。如:
(1)用法1

BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareIndex("index1", "type1", "id1").setSource(source);
bulkRequest.add(client.prepareIndex("index2", "type2", "id2").setSource(source);
BulkResponse bulkResponse = bulkRequest.execute().actionGet();

(2)用法二

BulkProcessor bulkProcessor = BulkProcessor.builder(  
        client,    
        new BulkProcessor.Listener() {  
            @Override  
            public void beforeBulk(long executionId,  
                                   BulkRequest request) { ... }   

            @Override  
            public void afterBulk(long executionId,  
                                  BulkRequest request,  
                                  BulkResponse response) { ... }   

            @Override  
            public void afterBulk(long executionId,  
                                  BulkRequest request,  
                                  Throwable failure) { ... }   
        })  
        .setBulkActions(10000)   
        .setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB))   
        .setFlushInterval(TimeValue.timeValueSeconds(5))   
        .build();  
bulkProcessor.add(new IndexRequest("index1", "type1", "id1").source(source1));  
bulkProcessor.add(new DeleteRequest("index2", "type2", "id2"); 

(3)說明
1)beforeBulk會在批量提交之前執行,可以從BulkRequest中獲取請求資訊request.requests()或者請求數量request.numberOfActions()。
2) 第一個afterBulk會在批量成功後執行,可以跟beforeBulk配合計算批量所需時間。
3)第二個afterBulk會在批量失敗後執行。
4)在例子中,當請求超過10000個(default=1000)或者總大小超過1GB(default=5MB)時,觸發批量提交動作。