python连接clickhouse

步骤一:安装包

pip install clickhouse-driver
pip install clickhouse_sqlalchemy
pip install sqlalchemy

步骤二:连接

from clickhouse_sqlalchemy import make_session
from sqlalchemy import create_engine
import pandas as pd

conf = {
    "user": "ck",
    "password": "Eitmpou9",
    "server_host": "192.168.1.142",
    "port": "8123",
    "db": "szyx"
}

connection = 'clickhouse://{user}:{password}@{server_host}:{port}/{db}'.format(**conf)
engine = create_engine(connection, pool_size=100, pool_recycle=3600, pool_timeout=20)

sql = 'select * from dw_task_visit_log  limit 10'

session = make_session(engine)
cursor = session.execute(sql)
try:
    fields = cursor._metadata.keys
    df = pd.DataFrame([dict(zip(fields, item)) for item in cursor.fetchall()])
finally:
    cursor.close()
    session.close()
python连接clickhouse_第1张图片

你可能感兴趣的:(Clickhouse,clickhouse)