Spring Boot

Elasticsearch read

쿨쿨자는너구리 2023. 4. 30. 19:45

 

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>

소스코드

// Create the low-level client
RestClient restClient = RestClient.builder(new HttpHost(es_ip, es_port)).build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchAsyncClient asyncClient = new ElasticsearchAsyncClient(transport);

CompletableFuture<SearchResponse<Resource>> searchResponse = asyncClient.search(r -> r.index(indexname).size(size),
Resource.class);

try {
  return searchResponse.get().hits().hits();
} catch (Exception e) {
  e.printStackTrace();
return null;
}

size - 가져올 데이터 수

CompletableFuture - 생성자, get, set으로 이루어진 class에 자동으로 결과 저장

 

Resource.class

public class Resource {
private String hostname = "";
...
public Resource() {
}

public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
...

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

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