본문 바로가기

빅데이터/Elasticsearch

ElasticsearchClient 7.17.7 기준 java client example code

 

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/java-rest-low-usage-initialization.html

 

Initialization | Elasticsearch Java API Client [7.17] | Elastic

A RestClient instance can be built through the corresponding RestClientBuilder class, created via RestClient#builder(HttpHost...) static method. The only required argument is one or more hosts that the client will communicate with, provided as instances of

www.elastic.co

https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/7.17/indexing.html

 

Indexing single documents | Elasticsearch Java API Client [7.17] | Elastic

Indexing single documentsedit The Java API Client offers several ways to index data: you can provide application objects that will be automatically mapped to JSON, or you can provide raw JSON data. Using application objects is more suited to applications w

www.elastic.co

 

반응형