Elasticsearch 데이터 처리
ELK Stack에서 데이터를 저장하고, 검색하는 (개인적으로) 가장 핵심이라고 생각하는 Elasticsearch
우리에게 친숙한 데이터베이스인 RDMS와 용어부터 차이가 있다.
자세한 이론적인 내용은 아직 너무 걸음마 수준이고, 나도 검색해보고 있는 입장이라 생략하고
지금까지 파악? 시도?해 본 Elasticsearch의 데이터 처리 방법에 대해서 잠깐 정리해본다.
데이터 저장하기 - Sample Data
Elasticsearch에서 제공하는 Sample Data를 이용한 데이터 저장하기
Loading sample data | Kibana Guide [6.8] | Elastic
If security is enabled, you must have the all Kibana privilege to run this tutorial. You must also have the create, manage read, write, and delete index privileges. See Security privileges for more information.
www.elastic.co
가이드 그대로 했는데 Mapping Error가 뜬다면 ?
분명 버전도 똑같은데 자꾸 도큐먼트 부분에서 매핑 에러가 떴다.
Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters
원인을 검색해보니 알 수 없어서 unsupported 라길래 그냥 _doc 중괄호를 날려버렸다.
PUT /shakespeare
{
"mappings": {
"properties": {
"speaker": {"type": "keyword"},
"play_name": {"type": "keyword"},
"line_id": {"type": "integer"},
"speech_number": {"type": "integer"}
}
}
}
그랬더니 됐다.
이유는 ... 공부중이다.
데이터 구조
Elasticsearch의 데이터들은 각각 고유한 id를 갖는다.
데이터들이 모여 하나의 Document를 이루고, Document들이 모여 하나의 Index가 된다.
데이터를 저장하기에 앞서 Index를 만들어줘야 하는데, 주로 Index를 생성하면서 데이터 타입을 지정하는 Mapping을 같이 해준다.
PUT http://<호스트>:<포트>/<인덱스>
{
"mappings": {
"<도큐멘트>": {
"properties": {
"<키1>": {"type": "<데이터 타입>"},
"<키2>": {"type": "<데이터 타입>"},
"<키3>": {"type": "<데이터 타입>"}
}
}
}
}
인덱스가 잘 만들었는지는 다음 명령어로 확인해볼 수 있다.
GET /_cat/indices?v
만들어 준 인덱스에 데이터를 저장하려면 http://<호스트>:<포트>/<인덱스>/<도큐멘트>/<도큐멘트 id> 로 curl을 날려주면 된다.
curl -XPUT 'http://<호스트>:<포트>/<인덱스>/_doc/<도큐먼트 id>' \
{
"<키 1>":"<값 1>",
"<키 2>":"<값 2>",
"<키 3>":"<값 3>"
}
로컬에 저장 된 JSON 파일 이용 시
curl -H 'Content-Type: application/x-ndjson'
\ -XPOST 'http://<호스트>:<포트>/<인덱스>/_doc/<도큐먼트 id>/_bulk?pretty'
\ --data-binary @<JSON 파일경로.json>
저장한 인덱스 Kibana에서 사용하기 (Yellow 상태 ➡️ Green 상태로 전환)
저장한 인덱스의 상태를 조회해보면 Yellow로 되어 있는 것을 확인할 수 있다. 이 상태가 Green일 때 Kibana에서 사용가능해진다.
http://<호스트>:<포트>/_settings?pretty' -d '{"index":{"number_of_replicas": 0}}' -H 'Content-Type: application/json'
Bulk_API : 여러 데이터 한번에 넣을 때 사용
POST _bulk
{"index":{"_index":"<인덱스>", "_id":"1"}}
{"<필드>":"<값 1>"}
{"index":{"_index":"<인덱스>", "_id":"2"}}
{"<필드>":"<값 2>"}