Python制作数据导入导出工具
python2.6编写,自己瞎写的,备用
'''
ExportandImportElasticSearchData.
SimpleExampleAt__main__
@author:wgzh159@163.com
@note:uncheckconsistencyofdata,pleasedoitbyself
'''
importjson
importos
importsys
importtime
importurllib2
reload(sys)
sys.setdefaultencoding('utf-8')#@UndefinedVariable
classexportEsData():
size=10000
def__init__(self,url,index,type):
self.url=url+"/"+index+"/"+type+"/_search"
self.index=index
self.type=type
defexportData(self):
print("exportdatabegin...")
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("exportdataend!!!\n\ttotalconsumingtime:"+str(time.time()-begin)+"s")
defwriteFile(self,msg):
obj=json.loads(msg)
vals=obj["hits"]["hits"]
try:
f=open(self.index+"_"+self.type+".json","a")
forvalinvals:
a=json.dumps(val["_source"],ensure_ascii=False)
f.write(a+"\n")
finally:
f.flush()
f.close()
classimportEsData():
def__init__(self,url,index,type):
self.url=url+"/"+index+"/"+type
self.index=index
self.type=type
defimportData(self):
print("importdatabegin...")
begin=time.time()
try:
f=open(self.index+"_"+self.type+".json","r")
forlineinf:
self.post(line)
finally:
f.close()
print("importdataend!!!\n\ttotalconsumingtime:"+str(time.time()-begin)+"s")
defpost(self,data):
req=urllib2.Request(self.url,data,{"Content-Type":"application/json;charset=UTF-8"})
urllib2.urlopen(req)
if__name__=='__main__':
'''
ExportData
e.g.
URLindextype
exportEsData("http://10.100.142.60:9200","watchdog","mexception").exportData()
exportfilename:watchdog_mexception.json
'''
#exportEsData("http://10.100.142.60:9200","watchdog","mexception").exportData()
exportEsData("http://10.100.142.60:9200","watchdog","mexception").exportData()
'''
ImportData
*importfilename:watchdog_test.json(important)
"_"frontpartrepresentstheelasticsearchindex
"_"afterpartrepresentstheelasticsearchtype
e.g.
URLindextype
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()
以上所述就是本文的全部内容了,希望大家能够喜欢。