ELK

ELK 엘라스틱서치 Data CRUD

1. 엘라스틱서치 데이터 입력 조회 삭제 (GET, POST, PUT, DELETE)

이번 시간에 살펴볼 내용은 엘라스틱서치에서 Data CRUD를 실습해보는 시간을 가져보겠습니다.

그전에 엘라스틱서피와 RDS와의 차이점을 살펴보겠습니다.

ELASTIC SEARCH VS RDS

Elastic Search RDS
Index Database
Type Table
Document Row
Field Column
Mapping Schema

엘라스틱서치에서는 REST API를 사용합니다.

Elastic Search RDS CRUD
GET Select Read
PUT Update Update
POST Insert Create
DELETE Delete Delete

다음과 같이 표를 보면 CRUD의 역할과 동일하게 처리한다라고 생각을 하면됩니다.

GET 조회시

1
2
curl -XGET http://localhost:9200/classes
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index [classes]","resource.type":"index_or_alias","resource.id":"classes","index_uuid":"_na_","index":"classes"}],"type":"index_not_found_exception","reason":"no such index [classes]","resource.type":"index_or_alias","resource.id":"classes","index_uuid":"_na_","index":"classes"},"status":404}%

curl의 -XGET 옵션을 통하여 엘라스틱서치의 9200번 포트로 접근해줍니다.

1
2
3
4
curl -XGET http://localhost:9200/classes

http://localhost:9200 - 엘라스틱서치 주소
classes - Index

GET 조회시 깔끔하게 보고싶다면?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl -XGET http://localhost:9200/classe\?pretty
{
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index [classe]",
"resource.type" : "index_or_alias",
"resource.id" : "classe",
"index_uuid" : "_na_",
"index" : "classe"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index [classe]",
"resource.type" : "index_or_alias",
"resource.id" : "classe",
"index_uuid" : "_na_",
"index" : "classe"
},
"status" : 404
}

다음과 같이 JSON 포멧으로 깔끔하게 결과값들을 확인할 수 있습니다.

인덱스 생성 PUT

1
2
$curl -XPUT http://localhost:9200/classes
{"acknowledged":true,"shards_acknowledged":true,"index":"classes"}%

다음과 같이 인덱스가 정상적으로 생성된것을 확인할 수 있습니다.

인덱스 삭제 DELETE

1
2
curl -XDELETE http://localhost:9200/classes
{"acknowledged":true}%

다음과 같이 정상적으로 엘라스틱서치의 인덱스값이 삭제된것을 확인할 수 있습니다.

Document 생성하기

1
2
curl -XPOST -H 'Content-Type: application/json' http://localhost:9200/classes/class/1/ -d '{"title" : "Algorithm", "professor" : "John"}'
{"_index":"classes","_type":"class","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}%

주의사항
curl -XPOSt http://localhost:9200/classes/class/1/ -d ‘{“title” : “Algorithm”, “professor” : “John”}’ 입력 시 www-form-urlencoded is not supported status 406 에러가 발생합니다. 높은 버전에선 -H ‘Content-Type: application/json’ 를 사용해주어야합니다.

참고링크
https://www.elastic.co/kr/blog/strict-content-type-checking-for-elasticsearch-rest-requests

Starting from Elasticsearch 6.0, all REST requests that include a body must also provide the correct content-type for that body.

Document 생성된것 확인하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$curl -XGET http://localhost:9200/classes/class/1/\?pretty
{
"_index" : "classes",
"_type" : "class",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Algorithm",
"professor" : "John"
}
}

파일을 사용하여 Document 생성하기

1
2
curl -XPOST -H 'Content-Type: application/json' http://localhost:9200/classes/class/1/ -d @class.json
{"_index":"classes","_type":"class","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}%

@기호를 사용하여 class.json 파일안에 있는 json 데이터를 통하여 Document를 생성하였습니다. JSON내용은 다음과같습니다.

1
2
3
4
5
6
7
8
9
{
"title" : "Machine Learning",
"Professor" : "Minsuk Heo",
"major" : "Computer Science",
"semester" : ["spring", "fall"],
"student_count" : 100,
"unit" : 3,
"rating" : 5
}

Document 조회해보기

이제 정상적으로 파일에서 가져온 Document값을 조회해보도록 하겠습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl -XGET http://localhost:9200/classes/class/1/\?pretty
{
"_index" : "classes",
"_type" : "class",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Machine Learning",
"Professor" : "Minsuk Heo",
"major" : "Computer Science",
"semester" : [
"spring",
"fall"
],
"student_count" : 100,
"unit" : 3,
"rating" : 5
}
}

이렇게 정상적으로 파일에 들어가있는 값들을 통하여 document로 정상적으로 구성되었습니다.

Data Update

1
2
3
curl -XPOST -H 'Content-Type:application/json' http://localhost:9200/classes/class/1/_update -d '
{"doc":{"unit":1}}'
{"_index":"classes","_type":"class","_id":"1","_version":4,"result":"noop","_shards":{"total":0,"successful":0,"failed":0},"_seq_no":3,"_primary_term":1}%

_update를 뒤에 붙여주면서 진행을 하면 해당 unit값이 업데이트된것을 확인할 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
curl -XGET http://localhost:9200/classes/class/1\?pretty
{
"_index" : "classes",
"_type" : "class",
"_id" : "1",
"_version" : 5,
"_seq_no" : 4,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Algorithm",
"professor" : "John",
"unit" : 2
}
}

script를 이용하여 업데이트하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$curl -XPOST -H 'Content-Type:application/json' http://localhost:9200/classes/class/1/_update -d '
quote> {"script":"ctx._source.unit += 5"} '
{"_index":"classes","_type":"class","_id":"1","_version":6,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":5,"_primary_term":1}%
kgh  ~

$curl -XGET http://localhost:9200/classes/class/1\?pretty
{
"_index" : "classes",
"_type" : "class",
"_id" : "1",
"_version" : 6,
"_seq_no" : 5,
"_primary_term" : 1,
"found" : true,
"_source" : {
"title" : "Algorithm",
"professor" : "John",
"unit" : 7
}
}

{“script”:“ctx._source.unit += 5”} 과 같이 script문을 사용하면 다음과같이 정상적으로 데이터가 업데이트를 쉽게 처리할 수 있습니다.

Reference

https://github.com/minsuk-heo/BigData