Python制作數(shù)據(jù)導(dǎo)入導(dǎo)出工具
python 2.6編寫,自己瞎寫的,備用
'''
Export and Import ElasticSearch Data.
Simple Example At __main__
@author: wgzh159@163.com
@note: uncheck consistency of data, please do it by self
'''
import json
import os
import sys
import time
import urllib2
reload(sys)
sys.setdefaultencoding('utf-8') # @UndefinedVariable
class exportEsData():
size = 10000
def __init__(self, url,index,type):
self.url = url+"/"+index+"/"+type+"/_search"
self.index = index
self.type = type
def exportData(self):
print("export data begin...")
begin = time.time()
try:
os.remove(self.index+"_"+self.type+".json")
except:
os.mknod(self.index+"_"+self.type+".json")
msg = urllib2.urlopen(self.url).read()
print(msg)
obj = json.loads(msg)
num = obj["hits"]["total"]
start = 0
end = num/self.size+1
while(start<end):
msg = urllib2.urlopen(self.url+"?from="+str(start*self.size)+"&size="+str(self.size)).read()
self.writeFile(msg)
start=start+1
print("export data end!!!\n\t total consuming time:"+str(time.time()-begin)+"s")
def writeFile(self,msg):
obj = json.loads(msg)
vals = obj["hits"]["hits"]
try:
f = open(self.index+"_"+self.type+".json","a")
for val in vals:
a = json.dumps(val["_source"],ensure_ascii=False)
f.write(a+"\n")
finally:
f.flush()
f.close()
class importEsData():
def __init__(self,url,index,type):
self.url = url+"/"+index+"/"+type
self.index = index
self.type = type
def importData(self):
print("import data begin...")
begin = time.time()
try:
f = open(self.index+"_"+self.type+".json","r")
for line in f:
self.post(line)
finally:
f.close()
print("import data end!!!\n\t total consuming time:"+str(time.time()-begin)+"s")
def post(self,data):
req = urllib2.Request(self.url,data,{"Content-Type":"application/json; charset=UTF-8"})
urllib2.urlopen(req)
if __name__ == '__main__':
'''
Export Data
e.g.
URL index type
exportEsData("http://10.100.142.60:9200","watchdog","mexception").exportData()
export file name: watchdog_mexception.json
'''
#exportEsData("http://10.100.142.60:9200","watchdog","mexception").exportData()
exportEsData("http://10.100.142.60:9200","watchdog","mexception").exportData()
'''
Import Data
*import file name:watchdog_test.json (important)
"_" front part represents the elasticsearch index
"_" after part represents the elasticsearch type
e.g.
URL index type
mportEsData("http://10.100.142.60:9200","watchdog","test").importData()
'''
#importEsData("http://10.100.142.60:9200","watchdog","test").importData()
importEsData("http://10.100.142.60:9200","watchdog","test").importData()
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- 使用python將excel數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫過程詳解
- 用Python將Excel數(shù)據(jù)導(dǎo)入到SQL Server的例子
- Python使用xlrd模塊操作Excel數(shù)據(jù)導(dǎo)入的方法
- python實(shí)現(xiàn)zencart產(chǎn)品數(shù)據(jù)導(dǎo)入到magento(python導(dǎo)入數(shù)據(jù))
- 淺談Python數(shù)學(xué)建模之線性規(guī)劃
- 了解一下python內(nèi)建模塊collections
- Python進(jìn)行統(tǒng)計(jì)建模
- python實(shí)現(xiàn)數(shù)據(jù)分析與建模
- Python創(chuàng)建模塊及模塊導(dǎo)入的方法
- 淺談Python數(shù)學(xué)建模之?dāng)?shù)據(jù)導(dǎo)入
相關(guān)文章
Python實(shí)現(xiàn)向好友發(fā)送微信消息
利用python可以實(shí)現(xiàn)微信消息發(fā)送功能,怎么實(shí)現(xiàn)呢?你肯定會(huì)想著很復(fù)雜,但是python的好處就是很多人已經(jīng)把接口打包做好了,只需要調(diào)用即可,今天通過本文給大家分享使用?Python?實(shí)現(xiàn)微信消息發(fā)送的思路代碼,一起看看吧2022-06-06
在linux下實(shí)現(xiàn) python 監(jiān)控usb設(shè)備信號(hào)
今天小編就為大家分享一篇在linux下實(shí)現(xiàn) python 監(jiān)控usb設(shè)備信號(hào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python和OpenCV庫實(shí)現(xiàn)識(shí)別人物出現(xiàn)并鎖定
本文主要介紹了Python和OpenCV庫實(shí)現(xiàn)識(shí)別人物出現(xiàn)并鎖定,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
OpenCV+face++實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別解鎖功能
這篇文章主要為大家詳細(xì)介紹了OpenCV+face++實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別解鎖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Python發(fā)送http請(qǐng)求解析返回json的實(shí)例
下面小編就為大家分享一篇Python發(fā)送http請(qǐng)求解析返回json的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
python實(shí)現(xiàn)自冪數(shù)的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)自冪數(shù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
python中對(duì)二維列表中一維列表的調(diào)用方法
在本文里小編給大家整理的是關(guān)于python中對(duì)二維列表中一維列表的調(diào)用方法,正在學(xué)習(xí)的朋友們可以參考下。2020-06-06
python正則表達(dá)式函數(shù)match()和search()的區(qū)別
match()和search()都是python中的正則匹配函數(shù),那這兩個(gè)函數(shù)有何區(qū)別呢?本文詳細(xì)介紹了這2個(gè)函數(shù)的區(qū)別2021-10-10
詳解Django中Request對(duì)象的相關(guān)用法
這篇文章主要介紹了詳解Django中Request對(duì)象的相關(guān)用法,Django是Python重多人氣框架中最著名的一個(gè),需要的朋友可以參考下2015-07-07

