본문 바로가기
카테고리 없음

[elasticsearch]excel to elasitcsearch , csv to elasticsearch 간단

by whathelllllllllllllllll 2021. 10. 28.
728x90

매번 정착하지못하고, 헤매는 csv to elasticsearch

그대로 읽어서 ㅜㅜ 업로드를 해본다. 

 

*설명 : PO, EXNM,NM,TAG 엑셀파일의 컬럼이름 

 

 

from elasticsearch import Elasticsearch
import time
from datetime import datetime
from elasticsearch import helpers
import pandas as pd
import arrow
from pandas.core.frame import DataFrame

#csv ####wine = pd.read_csv("c:/test_ez/tag_20211028.csv")

#excel##

excel_sheet_name = '엑셀은시트이름필수'
excel_file_name = '파일이름.xlsx'
wine = pd.read_excel("c:/test/"+excel_file_name, engine='openpyxl', sheet_name=excel_sheet_name)

##null 있는지 여부 : #wine[wine['TAG'].isnull()]
#null인값을 ''으로 대체,  
wine=wine.fillna({'TAG':''})


es = Elasticsearch("localhost:9200"
                                      , http_auth=('elastic', 'whaaaaatttt'), use_ssl=True, verify_certs=True)
date_s = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
data = [
{"_index": "super_2021102999",
"_type": "_doc",
"_id": x[0],
"_source": {
"PO": x[0],
"@timestamp": date_s,
"EXNM": x[1],
"NM": x[2],
"TAG": x[3]}

  } 
    for x in zip(wine['PO'],wine['EXNM'],wine['NM'],wine['TAG'])
]

helpers.bulk(es, data)

 

 

## 참고 csv to es: https://pydole.tistory.com/entry/Python-elasticsearch-bulk-insert-contain-id
## null제거관련 https://truman.tistory.com/92

댓글