티스토리 뷰
01) Dependency 설정
Spring boot 프로젝트 하나 만들어 테스트.
Elasticsearch 디펜던시 추가.
<!-- Elastic Search -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.12.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
High level client는 Low level client를 한번 추상화해서 사용.
Low level client는 버전에 영향을 받지 않고 유연하지만 사용하기는 더 어려움.
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high.html
Java High Level REST Client | Java REST Client [master] | Elastic
The Java High Level REST Client works on top of the Java Low Level REST client. Its main goal is to expose API specific methods, that accept request objects as an argument and return response objects, so that request marshalling and response un-marshalling
www.elastic.co
홈페이지를 통해 API를 확인할 수 있음.
@SpringBootApplication
public class ElasticsearchTestApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(ElasticsearchTestApplication.class, args);
String hostname = "localhost";
int port = 9200;
HttpHost host = new HttpHost(hostname, port);
System.out.println(host);
RestClientBuilder restClientBuilder = RestClient.builder(host);
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
String index = "gaia";
String id = "1";
GetRequest getRequest = new GetRequest(index, id);
RequestOptions options = RequestOptions.DEFAULT;
GetResponse response = client.get(getRequest, options);
Map<String, Object> map = response.getSourceAsMap();
System.out.println(map);
}
}
해당 GetRequest를 통해 이전에 gaia Index에 1번 ID로 넣었던 데이터를 가져올 수 있었음.
싱글톤 패턴으러 ElasticUtil 만들어 호출해보기.
package com.example.demo;
import java.io.IOException;
import java.io.Reader;
import java.util.Map;
import java.util.Properties;
import org.apache.http.HttpHost;
import org.apache.ibatis.io.Resources;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
public class ElasticUtil {
private static ElasticUtil self;
private static RestHighLevelClient client;
private ElasticUtil() throws IOException {
Properties properties = new Properties();
Reader reader = Resources.getResourceAsReader("el.properties");
properties.load(reader);
String hostname = properties.getProperty("el.url");
int port = Integer.parseInt(properties.getProperty("el.port"));
HttpHost host = new HttpHost(hostname, port);
RestClientBuilder restClientBuilder = RestClient.builder(host);
client = new RestHighLevelClient(restClientBuilder);
}
public static ElasticUtil getInstance() throws IOException {
if(self == null)
self = new ElasticUtil();
return self;
}
public Map<String,Object> getReponse(String index, String id) {
GetResponse response = null;
GetRequest getRequest = new GetRequest(index, id);
RequestOptions options = RequestOptions.DEFAULT;
try {
response = client.get(getRequest, options);
} catch (IOException e) {
e.printStackTrace();
}
return response.getSourceAsMap();
}
}
src/main/resources 밑에 el.properties 파일을 만들고 url 및 port 정보 저장.
ElasticsearchTestApplication.java 에서 다음과 같이 호출.
package com.example.demo;
import java.io.IOException;
import java.util.Map;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ElasticsearchTestApplication {
public static void main(String[] args) throws IOException {
String index = "gaia";
String id = "1";
ElasticUtil elastic = ElasticUtil.getInstance();
Map<String, Object> map = elastic.getReponse(index, id);
System.out.println(map);
}
}
'DB > Elasticsearch' 카테고리의 다른 글
06. Elasticsearch - snapshot 생성 및 복원 (0) | 2022.07.13 |
---|---|
05. Elasticsearch - 외부 접속 허용 (0) | 2022.05.16 |
04. Elasticsearch - Java를 이용하여 Post Request (Create) (0) | 2021.07.17 |
02. Elasticsearch - 조회, 삽입, 수정, 삭제 (0) | 2021.07.13 |
01. Elasticsearch - 설치 및 실행 (0) | 2021.07.13 |