zoukankan      html  css  js  c++  java
  • clickhouse之python操作

    官网:https://clickhouse-driver.readthedocs.io/en/latest/

    使用python来对clickhouse进行操作

    安装)

    pip install clickhouse-driver

    1.clickhouse-driver (mymarilyn/clickhouse-driver: ClickHouse Python Driver with native interface support (github.com))

    纯客户端:

    >>> from clickhouse_driver import Client
    >>>
    >>> client = Client('localhost')
    >>>
    >>> client.execute('SHOW TABLES')
    [('test',)]
    >>> 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}]
    ... )
    1
    >>> client.execute('INSERT INTO test (x) VALUES', [[200]])
    1
    >>> client.execute(
    ...     'INSERT INTO test (x) '
    ...     'SELECT * FROM system.numbers LIMIT %(limit)s',
    ...     {'limit': 3}
    ... )
    []
    >>> client.execute('SELECT sum(x) FROM test')
    [(303,)]

    使用数据库接口:

    >>> from clickhouse_driver import connect
    >>>
    >>> conn = connect('clickhouse://localhost')
    >>> cursor = conn.cursor()
    >>>
    >>> cursor.execute('SHOW TABLES')
    >>> cursor.fetchall()
    [('test',)]
    >>> 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
    1
    >>> cursor.executemany('INSERT INTO test (x) VALUES', [[200]])
    >>> cursor.rowcount
    1
    >>> cursor.execute(
    ...     'INSERT INTO test (x) '
    ...     'SELECT * FROM system.numbers LIMIT %(limit)s',
    ...     {'limit': 3}
    ... )
    >>> cursor.rowcount
    0
    >>> cursor.execute('SELECT sum(x) FROM test')
    >>> cursor.fetchall()
    [(303,)]

    链接的使用使用的参数:

       host: 地址

      port: 端口

      user: 用户名

      password: 密码

      database: 数据库名称

      send_receive_timeout: 超时时间

  • 相关阅读:
    004 cat、head、tail、vim、cp、mv、rm
    003 系统的结构目录、pwd、cd、ls、tree、mkdir、touch
    shell编程
    多线程
    接口(适配器)
    常用方法
    爬虫要具备的准则:
    知识点扫盲篇
    记录_20190628
    记录_20190626
  • 原文地址:https://www.cnblogs.com/xingxia/p/clickhouse-python.html
Copyright © 2011-2022 走看看