merge_excel.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from clickhouse_driver import Client
  2. import time
  3. import os
  4. import clickhouse_connect
  5. from dotenv import load_dotenv
  6. load_dotenv()
  7. # SQL查询语句
  8. SQL_QUERY = """
  9. select g.code, g.name, cl.`code`, cl.name, sea.name, cat.name, sg.sales_price
  10. from rbp.rbp_sales_order_bill_size as ss
  11. left join rbp.rbp_goods g on ss.goods_id = g.id
  12. left join rbp.rbp_color cl on ss.color_id = cl.id
  13. left join rbp.rbp_sales_order_bill_goods sg on sg.goods_id = g.id
  14. LEFT JOIN rbp.rbp_season sea on g.season_id = toInt64(sea.id)
  15. LEFT JOIN rbp.rbp_category cat on g.category_id = toInt64(cat.id)
  16. where ss.goods_id = 2586599671001600 and ss.color_id = 2055025368795724 limit 0, 1;
  17. """
  18. # 查询ClickHouse数据库
  19. def query_clickhouse(sql_query, max_retries=3):
  20. """
  21. 连接ClickHouse数据库并执行查询
  22. """
  23. for attempt in range(max_retries):
  24. try:
  25. print(type(os.getenv("CONNECT_TIMEOUT")))
  26. # 创建ClickHouse客户端连接
  27. client = clickhouse_connect.get_client(
  28. host=os.getenv('HOST'),
  29. port=int(os.getenv('PORT')),
  30. user=os.getenv('USER'),
  31. password=os.getenv('PASSWORD'),
  32. database=os.getenv('DATABASE'),
  33. connect_timeout=int(os.getenv('CONNECT_TIMEOUT')),
  34. send_receive_timeout=int(os.getenv('SEND_RECEIVE_TIMEOUT'))
  35. )
  36. print(f"第{attempt+1}次尝试连接成功")
  37. # 执行查询语句
  38. result = client.command(sql_query)
  39. return result
  40. except Exception as e:
  41. print(f"第{attempt+1}次尝试连接失败: {str(e)}")
  42. if attempt < max_retries - 1:
  43. print(f"Waiting 2 seconds before retrying...")
  44. time.sleep(2)
  45. else:
  46. print(f"经过{max_retries}次尝试后仍然无法连接到数据库")
  47. return None
  48. finally:
  49. # 关闭连接
  50. if 'client' in locals():
  51. try:
  52. client.disconnect()
  53. print("数据库连接已关闭")
  54. except:
  55. pass
  56. # 执行查询
  57. if __name__ == "__main__":
  58. query_result = query_clickhouse(SQL_QUERY)
  59. print(query_result)