Python制作数据导入导出工具
作者:hebedich 发布时间:2023-07-25 06:59:59
标签:Python,数据导入导出
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()
以上所述就是本文的全部内容了,希望大家能够喜欢。


猜你喜欢
- unsafe 包func Alignof(x ArbitraryType) uintptrfunc Offsetof(x Arbitrary
- 1.下载mysql的repo源$ wget http://repo.mysql.com/mysql-community-release-el
- pandas可以非常方便的写数据到excel,那么如何写多个dataframe到不同的sheet呢?使用pandas.ExcelWriter
- 列表解析 在需要改变列表而不是需要新建某列表时,可以使用列表解析。列表解析表达式为: [expr for iter_var in itera
- 理论知识部分:一、简单总结几点数据库测试点:1.检查接口返回的数据是否与预期一致2.传递数据类型错误时能否处理,比如数据类型要求是整数,传递
- 我们知道可以将一个海量记录的 MySQL 大表根据主键、时间字段,条件字段等分成若干个表甚至保存在若干服务器中。 唯一的问题就是跨服务器批量
- 安装过程询问一般 y 就可以了1 安装1.1 下载wget https://dev.mysql.com/get/mysql-apt-conf
- 起源就在今年9月份,我负责的部门平台项目发布了一个新版本,该版本同时上线了一个新功能,简单说有点类似定时任务。头一天一切正常,但第二天出现了
- 学生成绩管理系统简介一个带有登录界面具有增减改查功能的学生成绩管理系统(面向对象思想,利用tkinter库进行制作,利用.txt文件进行存储
- MySQL的默认编码是Latin1,不支持中文,如何修改MySQL的默认编码呢,下面以gbk为例来说明(这里只介绍Windows环境下) 1
- 对于初学者来说,一份详尽又清晰明白的指南很重要。今天,猫猫跟大家一起,好好学习Python文件读写的内容,这部分内容特别常用,掌握后对工作和
- 描述super() 函数用于调用下一个父类(超类)并返回该父类实例的方法。super 是用来解决多重继承问题的,直接用类名调用父类方法在使用
- 1. dataloader() 初始化函数def __init__(self, dataset, batch_size=1, shuffle
- 类的代码: define('QR_MODE_NUL', -1); define('QR_MODE_NUM',
- 本文为大家分享了pygame游戏之旅的第3篇,供大家参考,具体内容如下载入car图片(我自己画的),需要用到pygame.image模块,定
- 以前做音乐项目的时候,最让我们头痛的就是满足用户的问题。在音乐的领域,不要试图去满足所有用户这个定律得到了最充分的验证。究其原因,无非是音乐
- 一、推理原理1.先去《英雄联盟》官网找到英雄及皮肤图片的网址:lol.qq.com2.从上面网址可以看到所有英雄都在,按下F12查看源代码,
- 本文实例讲述了PHP面向对象的特性。分享给大家供大家参考,具体如下:Demo1.php<?php header('
- 可以用函数 json.dumps()将 Python 对象编码转换为字符串形式。例如:import json python_obj = [[
- 本文记录了mysql 8.0.12 安装配置方法,供大家参考,具体内容如下1、从官网下载MySQL for Windows:https://