hanzo-dev 7d6b25e39d call the store the datastore
27 docstrings still said ClickHouse. The store is the datastore everywhere else
in the tree — the package, the module, the env vars, the table — so the prose
was the last place a reader met the old name.

Kept three kinds of reference, deliberately:
  - clickhouse.tech/... links — citations to the upstream engine's own docs.
    They explain what an engine argument MEANS; deleting them would remove the
    only pointer to the semantics and make the code harder, not cleaner.
  - "clickhouse-server generated" in the error parser — that string names the
    process that literally emits the message being parsed.
  - the one line in migrations.py recording what the table used to be called,
    which is the point of that comment.

A name we brand is ours to change. A name that identifies someone else's
software is a fact.

Co-authored-by: Hanzo Dev <dev@hanzo.ai>
2026-07-30 12:28:06 -07:00
2026-06-28 20:23:42 -07:00
2019-07-13 11:51:10 +03:00
2020-07-16 07:21:35 +03:00
2018-04-21 11:49:14 +03:00

infi.clickhouse_orm

Introduction

This project is simple ORM for working with the ClickHouse database. It allows you to define model classes whose instances can be written to the database and read from it.

Let's jump right in with a simple example of monitoring CPU usage. First we need to define the model class, connect to the database and create a table for the model:

from datastore_orm import Database, Model, DateTimeField, UInt16Field, Float32Field, Memory, F

class CPUStats(Model):

    timestamp = DateTimeField()
    cpu_id = UInt16Field()
    cpu_percent = Float32Field()

    engine = Memory()

db = Database('demo')
db.create_table(CPUStats)

Now we can collect usage statistics per CPU, and write them to the database:

import psutil, time, datetime

psutil.cpu_percent(percpu=True) # first sample should be discarded
while True:
    time.sleep(1)
    stats = psutil.cpu_percent(percpu=True)
    timestamp = datetime.datetime.now()
    db.insert([
        CPUStats(timestamp=timestamp, cpu_id=cpu_id, cpu_percent=cpu_percent)
        for cpu_id, cpu_percent in enumerate(stats)
    ])

Querying the table is easy, using either the query builder or raw SQL:

# Calculate what percentage of the time CPU 1 was over 95% busy
queryset = CPUStats.objects_in(db)
total = queryset.filter(CPUStats.cpu_id == 1).count()
busy = queryset.filter(CPUStats.cpu_id == 1, CPUStats.cpu_percent > 95).count()
print('CPU 1 was busy {:.2f}% of the time'.format(busy * 100.0 / total))

# Calculate the average usage per CPU
for row in queryset.aggregate(CPUStats.cpu_id, average=F.avg(CPUStats.cpu_percent)):
    print('CPU {row.cpu_id}: {row.average:.2f}%'.format(row=row))

This and other examples can be found in the examples folder.

To learn more please visit the documentation.

S
Description
A Python library for working with the ClickHouse database (https://clickhouse.yandex/)
Readme BSD-3-Clause
950 KiB
Languages
Python 99.6%
Shell 0.4%