ES批量提交-RestHighLevelClient
生活随笔
收集整理的這篇文章主要介紹了
ES批量提交-RestHighLevelClient
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
引包:
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>6.8.13</version></dependency>批量提交:?
import org.apache.commons.lang3.RandomStringUtils; import org.apache.http.HttpHost; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient;import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map;public class Test {private static RestHighLevelClient restClient;public static void main(String[] args) {RestClientBuilder builder = RestClient.builder( // new HttpHost("21.145.229.153",9200,"http"), // new HttpHost("21.145.229.253",9200,"http"),new HttpHost("192.168.0.1", 9200, "http")); // CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); // credentialsProvider .setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("elastic","1qaz!QAZ")); // builder.setHttpClientConfigCallback(f->f.setDefaultCredentialsProvider(credentialsProvider ));restClient = new RestHighLevelClient(builder);/*** 造點假數(shù)據(jù)*/List<Map<String, Object>> list = new ArrayList<>();for (int i = 0; i < 100; i++) {Map<String, Object> map = new HashMap<>();map.put("message", RandomStringUtils.randomAlphanumeric(28));map.put("user", RandomStringUtils.randomAlphanumeric(8));map.put("postDate", new Date());list.add(map);}/*** 批量從插入數(shù)據(jù)*/BulkRequest request = new BulkRequest();for (int j = 0; j < list.size(); j++) {Map<String, Object> item = list.get(j);request.add(new IndexRequest("TestIndex", "TestType").source(item));}try {BulkResponse bulk = restClient.bulk(request, RequestOptions.DEFAULT);if (bulk.status().getStatus() == 200) {System.out.println("批量插入成功!");}} catch (IOException e) {e.printStackTrace();}} }?
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的ES批量提交-RestHighLevelClient的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java异步执行任务
- 下一篇: Mac下安装MariaDB(自用备忘)