Thursday, January 31, 2019

ตัวอย่าง python ที่ใช้ในการ import ข้อมูลจาก excel เข้า elasticsearch

import xlrd
from elasticsearch import Elasticsearch

es=Elasticsearch([{'host':'localhost','port':9200}])
print(str(es))

wb = xlrd.open_workbook('tci-out-31012019.xls')
sheet_list = wb.sheet_names()
s = wb.sheet_by_name(sheet_list[0])

index = 0
header_arr = []

for row in range(0, s.nrows):
    if index == 0:
        for col in range(0, s.ncols):
            header_arr.append(s.cell(row,col).value.strip())
    else:
        value = {}
        for col in range(0, s.ncols):
            val = s.cell(row,col).value
            if isinstance(val, unicode):
                str_val = ""+val.encode('utf8')
            else:
                str_val = str(val)
            value[str(header_arr[col]).strip()] = str_val.strip()

        res = es.index(index='tci',doc_type='article',id=row,body=value,request_timeout=30)
    index = index+1

print("finished!")

ถ้ามี error ขึ้นว่า

FORBIDDEN/12/index read-only / allow delete (api)]

แสดงว่ามีพื้นที่ hdd เหลือไม่เพียงพอ เพราะ elasticsearch ต้องการ disk อย่างน้อยต้องเหลือ 5%

No comments:

Post a Comment