Python制作數(shù)據(jù)導入導出工具
更新時間:2015年07月31日 14:57:44 投稿:hebedich
正好最近在學習python,于是打算用python實現(xiàn)了數(shù)據(jù)導入導出工具,由于是新手,所以寫的有些不完善的地方還請見諒
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將excel數(shù)據(jù)導入數(shù)據(jù)庫過程詳解
- 用Python將Excel數(shù)據(jù)導入到SQL Server的例子
- Python使用xlrd模塊操作Excel數(shù)據(jù)導入的方法
- python實現(xiàn)zencart產(chǎn)品數(shù)據(jù)導入到magento(python導入數(shù)據(jù))
- 淺談Python數(shù)學建模之線性規(guī)劃
- 了解一下python內建模塊collections
- Python進行統(tǒng)計建模
- python實現(xiàn)數(shù)據(jù)分析與建模
- Python創(chuàng)建模塊及模塊導入的方法
- 淺談Python數(shù)學建模之數(shù)據(jù)導入
相關文章
在linux下實現(xiàn) python 監(jiān)控usb設備信號
今天小編就為大家分享一篇在linux下實現(xiàn) python 監(jiān)控usb設備信號,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python和OpenCV庫實現(xiàn)識別人物出現(xiàn)并鎖定
本文主要介紹了Python和OpenCV庫實現(xiàn)識別人物出現(xiàn)并鎖定,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04OpenCV+face++實現(xiàn)實時人臉識別解鎖功能
這篇文章主要為大家詳細介紹了OpenCV+face++實現(xiàn)實時人臉識別解鎖功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08python正則表達式函數(shù)match()和search()的區(qū)別
match()和search()都是python中的正則匹配函數(shù),那這兩個函數(shù)有何區(qū)別呢?本文詳細介紹了這2個函數(shù)的區(qū)別2021-10-10