티스토리 뷰

설치.windows10

www.elastic.co/downloads/elasticsearch?latest

[

Download Elasticsearch Free | Get Started Now | Elastic | Elastic

Want it hosted? Deploy on Elastic Cloud. Get Started »

www.elastic.co

](https://www.elastic.co/downloads/elasticsearch?latest)

위 사이트에서 다운로드 받아서 아래 파일 elasticsearch.bat를 실행 하시면 됩니다.

실행이 잘 되었는지 확인 해보는 방법은 localhost:9200 에 접속 했을 때 아래와 같이 나오면 잘 실행이 된 것입니다.

속도

파이썬에서 JSON파일을 읽는 속도

데이터에 query를 하고 싶을 때 간단하게 pandas를 이용 하는 방법이 있습니다.

df = pd.read_json('file_name') 이렇게 불러와서 쿼리를 하면 간단 합니다. 하지만 파일 크기가 100Mb가 넘어간다면 이야기가 달라집니다.

json file 200mb를 읽어서 python의 json object로 변환하는 시간 약 7초(노트북 기준)가 걸립니다.

시작 : 2020-10-27 11:23:53.256212
끝 : 2020-10-27 11:23:59.852842

df로 불러와서 query를 한다면 더 오랜 시간이 걸릴 것입니다. 하지만 elastic search에 넣고 query를 한다면 query를 했을 때 1초 안에 query를 할 수 있고 작업 속도도 더 빨라지게 됩니다.

es에 insert하는 속도

origin cnt: 485427
bulk insert started. docs cnt: 485427 2020-10-27 14:07:34.658790
bulk insert completed. (485427, []) 2020-10-27 14:08:11.000180

환경마다 다르겠지만 제 노트북으로 테스트한 결과 209Mb의 485,427건 데이터를 es에 넣는데 1분정도 걸렸습니다.

100만건에 400mb정도 된다면 2분정도 걸릴 것 같습니다.

Index생성

curl -XPUT http://localhost:9200/raw

Index생성.with mapping

PUT addr_idx_01/
{
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "sido" : {
          "type" : "keyword"
        },
        "sigungu" : {
          "type" : "keyword"
        }
      }
    }
  }

Index생성 .with document

처음 index를 생성 할 때 mapping을 아직 만들지 않은 경우 한줄을 넣으면 자동으로 mapping을 생성 해줍니다. 자동으로 생성된 mapping을 수정해서 다시 인덱스를 생성할 때 사용합니다.

POST new_index_01/_doc/1
{
  "name":"kyeongrok",
  "age":34
}

위 명령어에서 _doc을 넣어주는 것은 document를 생성하겠다는 뜻이고 /1은 1번 인덱스에 생성하겠다는 뜻입니다.

document생성

index이름은 raw이고 0번 id에 {"hello":"word"}라는 레코드를 넣는다. {}를 ''로 감싸 주어야 한다.
curl -XPOST http://localhost:9200/raw/0/ -H 'Content-Type: application/json' -d '{"hello":"world"}'

curl -XPOST http://localhost:9200/raw/drive/Z1\_YLKJ6\_CONT\_20190915T152513 -H 'Content-Type: application/json' -d @one_drive2.json

one_drive2.json

{"file_count": 2, "ingest_version": "1.4.1-rc2", "drive_id": "Z1_YLKJ6_CONT_20190915T152513", "ingest_duration": 119, "nfs_host": "10.35.106.35", 
 "smb_share": "\\10.35.106.35\raw/YLKJ6/201909/15T152513/Z1_YLKJ6_CONT_20190915T152513", "smb_host": "10.35.106.35", 
 "project_name": "Z1", "ingest_station": "ingeststation2.poc.com", "target_path": "/dmsdemo/z1/DMSPOC/raw/YLKJ6/201909/15T152513", 
 "tags": ["road_work", "rainning"], "dmsclient_version": "1.4.1-rc2", "cluster_id": "DMSPOC",     "logged_at": "2019-09-15T15:25:13+00:00", "size": 2147483648, "updated_at": "2019-09-17T03:57:29.550632",
"source_path": "/raw_data/Z1_YLKJ6_CONT_20190915T152513", "state": "copied", "flc_state": "ready", "car_id": "YLKJ6"}

데이터 넣은 것 조회하기

id가 Z1_YLKJ6_CONT_20190915T152513 이기 때문에 index, type뒤에 넣고 조회함

curl -XGET http://localhost:9200/raw/drive/Z1_YLKJ6_CONT_20190915T152513/\?pretty

인덱스 지우기

DELETE my-index

io_log인덱스 생성하고 datetime설정하기

PUT io_log
{
  "mappings": {
    "properties": {
      "date": {
        "type":   "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}

해당 인덱스 document전체 지우기

POST logstash-test/logs/_delete_by_query?conflicts=proceed
{
  "query": {
    "match_all": {}
  }
}

모든 인덱스 조회하기.with id pw

curl --user <user_id>:<user_pw> -XGET http://localhost:9200/_cat/indices

특정 Index의 mapping조회하기

-XGET http://localhost:9200/<INDEX_NAME>/_mapping?pretty

인덱스에서 검색하기

bds_aaa_price 로 시작하는 모든 index에서 std_prdlst_new_nm가 '에어컨'인 데이터를 찾습니다. 날짜는 20180101~20181231까지

GET bds_aaa_price*/_search
{
    "query":{
        "bool":{
        "must": [
            {"range": {"delng_de": {"gte": 20180101,"lte": 20181231}}}],
            "should":[
                {"term": {"std_prdlst_new_nm": "에어컨"}}
            ]
        }
    }
}

아이디로 업데이트 하기

PUT cars/_doc/RKAngW0BkaXJzSI6hZco
{ 
  "fuel_type":"diesel2"
}

결과

{
  "_index" : "cars",
  "_type" : "_doc",
  "_id" : "RKAngW0BkaXJzSI6hZco",
  "_version" : 2,
  "_seq_no" : 3552913,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "fuel_type" : "diesel2"
  }
}

참고

yookeun.github.io/elasticsearch/2018/03/09/elastic-mapping/

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함