python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié)
更新時間:2022年05月17日 09:37:03 作者:挽手等風起
這篇文章主要介紹了python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
python連接clickhouse數(shù)據(jù)庫
在Python中獲取系統(tǒng)信息的一個好辦法是使用psutil這個第三方模塊。
顧名思義,psutil = process and system utilities,它不僅可以通過一兩行代碼實現(xiàn)系統(tǒng)監(jiān)控,還可以跨平臺使用。
主要針對clickhouse_driver的使用進行簡要介紹
第一步:
- 通過pip install clickhouse_driver 安裝 clickhouse_driver
第二步:
- 方法一:使用clickhouse_driver 包中的Client類,通過實例化一個客戶端進行對數(shù)據(jù)庫的增刪改查操作
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","網(wǎng)絡IO"] metric_name1 = ["讀(數(shù)量)","寫(數(shù)量)", "讀(字節(jié))", "寫(字節(jié))", "讀(時間)", "寫(時間)"] metric_name2 = ["發(fā)送字節(jié)數(shù)","接收字節(jié)數(shù)","發(fā)送包數(shù)","接收包"] 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 "網(wǎng)絡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("成功寫入數(shù)據(jù)") except Exception as e: print(str(e))
- 方法二:使用clickhouse_driver 包中的connect函數(shù),通過實例化一個客戶端進行對數(shù)據(jù)庫的增刪改查操作
from datetime import datetime import psutil from clickhouse_driver import connect host_name = '192.168.50.94' #賬號:密碼@主機名:端口號/數(shù)據(jù)庫 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","網(wǎng)絡IO"] metric_name1 = ["讀(數(shù)量)","寫(數(shù)量)", "讀(字節(jié))", "寫(字節(jié))", "讀(時間)", "寫(時間)"] metric_name2 = ["發(fā)送字節(jié)數(shù)","接收字節(jié)數(shù)","發(fā)送包數(shù)","接收包"] 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 "網(wǎng)絡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("成功寫入數(shù)據(jù)") except Exception as e: print(str(e))
python將數(shù)據(jù)寫入clickhouse
from clickhouse_driver import Client # connect ClickHouse client = Client(host= ,port= ,user= ,database= , password=) # 得到table1中查詢的數(shù)據(jù)導入table2中(database2中應該事先建立對應的table2表) query_ck_sql = """ SELECT * FROM database1.table1 WHERE date = today() """ # 導入數(shù)據(jù)到臨時表 try: # 導入數(shù)據(jù) 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)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運算
這篇文章主要介紹了python3 中的字符串(單引號、雙引號、三引號)以及字符串與數(shù)字的運算,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07Django的URLconf中使用缺省視圖參數(shù)的方法
這篇文章主要介紹了Django的URLconf中使用缺省視圖參數(shù)的方法,Django是最著名的Python的web開發(fā)框架,需要的朋友可以參考下2015-07-07將python打包的exe做成windows服務運行的流程步驟
將 Python 腳本打包的 exe 文件作為 Windows 服務運行,可以通過以下步驟實現(xiàn),Windows 服務是一種在后臺運行的程序,通常不需要用戶交互,本文給大家介紹了一個完整的指南,需要的朋友可以參考下2025-02-02Python使用Tkinter實現(xiàn)轉(zhuǎn)盤抽獎器的步驟詳解
這篇文章主要介紹了Python使用Tkinter實現(xiàn)轉(zhuǎn)盤抽獎器,,本文分場景通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01python數(shù)據(jù)分析近年比特幣價格漲幅趨勢分布
這篇文章主要為大家介紹了python分析近年來比特幣價格漲幅趨勢的數(shù)據(jù)分布,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11