How to connect

SQL API

ClickHouse is an open-source column-oriented DBMS for online analytical processing (OLAP) that allows users to generate analytical reports using SQL queries in real-time.

The database can be interacted with either via its CLI client or connector available for all the mainstream languages.

Authentication

Please Contact Koinju to get the database url and your credentials, the following examples show how to authenticate yourself with them.

Python

Install clickhouse-connect

pip install clickhouse-connect

Because the connector allows to query directly into a pandas DataFrame, if this feature is desired pandas should be installed as well.

Get the last 20 trades for any instrument starting with BTC

import clickhouse_connect

conn = clickhouse_connect.get_client(
    host="<provided_database_url>",
    port=8443,
    username="<username>",
    password="<password>",
    database="public_data",
)
df = conn.query_df(
    "select * from trade where  market like 'BTC%' and timestamp > toStartOfDay(now()) order by timestamp desc limit 20"
)
print(df.columns)
print(df[["exchange", "market", "timestamp", "price", "quantity", "side"]].head())

Outputs

The query includes an additional filter by timestamp to optimize speed. The trade table, over 40TB, contains all public trades across several exchanges. While the entire dataset is searchable, limiting the time frame ensures results return in milliseconds instead of seconds, especially if queries do not match existing indexes. More details on query optimization are provided for each endpoint and in a general overview.

All datetimes returned by koinju timezone aware and set to UTC. By default the connector will convert them to the user's timezone. So care should be applied if for some reasons the timezone awareness need to be dropped ( as for example when storing results in a excel spreadsheet).

Type equivalence between clickhouse and python : https://clickhouse.com/docs/integrations/python#read-format-options-python-types

Rust

Install clickhouse-rs and other dependecies

Then execute the following

Ouputs

The conversion of the Decimal type into String in the query allows to decode the strings directly into Decimal type from rust_decimal crate.

Type equivalence between clickhouse and rust : https://clickhouse.com/docs/integrations/rust#data-types

Golang

Install [clickhouse-go](https://github.com/ClickHouse/clickhouse-go)

By default all the decimal numbers in our tables are represented in [Decimal256 with a scale of 20](https://clickhouse.com/docs/sql-reference/data-types/decimal) as to not lose any precision from data recieved from the exchages.

If user value correctness the users can stick to decimal by installing and using this [decimal library](https://github.com/shopspring/decimal ) ) or if speed is a priority simply using float. The connector will accept both values for field structs represented as decimals in the database.

Outputs

Type equivalence between clickhouse and go: https://clickhouse.com/docs/integrations/go#type-conversions

Other methods

Using ClickHouse CLI Client doc : https://clickhouse.com/docs/integrations/sql-clients/cli Using BI and visualization tools doc : https://clickhouse.com/docs/integrations/data-visualization Using programming language clients and various third-party services doc : https://clickhouse.com/docs/integrations Data formats : Along with getting the SQL result directly, ClickHouse also supports exporting data in various formats, like CSV, parquet etc. doc : https://clickhouse.com/docs/integrations/data-formats

REST API

Authentication

Contact Koinju to get your api keys then use http basic auth to authenticate your requests.

Part of the documentation are generated from OPEN API which spec file is avaialble here.

curl

python

Last updated

Was this helpful?