# ElasticSearch 벌크(Bulk)

img


# μ‹€μŠ΅ ν™˜κ²½

  • πŸ’‘ 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 (opens new window)μ—μ„œ 확인해보면

  • 첫 번째 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
    }
  }
}

잘 μ‚½μž…λœ 것을 확인할 수 μžˆλ‹€.


Last Updated: 6/18/2023, 2:13:15 PM