ElasticSearch 数据导入导出工具

  1. '''
  2. Export and Import ElasticSearch Data.
  3. Simple Example At __main__
  4. @author: [email protected]
  5. @note: uncheck consistency of data, please do it by self
  6. '''
  7.  
  8. import json
  9. import os
  10. import sys
  11. import time
  12. import urllib2
  13.  
  14. reload(sys)
  15. sys.setdefaultencoding('utf-8')# @UndefinedVariable
  16.  
  17. class exportEsData():
  18. size =10000
  19. def __init__(self, url,index,type):
  20. self.url = url+"/"+index+"/"+type+"/_search"
  21. self.index = index
  22. self.type = type
  23. def exportData(self):
  24. print("export data begin...")
  25. begin= time.time()
  26. try:
  27. os.remove(self.index+"_"+self.type+".json")
  28. except:
  29. os.mknod(self.index+"_"+self.type+".json")
  30. msg = urllib2.urlopen(self.url).read()
  31. print(msg)
  32. obj = json.loads(msg)
  33. num = obj["hits"]["total"]
  34. start =0
  35. end= num/self.size+1
  36. while(start<end):
  37. msg = urllib2.urlopen(self.url+"?from="+str(start*self.size)+"&size="+str(self.size)).read()
  38. self.writeFile(msg)
  39. start=start+1
  40. print("export data end!!!\n\t total consuming time:"+str(time.time()-begin)+"s")
  41. def writeFile(self,msg):
  42. obj = json.loads(msg)
  43. vals = obj["hits"]["hits"]
  44. try:
  45. f = open(self.index+"_"+self.type+".json","a")
  46. for val in vals:
  47. a = json.dumps(val["_source"],ensure_ascii=False)
  48. f.write(a+"\n")
  49. finally:
  50. f.flush()
  51. f.close()
  52.  
  53. class importEsData():
  54. def __init__(self,url,index,type):
  55. self.url = url+"/"+index+"/"+type
  56. self.index = index
  57. self.type = type
  58. def importData(self):
  59. print("import data begin...")
  60. begin= time.time()
  61. try:
  62. f = open(self.index+"_"+self.type+".json","r")
  63. for line in f:
  64. self.post(line)
  65. finally:
  66. f.close()
  67. print("import data end!!!\n\t total consuming time:"+str(time.time()-begin)+"s")
  68. def post(self,data):
  69. req = urllib2.Request(self.url,data,{"Content-Type":"application/json; charset=UTF-8"})
  70. urllib2.urlopen(req)
  71.  
  72. if __name__ =='__main__':
  73. '''
  74. Export Data
  75. e.g.
  76. URL index type
  77. exportEsData("http://10.100.142.60:9200","watchdog","mexception").exportData()
  78. export file name: watchdog_mexception.json
  79. '''
  80. #exportEsData("http://10.100.142.60:9200","watchdog","mexception").exportData()
  81. exportEsData("http://10.100.142.60:9200","watchdog","mexception").exportData()
  82. '''
  83. Import Data
  84. *import file name:watchdog_test.json (important)
  85. "_" front part represents the elasticsearch index
  86. "_" after part represents the elasticsearch type
  87. e.g.
  88. URL index type
  89. mportEsData("http://10.100.142.60:9200","watchdog","test").importData()
  90. '''
  91. #importEsData("http://10.100.142.60:9200","watchdog","test").importData()
  92. importEsData("http://10.100.142.60:9200","watchdog","test").importData()

猜你喜欢

转载自hugoren.iteye.com/blog/2271989