iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python插入数据到elasticse
  • 762
分享到

Python插入数据到elasticse

数据Pythonelasticse 2023-01-31 01:01:07 762人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

将一个文件中的内容逐条写入elasticsearch中,效率没有写hadoop高,跟kafka更没得比import time from elasticsearch import Elasticsearch from collections

将一个文件中的内容逐条写入elasticsearch中,效率没有写hadoop高,跟kafka更没得比
import time
from elasticsearch import Elasticsearch
from collections import OrderedDict


start_time = time.time()
es = Elasticsearch(['localhost:9200'])
temp_list = []
with open('log.out','r',encoding='utf-8')as f:
    data_list = f.readlines()
for data in data_list:
    temp = OrderedDict()
    temp['ServerIp'] = data.split('|')[0]
    temp['SpiderType'] = data.split('|')[1]
    temp['Level'] = data.split('|')[2]
    temp['Date'] = data.split('|')[3]
    temp['Type'] = data.split('|')[4]
    temp['OffSet'] = data.split('|')[5]
    temp['DockerId'] = data.split('|')[6]
    temp['WEBSiteId'] = data.split('|')[7]
    temp['Url'] = data.split('|')[8]
    temp['DateStamp'] = data.split('|')[9]
    temp['NaviGationId'] = data.split('|')[10]
    temp['ParentWebSiteId'] = data.split('|')[11]
    temp['TargetUrlNum'] = data.split('|')[12]
    temp['Timeconsume'] = data.split('|')[13]
    temp['Success'] = data.split('|')[14]
    temp['Msg'] = data.split('|')[15]
    temp['Extend1'] = data.split('|')[16]
    temp['Extend2'] = data.split('|')[17]
    temp['Extend3'] = data.split('|')[18]
    # temp_list.append(temp)
    body = {'ServerIp': temp['ServerIp'],
            'SpiderType': temp['SpiderType'],
            'Level': temp['Level'],
            'Date': temp['Date'],
            'Type': temp['Type'],
            'OffSet': temp['OffSet'],
            'DockerId': temp['DockerId'],
            'WebSiteId': temp['WebSiteId'],
            'Url': temp['Url'],
            'DateStamp': temp['DateStamp'],
            'NaviGationId': temp['NaviGationId'],
            'ParentWebSiteId': temp['ParentWebSiteId'],
            'TargetUrlNum': temp['TargetUrlNum'],
            'Timeconsume': temp['Timeconsume'],
            'Success': temp['Success'],
            'Msg': temp['Msg'],
            'Extend1': temp['Extend1'],
            'Extend2': temp['Extend2'],
            'Extend3': temp['Extend3'],
            }
    es.index(index='shangjispider', doc_type='spider', body=body, id=None)
end_time = time.time()
t = end_time - start_time
print(t)

不得不说,这样搞,效率真的不高,插入287条用了30s,根本没法投入生产,在想别的办法

-----------------------------------------------------------------------------------------------------------------

又搞了半天,发现了一个新的方法,效率陡增啊,老铁,有木有!!!!

=》

==》

===》

====》

=====》

======》


-----------------------------------------------擦亮你的双眼---------------------------------------------------------


import time
from elasticsearch import Elasticsearch
from elasticsearch import helpers


start_time = time.time()
es = Elasticsearch()
actions = []
f = open('log.out', 'r', encoding='utf-8')
data_list = f.readlines()
i = 0
for data in data_list:
    line = data.split('|')
    action = {
        "_index": "haizhen",
        "_type": "imagetable",
        "_id": i,
        "_source": {
            'ServerIp': line[0],
            'SpiderType': line[1],
            'Level': line[2],
            'Date': line[3],
            'Type': line[4],
            'OffSet': line[5],
            'DockerId': line[6],
            'WebSiteId': line[7],
            'Url': line[8],
            'DateStamp': line[9],
            'NaviGationId': line[10],
            'ParentWebSiteId': line[11],
            'TargetUrlNum': line[12],
            'Timeconsume': line[13],
            'Success': line[14],
            'Msg': line[15],
            'Extend1': line[16],
            'Extend2': line[17],
            'Extend3': line[18],
        }
    }
    i += 1
    actions.append(action)
    if len(action) == 1000:
        helpers.bulk(es, actions)
        del actions[0:len(action)]

if i > 0:
    helpers.bulk(es, actions)

end_time = time.time()
t = end_time - start_time
print('本次共写入{}条数据,用时{}s'.fORMat(i, t))

见证奇迹的时刻

这效率杠杠滴呀,反正是够我用了,先这样吧,需要提升了再想办法吧

老铁,记得给我点赞喏!!!!

--结束END--

本文标题: Python插入数据到elasticse

本文链接: https://www.lsjlt.com/news/184482.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作