๋กœ์ผ“๐Ÿพ
article thumbnail

์ € ๊ฐ™์€ ๊ฒฝ์šฐ Elasitc Search๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ง์ ‘ API ๋ฌธ์„œ ๋ณด๊ณ  ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. API ๋ฌธ์„œ๊ฐ€ ์˜์–ด์ด๊ธด ํ•˜์ง€๋งŒ ์ƒ๋‹นํžˆ ์ž์„ธํžˆ ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜ ๋งํฌ๋Š” Elasitc Search ๊ณต์‹ API ๋ฐ ์‚ฌ์šฉ๋ฒ• ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค.

 

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

 

์ฃผ์˜ ํ• ๋งŒํ•œ ์ ์€ ์ตœ์†Œ jdk1.8์ด์ƒ์ด์—ฌ์•ผ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  6.x๋ฒ„์ „๋“ค์€ ํ˜ธํ™˜์ด ์ œ๋Œ€๋กœ ์•ˆ๋  ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

์ œ ์„œ๋ฒ„ ํ™˜๊ฒฝ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • CentOS7
  • JDK1.8
  • ElasticSearch 7.13.4
  • maven 3.x

 

[STEP1] maven pom.xml์—์„œ Elastic Search(์ดํ•˜ ES) dependency์„ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

<!-- elasticsearch -->
<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>elasticsearch-rest-high-level-client</artifactId>
	<version>7.13.4</version>
</dependency>

The High Level Java REST Client depends on the following artifacts and their transitive dependencies:

  • org.elasticsearch.client:elasticsearch-rest-client
  • org.elasticsearch:elasticsearch

[STEP2] ES์™€ ์—ฐ๋™ํ•˜๊ธฐ

RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost("localhost", 9200, "http"));

์œ„์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•จ์œผ๋กœ์จ ES ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ES๋Š” ๊ธฐ๋ณธ ํฌํŠธ๊ฐ€ 9200์ž…๋‹ˆ๋‹ค.

 

client.close();

๊ทธ๋ฆฌ๊ณ  ํ•ญ์ƒ ๋™์ ํ•ด์ œ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ client๋ฅผ ๋‹ซ์•„์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

* ์ฐธ๊ณ  : ๋งŒ์•ฝ ์‚ฌ์šฉ์ž ์ธ์ฆ์„ ํ•ด์•ผํ•œ๋‹ค๋ฉด ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

 

[Elastic Search+Java] ์‚ฌ์šฉ์ž ์ธ์ฆ ๊ตฌํ˜„ํ•˜๊ธฐ Basic Authentication

์ € ๊ฐ™์€ ๊ฒฝ์šฐ Elasitc Search๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ง์ ‘ API ๋ฌธ์„œ ๋ณด๊ณ  ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๋งํฌ๋Š” Elasitc Search ๊ณต์‹ API Basic Authentication ์ž…๋‹ˆ๋‹ค. Basic authentication | Java REST Client [7.14] |..

kingofbackend.tistory.com


[FINAL STEP] INDEX ์ƒ์„ฑํ•˜๊ธฐ

ES์—์„œ INDEX๋Š” ๊ธฐ์กด RDB์™€๋Š” ๊ฐœ๋…์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ES์—์„œ INDEX๋Š” DB์™€ ๊ฐ™๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

  • INDEX = DB(SCHEMA)
  • TYPE = TABLE ( 7.x ์ด์ƒ๋ถ€ํ„ฐ๋Š” TPYE์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ณต์‹๋ฌธ์„œ ์ฐธ๊ณ )
  • DOCUMENTs = ROWS

์—ญ์‹œ ๊ณต์‹ ๋ฌธ์„œ์— ์ƒ๋‹นํžˆ ์ž์„ธํžˆ ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ ์ €๊ฐ™์€ ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด INDEX๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

GetIndexRequest indexRequest = new GetIndexRequest("st_pdt");
BulkRequest bulkRequest = new BulkRequest(); 

int pdt_id=0;

for(Pdt pdt : pdtList) {
  builder.startObject();
  {	
      builder.field("s_id", pdt.getShopid());
      builder.field("s_name", pdt.getS_name());
  }
  builder.endObject();
  
  bulkRequest.add(new IndexRequest("st_pdt").id(String.valueOf(pdt_id)).source(builder));
  pdt_id++;
}

 

Single document APIs์ธ Index API์™€ Multi document APIs์ธ Bulk API๊ฐ€ ์žˆ๋Š”๋ฐ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ๋ฐ์ดํ„ฐ๋ฅผ INSERT ํ• ๊ฑฐ๋ƒ ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ํ• ๊ฑฐ๋ƒ์˜ ์ฐจ์ด์ž…๋‹ˆ๋‹ค. ๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ INSERT ํ•˜๋ ค๊ณ  ์ €๋Š” Bulk API๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

GetIndexRequest๋ฅผ ์ด์šฉํ•˜์—ฌ st_pdt INDEX๋ฅผ ์ƒ์„ฑํ•˜์˜€๊ณ , BulkRequest๋ฅผ ์ด์šฉํ•˜์—ฌ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ st_pdt INDEX์— INSERTํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

$ curl -X GET "localhost:9200/st_pdt" ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ œ๋Œ€๋กœ INDEX๊ฐ€ ๋งŒ๋“ค์–ด์กŒ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

 

์ด์ƒ ์ž๋ฐ”์™€ ES๋ฅผ ์—ฐ๋™ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๊ธ€์—์„œ๋Š” ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ st_pdt์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ GETํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

profile on loading

Loading...