start-linux.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import json
  2. from MyUtils.ConfigUtils import ConfigUtils
  3. from MyUtils.ZillionUtil import ZillionUtil
  4. import os,time
  5. import datetime
  6. #插入hbase
  7. def put_hbase(hbasedatabase,hbasetable,datas):
  8. for i in range(0, len(datas), 1000):
  9. dataranges = datas[i:i + 1000]
  10. zillionUtil.insert(hbasedatabase, hbasetable, dataranges)
  11. #删除hbase数据
  12. def remove_hbase(hbasedatabase,hbasetable,key):
  13. zillionUtil.remove(hbasedatabase, hbasetable, key)
  14. #删除hbase数据
  15. def delete_hbase(hbasedatabase,hbasetable):
  16. Criteria = {"project_id":"3301100002"}
  17. zillionUtil.delete(hbasedatabase, hbasetable, Criteria)
  18. #获取表主键
  19. def get_hbase_key(hbasedatabase, hbasetable):
  20. keys = zillionUtil.table_key(hbasedatabase, hbasetable)
  21. return keys
  22. datetimenow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  23. datetimenow_strp = datetime.datetime.strptime(datetimenow,"%Y-%m-%d %H:%M:%S")
  24. print(type(datetimenow_strp),datetimenow_strp)
  25. config = ConfigUtils("config.xml")
  26. url, hbasedatabase = config.readTop("metadata", ["url", "database"])
  27. sourcepath, targetpath = config.readTop("cmd", ["sourcepath", "targetpath"])
  28. #连接hbase
  29. zillionUtil = ZillionUtil(url)
  30. ##切换到输出目录,清空文件
  31. #os.chdir(targetpath)
  32. #os.system("rm -rf *")
  33. #切换到java程序工作目录,执行导出数据程序
  34. os.chdir(sourcepath)
  35. print("进入%s目录,开始执行java程序"%os.getcwd())
  36. cmd = "java -jar -Dfile.encoding=UTF-8 data-migration.jar"
  37. status = os.system(cmd)
  38. #如果程序执行成功
  39. if status == 0:
  40. print("导出程序执行成功")
  41. #切换到输出目录,判断文件是否是最新文件
  42. os.chdir(targetpath)
  43. print("进入%s目录,检查文件并写入hbase"%os.getcwd())
  44. list = os.listdir(os.getcwd())
  45. for i in list:
  46. print(i)
  47. file = i.lstrip("\\")
  48. cmd_mv = "mv \%s %s"%(i,file)
  49. print(cmd_mv)
  50. os.system(cmd_mv)
  51. updatetime = os.path.getmtime(file) #查询文件修改时间
  52. timeArray = time.localtime(updatetime)
  53. otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
  54. otherStyleTime_strp = datetime.datetime.strptime(otherStyleTime,"%Y-%m-%d %H:%M:%S")
  55. detal_time = (otherStyleTime_strp - datetimenow_strp).total_seconds()
  56. print("%s文件最新修改时间:%s"%(file,otherStyleTime))
  57. hbasetable = file.split(".")[0]
  58. if int(detal_time) < 10000:
  59. with open(file,"r",encoding = 'utf-8') as fp:
  60. if fp.name == "rel_btw_objs.json":
  61. datas_rel = []
  62. for line in fp.readlines():
  63. data = json.loads(line)
  64. datas_rel.append(data)
  65. print("删除%s"%fp.name)
  66. delete_hbase(hbasedatabase,hbasetable)
  67. put_hbase(hbasedatabase, hbasetable, datas_rel)
  68. else:
  69. keys = get_hbase_key(hbasedatabase, hbasetable)
  70. datas = []
  71. for line in fp.readlines():
  72. data = json.loads(line)
  73. hbase_key = {}
  74. for key in keys:
  75. if key in data:
  76. hbase_key[key] = data[key]
  77. datas.append(data)
  78. remove_hbase(hbasedatabase,hbasetable,hbase_key)
  79. time.sleep(0.1)
  80. put_hbase(hbasedatabase,hbasetable,datas)
  81. print("%s写入完成"%hbasetable)
  82. else:
  83. print("检查%s导出文件数据,可能不是最新数据"%file)
  84. print("数据写入完成")
  85. os.system("apt-get update&&apt -y install curl")
  86. os.system("curl http://data-platform-3:80/mng/clear_cache?secret=A123456")
  87. os.system("curl http://sagacloud-object-service:80/object/sync/AllData?projectId=Pj3301100002")
  88. print("数据刷新完成")
  89. else:
  90. print("执行java程序失败")