[ElasticSearch] 데이터 벌크(Bulk)
by youngjun._.
실습 환경
- 💡 Elasticsearch 7.9.0
- 💡 Windows 10
- 💡 Git Bash
Bulk는 간단히 설명하면 여러개의 Document(데이터)를 한번에 ES
에 삽입하는 방법이다.
1. BULK POST
명령어 구조는 다음과 같다.
curl -XPOST "/ES주소/"_bulk --data-binary @"file명".json
실제로 사용해보자.
삽입하고자 하는 Document 내용을 JSON file에서 확인해보면
- 첫 번째 Line : Meta information
- 두 번째 Line : 실제 들어갈 값
{ "index" : { "_index" : "classes", "_type" : "class", "_id" : "1" } } {"title" : "Machine Learning","Professor" : "YoungJun Park","major" : "Computer Science","semester" : ["spring", "fall"],"student_count" : 100,"unit" : 3,"rating" : 5, "submit_date" : "2020-01-02", "school_location" : {"lat" : 36.00, "lon" : -120.00}}
이렇게 2 Line
씩 반복되는 포맷이 저장되어있다.
POST 명령어로 삽입해보자.
$ curl -XPOST http://localhost:9200/_bulk?pretty --data-binary @classes.json -H 'Content-Type: application/json'
만약 Bulk Request Error가 발생한다면 JSON file 마지막에 엔터를 입력해
New Line
을 입력해주면 된다.
잘 들어갔는지 1번 id
부터 확인해보자.
$ curl -XGET http://localhost:9200/classes/class/1/?pretty
결과
{ "_index" : "classes", "_type" : "class", "_id" : "1", "_version" : 7, "_seq_no" : 6, "_primary_term" : 1, "found" : true, "_source" : { "title" : "Machine Learning", "Professor" : "YoungJun Park", "major" : "Computer Science", "semester" : [ "spring", "fall" ], "student_count" : 100, "unit" : 3, "rating" : 5, "submit_date" : "2020-01-02", "school_location" : { "lat" : 36.0, "lon" : -120.0 } } }
2번 id
도 확인해보면
$ curl -XGET http://localhost:9200/classes/class/2/?pretty
결과
{ "_index" : "classes", "_type" : "class", "_id" : "2", "_version" : 1, "_seq_no" : 7, "_primary_term" : 1, "found" : true, "_source" : { "title" : "Network", "Professor" : "YoungJun Park", "major" : "Computer Science", "semester" : [ "fall" ], "student_count" : 50, "unit" : 3, "rating" : 4, "submit_date" : "2020-02-02", "school_location" : { "lat" : 36.0, "lon" : -120.0 } } }
잘 삽입된 것을 확인할 수 있다.
블로그의 정보
개발하는만두
youngjun._.