DataBaseUtils.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """
  2. @desc 数据库连接
  3. @auth chenkai
  4. @date 2020/11/19
  5. """
  6. from model.DataBaseOperation import MysqlOperation
  7. import yaml
  8. import os
  9. import pandas as pd
  10. from clickhouse_driver.client import Client
  11. import logging
  12. def db_config():
  13. p_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
  14. path = os.path.join(p_path, "config", "db_config.yaml")
  15. f = open(path, encoding="utf-8")
  16. config = yaml.load(f.read(), Loader=yaml.FullLoader)
  17. return config
  18. class MysqlUtils:
  19. _quchen_text = None
  20. _zx = None
  21. _zx_ads = None
  22. _dm = None
  23. _zx_test = None
  24. def __init__(self):
  25. self.config = db_config()
  26. @property
  27. def quchen_text(self):
  28. conf = self.config['quchen_text']
  29. self._quchen_text = MysqlOperation(host=conf['host'],
  30. user=conf['user'],
  31. passwd=conf['passwd'],
  32. db=conf['db'])
  33. return self._quchen_text
  34. @property
  35. def zx(self):
  36. conf = self.config['zx']
  37. self._zx = MysqlOperation(host=conf['host'],
  38. user=conf['user'],
  39. passwd=conf['passwd'],
  40. db=conf['db'])
  41. return self._zx
  42. @property
  43. def zx_platform(self):
  44. conf = self.config['zx_platform']
  45. self._zx = MysqlOperation(host=conf['host'],
  46. user=conf['user'],
  47. passwd=conf['passwd'],
  48. db=conf['db'])
  49. return self._zx
  50. @property
  51. def zx_ads(self):
  52. conf = self.config['zx_ads']
  53. self._zx_ads = MysqlOperation(host=conf['host'],
  54. user=conf['user'],
  55. passwd=conf['passwd'],
  56. db=conf['db'])
  57. return self._zx_ads
  58. @property
  59. def zx_test(self):
  60. conf = self.config['zx_test']
  61. self._zx_test = MysqlOperation(host=conf['host'],
  62. user=conf['user'],
  63. passwd=conf['passwd'],
  64. db=conf['db'])
  65. return self._zx_test
  66. @property
  67. def dm(self):
  68. conf = self.config['dm']
  69. self._dm = MysqlOperation(host=conf['host'],
  70. user=conf['user'],
  71. passwd=conf['passwd'],
  72. db=conf['db'])
  73. return self._dm
  74. def find_db(self, db):
  75. if db == "quchen_text":
  76. self._quchen_text = self._quchen_text
  77. return self._quchen_text
  78. else:
  79. logging.info("输入数据库有误")
  80. def close(self):
  81. if self._quchen_text:
  82. self._quchen_text.cursor.close()
  83. self._quchen_text.conn.close()
  84. if self._zx:
  85. self._zx.cursor.close()
  86. self._zx.conn.close()
  87. if self._dm:
  88. self._dm.cursor.close()
  89. self._dm.conn.close()
  90. if self._zx_test:
  91. self._zx_test.cursor.close()
  92. self._zx_test.conn.close()
  93. if self._zx_ads:
  94. self._zx_ads.cursor.close()
  95. self._zx_ads.conn.close()
  96. class CkUtils:
  97. def __init__(self):
  98. self.config = db_config()
  99. conf = self.config['clickhouse']
  100. self.client = Client(host=conf['host'],
  101. user=conf['user'],
  102. password=conf['passwd'],
  103. port=conf['port'],
  104. send_receive_timeout=5)
  105. def execute(self, sql):
  106. return self.client.execute(sql)
  107. def getData_pd(self, sql, col):
  108. """
  109. :param sql:
  110. :param col: ['a','b']
  111. :return:
  112. """
  113. data = self.execute(sql)
  114. df = pd.DataFrame(data, columns=col)
  115. return df
  116. def getData_pdv2(self, sql):
  117. data = self.client.execute_iter(sql, with_column_types=True)
  118. columns = [column[0] for column in next(data)]
  119. df = pd.DataFrame.from_records(data, columns=columns)
  120. return df
  121. def getData_json(self, sql):
  122. return self.getData_pdv2(sql).to_json(orient='records')
  123. def getColumns(self, table, is_list=False):
  124. data = self.execute("desc " + table)
  125. li = []
  126. str = ''
  127. for i in data:
  128. li.append(i[0])
  129. str += i[0] + ','
  130. if is_list:
  131. return li
  132. else:
  133. return str[:-1]
  134. def insertMany(self, table, col, data):
  135. """
  136. :param table: 表名 srt
  137. :param col: 字段名 srt eg: ”a,b,c“
  138. :param data: tuple/list
  139. :return:
  140. """
  141. max = 1000
  142. sql = "insert into {} ({}) values ".format(table, col)
  143. if len(data) == 0:
  144. logging.info("data.len==0")
  145. return
  146. if len(data) <= max:
  147. sql = sql + str(data)[1:-1]
  148. # log.info(sql)
  149. # log.info("insert {} rows".format(len(data)))
  150. self.execute(sql)
  151. return
  152. else:
  153. sql2 = sql + str(data[:max])[1:-1]
  154. # log.info(sql2)
  155. self.execute(sql2)
  156. # log.info("insert {} rows".format(max))
  157. self.insertMany(table, col, data[max:])
  158. if __name__ == '__main__':
  159. # p_path = os.path.dirname(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
  160. # print(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
  161. ck = CkUtils()
  162. print(ck.execute("desc order"))