ES在之前的博客已有介紹,提供很多接口,本文介紹如何使用python批量導(dǎo)入。ES官網(wǎng)上有較多說(shuō)明文檔,仔細(xì)研究并結(jié)合搜索引擎應(yīng)該不難使用。
先給代碼
#coding=utf-8from datetime import datetimefrom elasticsearch import Elasticsearchfrom elasticsearch import helperses = Elasticsearch()actions = []f=open('index.txt')i=1for line in f: line = line.strip().split(' ') action={ "_index":"image", "_type":"imagetable", "_id":i, "_source":{ u"圖片名":line[0].decode('utf8'), u"來(lái)源":line[1].decode('utf8'), u"權(quán)威性":line[2].decode('utf8'), u"大小":line[3].decode('utf8'), u"質(zhì)量":line[4].decode('utf8'), u"類別":line[5].decode('utf8'), u"型號(hào)":line[6].decode('utf8'), u"國(guó)別":line[7].decode('utf8'), u"采集人":line[8].decode('utf8'), u"所屬部門(mén)":line[9].decode('utf8'), u"關(guān)鍵詞":line[10].decode('utf8'), u"訪問(wèn)權(quán)限":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)
每句話的含義還是很明顯的,這里需要說(shuō)幾點(diǎn),首先是index.txt是以u(píng)tf8編碼的,所以需要decode('utf8')轉(zhuǎn)換成unicode對(duì)象,并且“圖片名”前需要加u,否則ES會(huì)報(bào)錯(cuò)
導(dǎo)入的速度還是很快的,2000多條記錄每秒。
以上這篇python批量導(dǎo)入數(shù)據(jù)進(jìn)Elasticsearch的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選