java 连接 Elasticsearch 使用 TransportClient 进行批量增加、删除、修改并使用BulkProcessorListener监听

依赖

<!-- org.elasticsearch.client/transport -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>7.13.3</version>
</dependency>

配置

@Configuration
public class ESConfig {

    @Bean
    public TransportClient getTransportClient() {
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch").build();
        try {
            TransportClient client = new PreBuiltTransportClient(settings)
                    //        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300))//通讯端口  而不是服务端口
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9301));
            return client;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }
}
@Autowired
private TransportClient transportClient;
public void bulkDatas() throws Exception {
    BulkProcessor bulkProcessor = BulkProcessor.builder(
            transportClient,
            new BulkProcessor.Listener() {
                @Override
                public void beforeBulk(long executionId,
                                       BulkRequest request) {
                    System.out.println("执行前");
                }
                @Override
                public void afterBulk(long executionId,
                                      BulkRequest request,
                                      BulkResponse response) {
                    System.out.println("执行后");
                }
                @Override
                public void afterBulk(long executionId,
                                      BulkRequest request,
                                      Throwable failure) {
                    System.out.println("有错误");
                }
            })
            .setBulkActions(10000)
            .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB))
            .setFlushInterval(TimeValue.timeValueSeconds(5))
            .setConcurrentRequests(1)
            .setBackoffPolicy(
                    BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))
            .build();
    DeleteRequest deleteRequest = new DeleteRequest("myindex", "_doc", "7");
    bulkProcessor.add(deleteRequest);
    
    IndexRequest indexRequest = new IndexRequest("myindex", "_doc", "7");
    indexRequest = indexRequest.source(XContentType.JSON, "name", "bb cc dd");
    bulkProcessor.add(indexRequest);
    
    bulkProcessor.awaitClose(10, TimeUnit.MINUTES);
}
posted @ 2022-03-07 18:04  叕叕666  阅读(122)  评论(0)    收藏  举报