Python如何连接clickhouse,有哪些方式
Admin 2022-08-31 群英技术资讯 865 次浏览
在Python中获取系统信息的一个好办法是使用psutil这个第三方模块。
顾名思义,psutil = process and system utilities,它不仅可以通过一两行代码实现系统监控,还可以跨平台使用。
第一步:
第二步:
from clickhouse_driver import Client from datetime import datetime import psutil host_name = '192.168.50.94' client = Client(host=host_name,database='default',user='default',password='自己设的密码',send_receive_timeout=20,port=55666) now = datetime.now() time_stamp = now.strftime('%a %b %d %H:%M:%S CST %Y')# Tue Apr 06 15:32:55 CST 2021 <class 'str'> create_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S') disk_io = psutil.disk_io_counters() net_io = psutil.net_io_counters() chart_name = ["磁盘IO","网络IO"] metric_name1 = ["读(数量)","写(数量)", "读(字节)", "写(字节)", "读(时间)", "写(时间)"] metric_name2 = ["发送字节数","接收字节数","发送包数","接收包"] metric_value1 = [disk_io.read_count,disk_io.write_count,disk_io.read_bytes,disk_io.write_bytes,disk_io.read_time,disk_io.write_time] metric_value2 = [net_io.bytes_sent,net_io.bytes_recv,net_io.packets_sent,net_io.packets_recv] try: for i in chart_name: if i is "磁盘IO": for j in metric_name1: sql = "insert into clickhouse_host_metrics777(time_stamp,host_name, chart_name, metric_name,metric_value,create_at) " \ "values('%s','%s','%s','%s','%s','%s')" % \ (time_stamp, host_name, i, j, metric_value1[metric_name1.index(j)], create_at) res = client.execute(sql) elif i is "网络IO": for j in metric_name2: sql = "insert into clickhouse_host_metrics777(time_stamp,host_name, chart_name, metric_name,metric_value,create_at) " \ "values('%s','%s','%s','%s','%s','%s')" % \ (time_stamp, host_name, i, j, metric_value2[metric_name2.index(j)], create_at) res = client.execute(sql) print("成功写入数据") except Exception as e: print(str(e))
from datetime import datetime import psutil from clickhouse_driver import connect host_name = '192.168.50.94' #账号:密码@主机名:端口号/数据库 conn = connect('clickhouse://default:自己设的密码@'+host_name+':55666/default') cursor = conn.cursor() now = datetime.now() time_stamp = now.strftime('%a %b %d %H:%M:%S CST %Y')# Tue Apr 06 15:32:55 CST 2021 <class 'str'> create_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S') disk_io = psutil.disk_io_counters() net_io = psutil.net_io_counters() chart_name = ["磁盘IO","网络IO"] metric_name1 = ["读(数量)","写(数量)", "读(字节)", "写(字节)", "读(时间)", "写(时间)"] metric_name2 = ["发送字节数","接收字节数","发送包数","接收包"] metric_value1 = [disk_io.read_count,disk_io.write_count,disk_io.read_bytes,disk_io.write_bytes,disk_io.read_time,disk_io.write_time] metric_value2 = [net_io.bytes_sent,net_io.bytes_recv,net_io.packets_sent,net_io.packets_recv] try: for i in chart_name: if i is "磁盘IO": for j in metric_name1: sql = "insert into clickhouse_host_metrics777(time_stamp,host_name, chart_name, metric_name,metric_value,create_at) values('%s','%s','%s','%s','%s','%s')" % \ (time_stamp, host_name, i, j, metric_value1[metric_name1.index(j)], create_at) # res = client.execute(sql) res = cursor.execute(sql) elif i is "网络IO": for j in metric_name2: sql = "insert into clickhouse_host_metrics777(time_stamp,host_name, chart_name, metric_name,metric_value,create_at) values('%s','%s','%s','%s','%s','%s')" % \ (time_stamp, host_name, i, j, metric_value2[metric_name2.index(j)], create_at) res = cursor.execute(sql) cursor.close() print("成功写入数据") except Exception as e: print(str(e))
from clickhouse_driver import Client # connect ClickHouse client = Client(host= ,port= ,user= ,database= , password=) # 得到table1中查询的数据导入table2中(database2中应该事先建立对应的table2表) query_ck_sql = """ SELECT * FROM database1.table1 WHERE date = today() """ # 导入数据到临时表 try: # 导入数据 client.execute("insert into {official_table_db}.{official_all_table_name} \ {query_ck_sql}".format( official_table_db = database2, official_table_name = table2, query_ck_sql = query_ck_sql) ,types_check = True) except Exception as e: print str(e)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。本节主要介绍递归的基本概念以及如何构建递归程序。
这篇文章主要为大家详细介绍了如何利用Python语言实现邮件自动下载以及附件解析功能,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
这篇文章介绍了Python中的线程操作模块(oncurrent),文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了Python if 判断语句,包括流程控制,顺序结构和分支结构,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要为大家介绍了Python的合并字典,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008