您的位置: 网站首页> 大家问> 当前文章

clickhouse接口clickhouse函数python

老董2020-04-24200围观,100赞

  clickhouse是一款牛逼的数据库,是战斗民族俄罗斯人发明的,而且目前没有完整的中文版文档,和普通数据库不同之处在于他是列式存储!python操作clickhouse也有对应的接口。安装用pip命令即可。

	  pip install clickhouse_driver

  纯客户端示例:

# -*- coding: utf-8 -*-
from clickhouse_driver import Client

client = Client('localhost')
client.execute('SHOW TABLES')
client.execute('DROP TABLE IF EXISTS test')
client.execute('CREATE TABLE test (x Int32) ENGINE = Memory')
client.execute(
         'INSERT INTO test (x) VALUES',
         [{'x': 100}]
     )
client.execute('INSERT INTO test (x) VALUES', [[200]])
client.execute(
         'INSERT INTO test (x) '
         'SELECT * FROM system.numbers LIMIT %(limit)s',
         {'limit': 3}
     )
client.execute('SELECT sum(x) FROM test')


  使用数据库API:

 from clickhouse_driver import connect

 
 conn = connect('clickhouse://localhost')
 cursor = conn.cursor()
 cursor.execute('SHOW TABLES')
 cursor.fetchall()
 cursor.execute('DROP TABLE IF EXISTS test')
 cursor.fetchall()
 cursor.execute('CREATE TABLE test (x Int32) ENGINE = Memory')
 cursor.fetchall()
 cursor.executemany(
        'INSERT INTO test (x) VALUES',
        [{'x': 100}]
    )
 cursor.rowcount
 cursor.executemany('INSERT INTO test (x) VALUES', [[200]])
 cursor.rowcount
 cursor.execute(
        'INSERT INTO test (x) '
        'SELECT * FROM system.numbers LIMIT %(limit)s',
        {'limit': 3}
    )
 cursor.rowcount
 cursor.execute('SELECT sum(x) FROM test')
 cursor.fetchall()



  参考文档:

  https://pypi.org/project/clickhouse-driver/

  https://github.com/mymarilyn/clickhouse-driver

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    clickhouse接口clickhouse函数python文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200