[895]Clickhouse

时间:2022-07-25
本文章向大家介绍[895]Clickhouse,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、简介

Yandex在2016年6月15日开源了一个数据分析的数据库,名字叫做ClickHouse,这对保守俄罗斯人来说是个特大事。更让人惊讶的是,这个列式存储数据库的跑分要超过很多流行的商业MPP数据库软件,例如Vertica。如果你没有听过Vertica,那你一定听过 Michael Stonebraker,2014年图灵奖的获得者,PostgreSQL和Ingres发明者(Sybase和SQL Server都是继承Ingres而来的), Paradigm4和SciDB的创办者。Michael Stonebraker于2005年创办Vertica公司,后来该公司被HP收购,HP Vertica成为MPP列式存储商业数据库的高性能代表,Facebook就购买了Vertica数据用于用户行为分析。简单的说,ClickHouse作为分析型数据库,有三大特点:一是跑分快,二是功能多,三是文艺范

官网地址:https://clickhouse.tech/ 官方文档:https://clickhouse.tech/docs/zh/single/

Python接口1

ClickHouse没有官方的Python接口,有个第三方的库,叫clickhouse-driver,GitHub地址是:mymarilyn/clickhouse-driver: ClickHouse Python Driver with native interface support 安装:

pip install clickhouse-driver

使用方法如下:

from clickhouse_driver import Client

client = Client(host='localhost', database='default', user='default', password='')
client.execute('SHOW DATABASES')

==========================================================
>>> from clickhouse_driver import connect
>>>
>>> conn = connect('clickhouse://localhost')
>>> cursor = conn.cursor()
>>>
>>> cursor.execute('SHOW TABLES')
>>> cursor.fetchall()
[('test',)]

Python接口2

pip install clickhouse-sqlalchemy==0.1.4 
pip install sqlalchemy==1.3.19

使用

# -*- coding:utf-8 -*-
from clickhouse_sqlalchemy import make_session
from sqlalchemy import create_engine


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


def get_session(engine):
    return make_session(engine)

def execute(sql):
    session = get_session(engine)
    cursor = session.execute(sql)
    try:
        fields = cursor._metadata.keys
        return [dict(zip(fields, item)) for item in cursor.fetchall()]
    finally:
        cursor.close()
        session.close()

query='SHOW TABLES'
result=execute(query)
print(result)

其它阅读

ClickHouse表引擎到底怎么选: https://developer.aliyun.com/article/762461 clickHouse可视化查询工具: https://www.cnblogs.com/treesoft/p/11963831.html Tabix: https://clickhouse.tech/docs/zh/interfaces/third-party/gui/ https://zhuanlan.zhihu.com/p/161383473

参考: https://blog.csdn.net/m0_37739193/article/details/79611560 https://blog.csdn.net/zhangpeterx/article/details/95060788 https://testerhome.com/topics/21135