您好,登錄后才能下訂單哦!
這篇文章主要介紹“python連接clickhouse數(shù)據(jù)庫(kù)的方式有哪些”,在日常操作中,相信很多人在python連接clickhouse數(shù)據(jù)庫(kù)的方式有哪些問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”python連接clickhouse數(shù)據(jù)庫(kù)的方式有哪些”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
在Python中獲取系統(tǒng)信息的一個(gè)好辦法是使用psutil這個(gè)第三方模塊。
顧名思義,psutil = process and system utilities,它不僅可以通過(guò)一兩行代碼實(shí)現(xiàn)系統(tǒng)監(jiān)控,還可以跨平臺(tái)使用。
第一步:
通過(guò)pip install clickhouse_driver 安裝 clickhouse_driver
第二步:
方法一:使用clickhouse_driver 包中的Client類(lèi),通過(guò)實(shí)例化一個(gè)客戶(hù)端進(jìn)行對(duì)數(shù)據(jù)庫(kù)的增刪改查操作
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='自己設(shè)的密碼',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 = ["磁盤(pán)IO","網(wǎng)絡(luò)IO"] metric_name1 = ["讀(數(shù)量)","寫(xiě)(數(shù)量)", "讀(字節(jié))", "寫(xiě)(字節(jié))", "讀(時(shí)間)", "寫(xiě)(時(shí)間)"] 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 "磁盤(pán)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)絡(luò)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("成功寫(xiě)入數(shù)據(jù)") except Exception as e: print(str(e))
方法二:使用clickhouse_driver 包中的connect函數(shù),通過(guò)實(shí)例化一個(gè)客戶(hù)端進(jìn)行對(duì)數(shù)據(jù)庫(kù)的增刪改查操作
from datetime import datetime import psutil from clickhouse_driver import connect host_name = '192.168.50.94' #賬號(hào):密碼@主機(jī)名:端口號(hào)/數(shù)據(jù)庫(kù) conn = connect('clickhouse://default:自己設(shè)的密碼@'+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 = ["磁盤(pán)IO","網(wǎng)絡(luò)IO"] metric_name1 = ["讀(數(shù)量)","寫(xiě)(數(shù)量)", "讀(字節(jié))", "寫(xiě)(字節(jié))", "讀(時(shí)間)", "寫(xiě)(時(shí)間)"] 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 "磁盤(pán)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)絡(luò)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("成功寫(xiě)入數(shù)據(jù)") except Exception as e: print(str(e))
from clickhouse_driver import Client # connect ClickHouse client = Client(host= ,port= ,user= ,database= , password=) # 得到table1中查詢(xún)的數(shù)據(jù)導(dǎo)入table2中(database2中應(yīng)該事先建立對(duì)應(yīng)的table2表) query_ck_sql = """ SELECT * FROM database1.table1 WHERE date = today() """ # 導(dǎo)入數(shù)據(jù)到臨時(shí)表 try: # 導(dǎo)入數(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)
到此,關(guān)于“python連接clickhouse數(shù)據(jù)庫(kù)的方式有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。