빅데이터/Elasticsearch
ElasticsearchClient 7.17.7 기준 java client example code
AndersonChoi
2022. 11. 28. 16:59
1) host, port 주입
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")).build();
BulkRequest.Builder br = new BulkRequest.Builder();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient esClient = new ElasticsearchClient(transport);
2) String으로 된 JSON 데이터 정의
String json = "{\"test\":1}"
Reader input = new StringReader(json);
IndexRequest<JsonData> request = IndexRequest.of(i -> i
.index("test-log") //set index
.withJson(input)
);
3) 전송
IndexResponse response = esClient.index(request);
log.info("Indexed with version " + response.version());
출처
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/7.17/indexing.html
반응형