Skip to main content

A ClickHouse client for Python, with a command-line interface.

Project description

pych-client

Coverage Tests Status PyPI

pych-client is a ClickHouse client for Python built on top of httpx. It targets the HTTP interface and offers the following features:

  • Sync (ClickHouseClient) and async (AsyncClickHouseClient) clients.
  • Streaming requests and responses.
  • Load credentials from environment variables, or from a configuration file.

Installation

# Default Python JSON parser:
pip install pych-client
# Faster orjson parser:
pip install pych-client[orjson]

Usage

from pych_client import AsyncClickHouseClient, ClickHouseClient

# See "Credential provider chain" for more information on credential specification.
credentials = dict(
    base_url="http://localhost:8123",
    database="default",
    username="default",
    password=""
)

# The client can be used directly, or as a context manager.
# The context manager will ensure that the HTTP client resources
# are properly cleaned-up on exit.
with ClickHouseClient(**credentials) as client:
    # `.bytes()` and `.text()` return the raw response content from the database.
    # `.json()` sets the format to `JSONEachRow` and parse the response content.
    client.bytes("SELECT arrayJoin([1, 2, 3]) AS a")
    # b'1\n2\n3\n'
    client.text("SELECT arrayJoin([1, 2, 3]) AS a")
    # '1\n2\n3\n'
    client.json("SELECT arrayJoin([1, 2, 3]) AS a")
    # [{'a': 1}, {'a': 2}, {'a': 3}]

    # `.iter_bytes()`, `.iter_text()` and `.iter_json()` return the response content
    # as it is received from the database, without buffering the entire response.
    # `.iter_text()` iterates on the line of the response.
    list(client.iter_bytes("SELECT arrayJoin([1, 2, 3]) AS a"))
    # [b'1\n2\n3\n', b'']
    list(client.iter_text("SELECT arrayJoin([1, 2, 3]) AS a"))
    # ['1', '2', '3']
    list(client.iter_json("SELECT arrayJoin([1, 2, 3]) AS a"))
    # [{'a': 1}, {'a': 2}, {'a': 3}]

    # In addition to the query, the following arguments can be set:
    # - `params`: a mapping of query parameters to their values.
    # - `data`: a bytes, string or an interator of bytes to send in the request body.
    # - `settings`: ClickHouse settings (e.g. `{"default_format": "JSONEachRow"`).
    params = {"table": "test_pych"}
    client.text('''
        CREATE TABLE {table:Identifier} (a Int64, b Int64)
        ENGINE MergeTree() ORDER BY (a, b)
    ''', params)
    client.text("INSERT INTO {table:Identifier} VALUES", params, "(1, 2)")
    client.text("INSERT INTO {table:Identifier} VALUES", params, [b"(3, 4)", b"(5, 6)"])
    client.json("SELECT * FROM {table:Identifier} ORDER BY a", params)
    # [{'a': '1', 'b': '2'}, {'a': '3', 'b': '4'}, {'a': '5', 'b': '6'}]

# `AsyncClickHouseClient` offers the same methods:
async with AsyncClickHouseClient(**credentials) as client:
    # Example usage for `.json()` and `.iter_json()`:
    await client.json("SELECT arrayJoin([1, 2, 3]) AS a")
    # [{'a': 1}, {'a': 2}, {'a': 3}]
    async for row in client.iter_json("SELECT arrayJoin([1, 2, 3]) AS a"):
        ...

Command-line interface

pych-client --help

Credential provider chain

The client looks for credentials in a way similar to the AWS SDK:

  1. If one of base_url, database, username or password is specified, these values will be used.
  2. If none of the previous values are specified, and one of PYCH_BASE_URL, PYCH_DATABASE, PYCH_USERNAME or PYCH_PASSWORD environment variables are present, these values will be used.
  3. If none of the previous values are specified, and the file ~/.config/pych-client/credentials.json exists, the fields base_url, database and username and password will be used.
  4. If none of the previous values are specified, the values http://localhost:8213, default and default will be used.

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

pych_client-0.4.2.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pych_client-0.4.2-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file pych_client-0.4.2.tar.gz.

File metadata

  • Download URL: pych_client-0.4.2.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pych_client-0.4.2.tar.gz
Algorithm Hash digest
SHA256 07f967f7fe3974d8145e6d685d32edbb06948cf5b886a3c9d6a05dc34d8b81f9
MD5 437b01a8870fd1ad09ead21dbeb4ac8e
BLAKE2b-256 9f8fe192e391a4de8a53d7206afe770fd51fd7805178fa653801849ca78c45b3

See more details on using hashes here.

File details

Details for the file pych_client-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: pych_client-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pych_client-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 198bff9f613991e5a7cfead2e4b8c8c09593d432e08b39b7b5ba9eb9cf0562c7
MD5 f321130803c713d7e0e99fb37fef07b7
BLAKE2b-256 e084d95394a523388129cca08e50d38fcc3fdc3c3d85e8362c8f2abc51932815

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