start_mysql.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import json
  2. from MyUtils.ZillionUtil import ZillionUtil
  3. import pymysql
  4. import datetime
  5. import argparse
  6. INSERT_SQL = "replace into %s.%s(building,meter,func_id,data_time,data_value,dt) values "
  7. # 获取项目点位
  8. def get_pointlist(hbase_database, hbase_table, buildingid):
  9. Criteria = {
  10. "building": buildingid
  11. }
  12. datas = zillionUtil.select(hbase_database, hbase_table, Criteria)
  13. pointlist = []
  14. for i in datas:
  15. point = []
  16. meter = i["meter"]
  17. funcid = i["funcid"]
  18. point.append(meter)
  19. point.append(funcid)
  20. pointlist.append(point)
  21. return pointlist
  22. # 获取能耗数据
  23. def get_data_time(hbase_database, hbase_table, buildingid, meter, funcid,from_time,to_time):
  24. Criteria = {
  25. "building": buildingid,
  26. "meter": meter,
  27. "funcid": funcid,
  28. "data_time":{
  29. "$gte": from_time,
  30. "$lt": to_time
  31. }
  32. }
  33. datas = zillionUtil.select(hbase_database, hbase_table, Criteria)
  34. return datas
  35. #取hbase数据,处理成sql语句
  36. def hbase_energy_data(points,table,building):
  37. sqls = []
  38. print("%s开始查询项目%s的%s至%s的数据" % (table, building, args.start_time, args.end_time))
  39. for i in points:
  40. meter,funcid = i[0],i[1]
  41. # print("%s开始查询项目%s的%s至%s的数据 %s-%s"%(table,building,args.start_time,args.end_time,meter,funcid))
  42. datas = get_data_time(hbase_database,table,building,meter,funcid,args.start_time,args.end_time)
  43. for i in datas:
  44. data_time = i["data_time"]
  45. data_value = i["data_value"]
  46. dt = data_time[0:4]+ "-" + data_time[4:6] + "-" + data_time[6:8]
  47. sqlline = "(%s,'%s',%s,%s,%s,'%s')" % (building, meter, funcid, data_time, data_value, dt)
  48. sqls.append(sqlline)
  49. return sqls
  50. # mysql插入数据
  51. def insert_mysql(sqls,building,my_table):
  52. print("开始往mysql插入%s的%s数据..."%(building,my_table))
  53. for i in range(0, len(sqls), 1000):
  54. sqlranges = sqls[i:i + 1000]
  55. sqlranges = INSERT_SQL % (my_database,my_table) + ",".join(sqlranges)
  56. mysql_cur.execute(sqlranges)
  57. conn.commit()
  58. print("mysql数据%s,项目%s插入成功,合计%s条..." % (my_table,building,len(sqls)))
  59. end_time = datetime.datetime.now().strftime("%Y%m%d") + "000000"
  60. start_time = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y%m%d") + "000000"
  61. parser = argparse.ArgumentParser()
  62. parser.add_argument("--start_time",default=start_time,help="--start_time 20220101000000")
  63. parser.add_argument("--end_time",default=end_time,help="--end_time 20220102000000")
  64. parser.add_argument("funcid",type=int,help="--funcid 10101")
  65. args = parser.parse_args()
  66. # print(args.start_time)
  67. # print(args.end_time)
  68. # print(args.funcid)
  69. # 读取配置文件信息
  70. with open("/mnt/datadisk0/saga/bi_saga/transfer_data/config.json", "r") as f:
  71. data = json.load(f)
  72. hbase_database = data["metadata"]["database"]
  73. url = data["metadata"]["url"]
  74. building_list = data["building"]["id"]
  75. print("项目列表:%s"%building_list)
  76. my_database = data["mysql"]["database"]
  77. my_fjd_table = data["mysql"]["fjd_table"]
  78. my_energy_table = data["mysql"]["energy_table"]
  79. my_co2_table = data["mysql"]["co2_table"]
  80. my_pm25_table = data["mysql"]["pm25_table"]
  81. my_temperature_table = data["mysql"]["temperature_table"]
  82. my_hcho_table = data["mysql"]["hcho_table"]
  83. my_humidity_table = data["mysql"]["humidity_table"]
  84. mysql = data["mysql"]
  85. del mysql["fjd_table"]
  86. del mysql["energy_table"]
  87. del mysql["co2_table"]
  88. del mysql["pm25_table"]
  89. del mysql["temperature_table"]
  90. del mysql["hcho_table"]
  91. del mysql["humidity_table"]
  92. tables = ["fjd_0_near_15min","data_servicedata_15min"]
  93. # #连接hbase
  94. zillionUtil = ZillionUtil(url)
  95. #连接mysql
  96. conn = pymysql.connect(**mysql)
  97. mysql_cur = conn.cursor()
  98. for building in building_list:
  99. #处理点位表
  100. pointlist = get_pointlist(hbase_database,"dy_pointlist",building)
  101. if pointlist:
  102. points = []
  103. for point in pointlist:
  104. i = point[1]
  105. if i == args.funcid:
  106. points.append(point)
  107. #电
  108. if args.funcid == 10101:
  109. # sqls = hbase_energy_data(points, "fjd_0_near_15min",building)
  110. # insert_mysql(sqls,building, my_fjd_table)
  111. sqls = hbase_energy_data(points, "data_servicedata_15min",building)
  112. insert_mysql(sqls,building, my_energy_table)
  113. #CO2
  114. if args.funcid == 11301:
  115. sqls = hbase_energy_data(points, "fjd_0_near_15min",building)
  116. insert_mysql(sqls,building, my_co2_table)
  117. #pm2.5
  118. if args.funcid == 11401:
  119. sqls = hbase_energy_data(points, "fjd_0_near_15min",building)
  120. insert_mysql(sqls,building, my_pm25_table)
  121. #甲醛
  122. if args.funcid == 11305:
  123. sqls = hbase_energy_data(points, "fjd_0_near_15min",building)
  124. insert_mysql(sqls,building, my_hcho_table)
  125. #温度
  126. if args.funcid == 11101:
  127. sqls = hbase_energy_data(points, "fjd_0_near_15min",building)
  128. insert_mysql(sqls,building, my_temperature_table)
  129. #湿度
  130. if args.funcid == 11201:
  131. sqls = hbase_energy_data(points, "fjd_0_near_15min",building)
  132. insert_mysql(sqls, building,my_humidity_table)
  133. else:
  134. print("未查询到项目%s的点位表..."%building)
  135. mysql_cur.close()
  136. conn.close()