最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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()

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

相關文章

  • Python使用chardet判斷字符編碼

    Python使用chardet判斷字符編碼

    這篇文章主要介紹了Python使用chardet判斷字符編碼的方法,較為詳細的分析了Python中chardet的功能、安裝及使用技巧,需要的朋友可以參考下
    2015-05-05
  • Django1.11自帶分頁器paginator的使用方法

    Django1.11自帶分頁器paginator的使用方法

    這篇文章主要為大家詳細介紹了Django1.11自帶分頁器Django的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • pytorch中torch.max和Tensor.view函數(shù)用法詳解

    pytorch中torch.max和Tensor.view函數(shù)用法詳解

    今天小編就為大家分享一篇pytorch中torch.max和Tensor.view函數(shù)用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python3.6.x中內(nèi)置函數(shù)總結(jié)及講解

    Python3.6.x中內(nèi)置函數(shù)總結(jié)及講解

    今天小編就為大家分享一篇關于Python3.6.x中內(nèi)置函數(shù)總結(jié)及講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • python 利用turtle模塊畫出沒有角的方格

    python 利用turtle模塊畫出沒有角的方格

    今天小編就為大家分享一篇python 利用turtle模塊畫出沒有角的方格,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python繪圖時,坐標軸負號顯示不出來的解決

    python繪圖時,坐標軸負號顯示不出來的解決

    這篇文章主要介紹了python繪圖時,坐標軸負號顯示不出來的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python幫你微信頭像任意添加裝飾別再@微信官方了

    Python幫你微信頭像任意添加裝飾別再@微信官方了

    昨天朋友圈刷爆了@微信官方的梗,從起初的為頭像添加國旗,到最后的各種Book思議的需求…而我呢?下面跟隨小編一起學習Python幫你微信頭像任意添加裝飾別再@微信官方了,感興趣的朋友一起看看吧
    2019-09-09
  • 使用Python爬取網(wǎng)頁中隱藏的div內(nèi)容

    使用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
  • Python實現(xiàn)自動生成請假條

    Python實現(xiàn)自動生成請假條

    這篇文章主要為大家詳細介紹了如何實現(xiàn)用Python來批量生成請假條,這回既學了東西又做了事情,兩不誤!感興趣的小伙伴可以跟隨小編一起學習一下
    2022-12-12
  • 基于PyQt5制作一個截圖翻譯工具

    基于PyQt5制作一個截圖翻譯工具

    這篇文章主要為大家介紹了如何利用PyQt5制作一個簡單的截圖翻譯工具,具有截圖功能、翻譯功能和文字識別OCR,需要的可以參考一下
    2022-05-05

最新評論

浦县| 尼玛县| 图们市| 敦煌市| 将乐县| 鄂州市| 安阳市| 丹江口市| 靖西县| 扶绥县| 东安县| 绩溪县| 托克托县| 郸城县| 瓦房店市| 厦门市| 大连市| 海兴县| 临武县| 长阳| 英德市| 太和县| 米易县| 任丘市| 安丘市| 亳州市| 邓州市| 德保县| 桃园县| 察哈| 读书| 高平市| 广东省| 株洲县| 石城县| 青岛市| 禹州市| 威信县| 小金县| 甘孜县| 赣州市|