Python制作數(shù)據(jù)導(dǎo)入導(dǎo)出工具
更新時間:2015年07月31日 14:57:44 投稿:hebedich
正好最近在學(xué)習(xí)python,于是打算用python實現(xiàn)了數(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()
以上所述就是本文的全部內(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實現(xiàn)zencart產(chǎn)品數(shù)據(jù)導(dǎo)入到magento(python導(dǎo)入數(shù)據(jù))
- 淺談Python數(shù)學(xué)建模之線性規(guī)劃
- 了解一下python內(nèi)建模塊collections
- Python進行統(tǒng)計建模
- python實現(xiàn)數(shù)據(jù)分析與建模
- Python創(chuàng)建模塊及模塊導(dǎo)入的方法
- 淺談Python數(shù)學(xué)建模之?dāng)?shù)據(jù)導(dǎo)入
相關(guān)文章
pytorch中torch.max和Tensor.view函數(shù)用法詳解
今天小編就為大家分享一篇pytorch中torch.max和Tensor.view函數(shù)用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python3.6.x中內(nèi)置函數(shù)總結(jié)及講解
今天小編就為大家分享一篇關(guān)于Python3.6.x中內(nèi)置函數(shù)總結(jié)及講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
使用Python爬取網(wǎng)頁中隱藏的div內(nèi)容
在這個信息爆炸的時代,互聯(lián)網(wǎng)上的數(shù)據(jù)無時無刻不在增長,許多網(wǎng)頁為了提升用戶體驗或保護數(shù)據(jù),會將部分內(nèi)容默認隱藏起來,只有在特定條件下才會顯示,所以本文將詳細介紹如何使用Python爬取這些隱藏的div內(nèi)容,需要的朋友可以參考下2025-03-03

