Elasticsearch数据导入

import linecache
from pyes import ES

ES_SERVER = "47.92.71.18:9200" //服务器地址
ES_INDEX = "cellinfo" //数据库实例
ES_TYPE = "cell" //表名
CELL_FILE = "cellinfo_v2_19(19).txt" //插入文件
BULK_SIZE = 1000 //1000条执行一次


def create_document(line):
# Delete new line '\n' notation
line = line.strip('\n')
# split line into list
data = line.split('\t')
# Compose return dict
ret_data = {
"mcc": data[0],
"mnc": data[1],
"lac": data[2],
"ci": data[3],
"location": {
"lat": round(float(data[4]), 8),
"lon": round(float(data[5]), 8),
},
"acc": data[6],
"date": data[7],
"validity": data[8],
"addr": data[9],
"province": data[10],
"city": data[11],
"district": data[12],
"township": data[13],
}
return ret_data


def main():
es_conn = ES(ES_SERVER, timeout=20.0, bulk_size=BULK_SIZE)
error_index = 0
try:
for i in range(1, 6000000):
current_line = linecache.getline(CELL_FILE, i)
es_conn.index(create_document(current_line), ES_INDEX, ES_TYPE, bulk=True)
if i % BULK_SIZE == 0:
print('%d' % i)
error_index = i
except IndexError:
es_conn.force_bulk()
print("end!")
except:
print("error at %d" % error_index)


if __name__ == '__main__':
main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值