python批量导入数据进Elasticsearch的实例

作者:小威xiaowei2 时间:2022-06-29 16:08:02 

ES在之前的博客已有介绍,提供很多接口,本文介绍如何使用python批量导入。ES官网上有较多说明文档,仔细研究并结合搜索引擎应该不难使用。

先给代码


#coding=utf-8
from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch import helpers
es = Elasticsearch()
actions = []
f=open('index.txt')
i=1
for line in f:
line = line.strip().split(' ')
action={
"_index":"image",
"_type":"imagetable",
"_id":i,
"_source":{
 u"图片名":line[0].decode('utf8'),
 u"来源":line[1].decode('utf8'),
 u"权威性":line[2].decode('utf8'),
 u"大小":line[3].decode('utf8'),
 u"质量":line[4].decode('utf8'),
 u"类别":line[5].decode('utf8'),
 u"型号":line[6].decode('utf8'),
 u"国别":line[7].decode('utf8'),
 u"采集人":line[8].decode('utf8'),
 u"所属部门":line[9].decode('utf8'),
 u"关键词":line[10].decode('utf8'),
 u"访问权限":line[11].decode('utf8')
 }
}
i+=1
actions.append(action)
if(len(actions)==500):
helpers.bulk(es, actions)
del actions[0:len(actions)]
if (len(actions) > 0):
helpers.bulk(es, actions)

每句话的含义还是很明显的,这里需要说几点,首先是index.txt是以utf8编码的,所以需要decode('utf8')转换成unicode对象,并且“图片名”前需要加u,否则ES会报错

导入的速度还是很快的,2000多条记录每秒。

来源:https://blog.csdn.net/u012236368/article/details/51284587

标签:python,导入,数据,Elasticsearch
0
投稿

猜你喜欢

  • Python中scatter函数参数及用法详解

    2022-05-03 09:55:58
  • 提高asp程序访问速度的方法

    2008-10-23 16:37:00
  • Python应用之利用pyecharts画中国地图

    2023-05-27 16:45:40
  • python tkinter界面居中显示的方法

    2023-10-14 11:16:18
  • ASP中Utf-8与Gb2312编码转换乱码问题的解决方法 页面编码声明

    2012-11-30 20:45:55
  • 整理几个js日历源代码

    2008-01-03 13:13:00
  • 通过淘宝数据爬虫学习python scrapy requests与response对象

    2021-11-07 05:46:39
  • Python实现学生成绩管理系统

    2023-08-13 09:51:17
  • PHP获取url的函数代码

    2023-10-15 12:45:00
  • python Pillow图像处理方法汇总

    2021-09-08 18:02:45
  • Google开源的Python格式化工具YAPF的安装和使用教程

    2023-02-02 12:32:16
  • 简单易懂Pytorch实战实例VGG深度网络

    2021-09-07 19:47:24
  • python爬虫中采集中遇到的问题整理

    2022-10-17 03:32:23
  • 如何在asp中创建DSN?

    2009-11-14 20:46:00
  • python的几种开发工具介绍

    2021-09-28 01:54:44
  • 如何“看看”服务器上的那个文件还在不在?

    2009-11-02 20:17:00
  • python ConfigParser库的使用及遇到的坑

    2021-08-13 05:45:23
  • Python函数高级(命名空间、作用域、装饰器)

    2022-03-15 23:31:44
  • python zip,lambda,map函数代码实例

    2023-08-25 05:04:49
  • python实现邮箱发送信息

    2023-02-25 15:11:51
  • asp之家 网络编程 m.aspxhome.com