Spring Boot

Elasticsearch indexing(Data insert)

쿨쿨자는너구리 2023. 4. 28. 13:51

Elasticsearch index

 

pom.xml

<dependency>
  <groupId>co.elastic.clients</groupId>
  <artifactId>elasticsearch-java</artifactId>
  <version>8.7.0</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.12.3</version>
</dependency>

Elasticsearch Data를 indexing(Data insert) 하기 위해서는 HashMap으로 데이터를 만들고, index 명령을 통해 데이터를 넣는다.

 

Source code 샘플

HashMap<String, Object> resource

HashMap<String, Object> resource = new HashMap<>();
resource.put("timestamp", timestamp);
resource.put("hostname", hostname);
resource.put("cpu_used", cpu_used);
resource.put("mem_total", mem_total);
resource.put("mem_available", mem_available);
resource.put("disk_total", disk_total);
resource.put("disk_used", disk_used);

RestClient restClient = RestClient.builder(new HttpHost(es_ip, es_port)).build();

ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchAsyncClient asyncClient = new ElasticsearchAsyncClient(transport);
asyncClient.indices().create(c -> c.index(indexname));

IndexRequest<HashMap<String, Object>> request = IndexRequest.of(i -> i.index(indexname).document(resource));
CompletableFuture<IndexResponse> response = asyncClient.index(request);

참고자료 - https://github.com/elastic/elasticsearch-java/blob/main/java-client/src/test/java/co/elastic/clients/elasticsearch/end_to_end/RequestTest.java

 

'Spring Boot' 카테고리의 다른 글

자바 데이터 타입  (0) 2023.04.30
Elasticsearch read  (0) 2023.04.30
Elasticsearch index 생성  (0) 2023.04.27
Spring Boot를 이용한 서버 자원 모니터링 프로그램  (0) 2023.03.27
Spring Boot란?  (0) 2023.03.15