python批量導入數(shù)據(jù)進Elasticsearch的實例
ES在之前的博客已有介紹,提供很多接口,本文介紹如何使用python批量導入。ES官網(wǎng)上有較多說明文檔,仔細研究并結合搜索引擎應該不難使用。
先給代碼
#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多條記錄每秒。
以上這篇python批量導入數(shù)據(jù)進Elasticsearch的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python小整數(shù)對象池和字符串intern實例解析
這篇文章主要介紹了Python小整數(shù)對象池和字符串intern實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
Python網(wǎng)絡編程之HTTP客戶端模塊urllib與urllib3
這篇文章介紹了Python網(wǎng)絡編程之HTTP客戶端模塊urllib與urllib3,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
如何利用python在剪貼板上讀取/寫入數(shù)據(jù)
說起處理數(shù)據(jù)就離不開導入導出,而我們使用Pandas時候最常用的就是read_excel、read_csv了,下面這篇文章主要給大家介紹了關于如何利用python在剪貼板上讀取/寫入數(shù)據(jù)的相關資料,需要的朋友可以參考下2022-07-07
Python安裝Imaging報錯:The _imaging C module is not installed問題解決
這篇文章主要介紹了Python安裝Imaging報錯:The _imaging C module is not installed問題解決方法,原來是PIL庫的庫文件沒有加到系統(tǒng)中導致老是提示這個錯誤,需要的朋友可以參考下2014-08-08

