Skip to main content

Python driver with native interface for ClickHouse

Project description

ClickHouse Python Driver

ClickHouse Python Driver with native (TCP) interface support.

https://travis-ci.org/mymarilyn/clickhouse-driver.svg?branch=master

Features

  • Compression support:

  • Basic types support:

    • Float32/64

    • [U]Int8/16/32/64

    • Date/DateTime

    • String/FixedString(N)

    • Enum8/16

    • Array(T)

  • External data for query processing.

Installation

The package can be installed using pip:

pip install clickhouse-driver

You can install extras packages if you need compression support. Example of LZ4 compression requirements installation:

pip install clickhouse-driver[lz4]

You also can specify multiple extras by using comma. Install LZ4 and ZSTD requirements:

pip install clickhouse-driver[lz4,zstd]

Usage example:

from clickhouse_driver.client import Client

client = Client('localhost')

print(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': 1}, {'x': 2}, {'x': 3}, {'x': 100}]
)
client.execute('INSERT INTO test (x) VALUES', [[200]])

print(client.execute('SELECT sum(x) FROM test'))

Arrays:

client.execute('CREATE TABLE test2 (x Array(Int32)) ENGINE = Memory')
client.execute(
    'INSERT INTO test2 (x) VALUES',
    [{'x': [10, 20, 30]}, {'x': (11, 21, 31)}]
)

print(client.execute('SELECT * FROM test2'))

Enums:

from enum import Enum

class MyEnum(Enum):
    foo = 1
    bar = 2

client.execute('''
    CREATE TABLE test3
    (
        x Enum8('foo' = 1, 'bar' = 2)
    ) ENGINE = Memory
''')
client.execute(
    'INSERT INTO test3 (x) VALUES',
    [{'x': MyEnum.foo}, {'x': 'bar'}, {'x': 1}]
)

print(client.execute('SELECT * FROM test3'))

Data compression:

from clickhouse_driver.client import Client

client_with_lz4 = Client('localhost', compression=True)
client_with_lz4 = Client('localhost', compression='lz4')
client_with_zstd = Client('localhost', compression='zstd')

External data for query processing:

tables = [{
    'name': 'ext',
    'structure': [('x', 'Int32'), ('y', 'Array(Int32)')],
    'data': [
        {'x': 100, 'y': [2, 4, 6, 8]},
        {'x': 500, 'y': [1, 3, 5, 7]},
    ]
}]
rv = self.client.execute(
    'SELECT sum(x) FROM ext', external_tables=tables)
print(rv)

CityHash algorithm notes

Unfortunately ClickHouse server comes with built-in old version of CityHash hashing algorithm. That’s why we can’t use original CityHash package. Downgraded version of this algorithm is placed at PyPi.

Connection Parameters

The first parameter host is required. There are some optional parameters:

  • port is port ClickHouse server is bound to. Default is 9000.

  • database is database connect to. Default is 'default'.

  • user. Default is 'default'.

  • password. Default is '' (no password).

  • client_name. This name will appear in server logs. Default is 'pyclient'.

  • compression. Whether or not use compression. Default is False.Possible choices:

    • True is equivalent to 'lz4'.

    • 'quicklz'.

    • 'lz4'.

    • 'lz4hc' high-compression variant of 'lz4'.

    • 'zstd'.

You can also specify timeouts via:

  • connect_timeout. Default is 10 seconds.

  • send_receive_timeout. Default is 300 seconds.

  • sync_request_timeout. Default is 5 seconds.

License

ClickHouse Python Driver is distributed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clickhouse-driver-0.0.4.tar.gz (19.0 kB view details)

Uploaded Source

File details

Details for the file clickhouse-driver-0.0.4.tar.gz.

File metadata

File hashes

Hashes for clickhouse-driver-0.0.4.tar.gz
Algorithm Hash digest
SHA256 19f28c6e27bc4456e600e5861518d85e9a0a7bce4f535aa8eece1bb95cada6fd
MD5 de1f35782c27f39617a0788d03a3e4fb
BLAKE2b-256 39aa271426b7719b0d0ec9b9fdaa241ae179bf0a8fb10b81e75a49908f7dff94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page