狂自私

导航

python连接clickhouse数据库

from clickhouse_sqlalchemy import make_session
from sqlalchemy import create_engine
from sqlalchemy.sql import text
import urllib.parse
user='default'
pw=urllib.parse.quote_plus('Lovedan@971220')
database='default'
addrs=['192.168.51.2:8123','192.168.51.3:8123']
for addr in addrs:
    connection = f"clickhouse://{user}:{pw}@{addr}/{database}"
    engine= create_engine(connection,pool_size=100,pool_recycle=3600,pool_timeout=10)
    clickhouse_cursor = make_session(engine=engine)
    sql='show tables;'
    query_result = clickhouse_cursor.execute(text(clickhouse_sql_str))
    try:
        fields = query_result._metadata.keys  #字段名
        print(fields._keys)
        for item in query_result.fetchall():  #具体内容
            print(item)
    finally:
        query_result.close()
        clickhouse_cursor.close()

 

posted on 2024-03-20 14:53  狂自私  阅读(495)  评论(0编辑  收藏  举报