国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python制作數(shù)據(jù)導(dǎo)入導(dǎo)出工具

2019-11-25 17:06:31
字體:
供稿:網(wǎng)友

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 jsonimport osimport sysimport timeimport 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()

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 星子县| 大田县| 额济纳旗| 格尔木市| 平南县| 凤山市| 嵩明县| 尼木县| 屏东市| 兴安盟| 正阳县| 涞水县| 邵武市| 高州市| 隆子县| 韩城市| 大理市| 尼木县| 肃宁县| 江口县| 鹰潭市| 黄陵县| 铜陵市| 黔南| 临沂市| 焉耆| 乐东| 鹤岗市| 江安县| 白朗县| 卢龙县| 海兴县| 五峰| 安泽县| 房山区| 尚志市| 文化| 鄄城县| 怀集县| 牟定县| 曲阜市|