Python制作数据导入导出工具
作者:hebedich 时间:2023-07-25 06:59:59
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()
以上所述就是本文的全部内容了,希望大家能够喜欢。
标签:Python,数据导入导出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
参数传递解决window.open的session变量丢失
2007-10-22 17:40:00
SQL 中主标识列IDENTITY使用技巧
2011-06-02 08:47:00
QQ在线客服网页代码大全
2008-01-17 18:28:00
![](https://img.aspxhome.com/file/UploadPic/20081/17/2008117205323775s.gif)
HTML的优化杂记
2010-03-10 10:39:00
ASP脚本变量、函数、过程和条件语句
2008-10-14 14:43:00
![](https://img.aspxhome.com/file/UploadPic/200812/2008121313306289.gif)
对网页切片算法问题的研究
2008-06-05 12:41:00
了解CSS的查找匹配原理,让CSS更简洁、高效
2010-06-08 13:36:00
了解WEB页面工具语言XML(一)产生背景
2008-09-05 17:18:00
iframe全跨域高度自适应解决方案
2008-12-21 16:16:00
![](https://img.aspxhome.com/file/UploadPic/200812/21/cross_domain_iframe-43s.jpg)
原来CSS也可以把IE6弄死
2007-08-14 09:30:00
形成视觉冲击的几种方式
2008-08-03 15:57:00
![](https://img.aspxhome.com/file/UploadPic/20088/3/200883164413717s.jpg)
交互设计师心得——核心竞争力
2010-01-19 13:45:00
![](https://img.aspxhome.com/file/UploadPic/20101/19/126eb492886g215-16s.jpg)
试了下Golang实现try catch的方法
2023-07-21 20:49:59
网页颜色变黑白CSS代码 适合所有类型网站
2010-04-21 10:37:00
ASP防止图片木马上传的代码
2011-02-05 11:08:00
python中的global关键字的使用方法
2023-07-15 13:26:50
设计稿标注首屏线的确定始末
2011-03-30 12:36:00
JSP 注释的详解及简单实例
2023-07-21 06:15:35
![](https://img.aspxhome.com/file/2023/1/97891_0s.png)
关于浏览器的一些观点
2008-08-06 12:48:00
![](https://img.aspxhome.com/file/UploadPic/20088/6/200886125027461s.gif)
IE中不可遍历的属性
2010-01-19 13:47:00