依赖
<!-- 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)
.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);
}