In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what are the ways in which python connects to clickhouse database". In daily operation, I believe many people have doubts about the way python connects to clickhouse database. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about how python connects to clickhouse database. Next, please follow the editor to study!
Python connects to clickhouse database
A good way to get system information in Python is to use psutil, a third-party module.
As the name implies, psutil = process and system utilities, it can not only be monitored by one or two lines of code, but can also be used across platforms.
This paper mainly briefly introduces the use of clickhouse_driver.
Step one:
Install clickhouse_driver through pip install clickhouse_driver
Step 2:
Method 1: use the Client class in the clickhouse_driver package to instantiate a client to add, delete, modify and query the database
From clickhouse_driver import Clientfrom datetimeimport datetimeimport psutilhost_name = '192.168.50.94'client = Client (password set by host=host_name,database='default',user='default',password=' itself', 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 create_at = datetime.now (). Strftime ('% Y-%m-%d% H15% MRV% S') disk_io = psutil.disk_io_counters () net_io = psutil.net_io_counters () chart_name = ["disk IO" "Network IO"] metric_name1 = ["read (quantity)", "write (number)", "read (byte)", "write (byte)", "read (time)", "write (time)"] metric_name2 = ["number of bytes sent", "number of bytes received", "number of packets sent", "received packets"] 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 "disk 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 ('% host_name, I, j, metric_ value1 [metric _ name1.index (j)] Create_at) res = client.execute (sql) elif i is "Network 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 ('% Stripe Magi% sai Magi% sai Mei Meng% s' '% s') "%\ (time_stamp, host_name, I, j, metric_ value2 [metric _ name2.index (j)], create_at) res = client.execute (sql) print (" successfully write data ") except Exception as e: print (str (e))
Method 2: use the connect function in the clickhouse_driver package to instantiate a client to add, delete, modify and query the database
From datetimeimport datetimeimport psutilfrom clickhouse_driver import connecthost_name = '192.168.50.94 account # account: password @ hostname: Port number / database conn = connect (' clickhouse://default: self-set password @'+ 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 create_at = datetime.now (). Strftime ('% Y-%m-%d% H15% MRO% S') disk_io = psutil.disk_io_counters () net_io = psutil.net_io_counters () chart_name = ["disk IO" "Network IO"] metric_name1 = ["read (quantity)", "write (number)", "read (byte)", "write (byte)", "read (time)", "write (time)"] metric_name2 = ["number of bytes sent", "number of bytes received", "number of packets sent", "received packets"] 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 "disk 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 ('% host_name, I, j, metric_ value1 [metric _ name1.index (j)] Create_at) # res = client.execute (sql) res = cursor.execute (sql) elif i is "Network 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 (time_stamp, host_name, I, j, metric_ value2 [metric _ name2.index (j)], create_at) res = cursor.execute (sql) cursor.close () print ("successfully write data") except Exception as e: print (str (e)) python writes data to clickhousefrom clickhouse_driver import Client # connect ClickHouseclient = Client (host=, port=, user=) Database= Password=) # Import the data queried in table1 into table2 (the corresponding table2 table should be established in advance in database2) query_ck_sql = "SELECT * FROM database1.table1 WHERE date = today ()" # Import data into temporary table try: # Import data 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) so far The study on "how python connects to the clickhouse database" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 272
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.