아무거나

MAPPING (SCHEMA) 본문

Data Store/Elastic Stack

MAPPING (SCHEMA)

전봉근 2019. 5. 8. 11:00
반응형

매핑없이 데이터를 넣는것은 매우 위험한일이다 예를들어 도큐먼트에 date를 넣는다하면 elasticsearch가 날짜인지

아닌지 모르니까 단순히 문자열로 저장하는 경우가 있다. 또한 숫자를 넣을때 숫자일지 문자일지 구분이 안갈때

문자로 넣을 수도 있다. 이런 부분들은 예를들어 계산이나 데이터 시각화에 문제가 생긴다. 그러므로

매핑은 적극적으로 해야한다.

 

[매핑할 json 파일]

{
  "class":{
    "properties":{
      "title":{"type":"string"},
      "professor":{"type":"string"},
      "major":{"type":"string"},
      "semester":{"type":"string"},
      "student_count":{"type":"integer"},
      "unit":{"type":"integer"},
      "rating":{"type":"integer"},
      "submit_date":{"type":"date", "format":"yyyy-MM-dd"},
      "school_location":{"type":"geo_point"}
    }
  }
}

 

- curl -XPUT 'http://localhost:9200/classes?pretty'  // 인덱스 생성

 

- curl -XGET 'http://localhost:9200/classes?pretty'  // 생성된 인덱스 확인

 

- curl -XPUT 'http://localhost:9200/classes/class/_mapping' -d @classesRating_mapping.json   // 매핑추가 {"acknowledged":true}

  [classesRating_mapping 데이터 내용]

{
    "class":{
      "properties":{
        "title":{"type":"string"},
        "professor":{"type":"string"},
        "major":{"type":"string"},
        "semester":{"type":"string"},
        "student_count":{"type":"integer"},
        "unit":{"type":"integer"},
        "rating":{"type":"integer"},
        "submit_date":{"type":"date", "format":"yyyy-MM-dd"},
        "school_location":{"type":"geo_point"}
      }
    }
  }

 

- curl -XGET 'http://localhost:9200/classes?pretty'  // 매핑된 인덱스 확인

  [결과]

{
    "classes" : {
      "aliases" : { },
      "mappings" : {
        "class" : {
          // 매핑된 json 데이터
          "properties" : {
            "major" : {
              "type" : "text"
            },
            "professor" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "rating" : {
              "type" : "integer"
            },
            "school_location" : {
              "type" : "geo_point"
            },
            "semester" : {
              "type" : "text"
            },
            "student_count" : {
              "type" : "integer"
            },
            "submit_date" : {
              "type" : "date",
              "format" : "yyyy-MM-dd"
            },
            "title" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "unit" : {
              "type" : "integer"
            }
          }
        }
      },
      "settings" : {
        "index" : {
          "creation_date" : "1510390588793",
          "number_of_shards" : "5",
          "number_of_replicas" : "1",
          "uuid" : "DimiIPiQRhWEMGiLlv_iKA",
          "version" : {
            "created" : "5060499"
          },
          "provided_name" : "classes"
        }
      }
    }
  }

 

- 매핑이 완료되면 index에 실제 데이터들을 넣어주자 bulk 옵션을 사용하여 여러개의 document를 한꺼번에 넣어주자.

  curl -XPOST 'http://localhost:9200/_bulk?pretty' --data-binary @classes.json  // 이전에 Bulk실습때 만들었던 classes.json도 적용하자

  (https://bkjeon1614.tistory.com/298 참고)

 

- curl -XGET 'http://localhost:9200/classes/class/1?pretty' // 확인​

반응형

'Data Store > Elastic Stack' 카테고리의 다른 글

메트릭 어그리게이션(Metric Aggregation)  (0) 2019.05.09
데이터 조회  (0) 2019.05.09
벌크(BULK) INSERT  (0) 2019.05.08
데이터 업데이트(UPDATE)  (0) 2019.05.07
데이터 입력 조회 삭제(GET, POST, DELETE..)  (0) 2019.05.07
Comments