A Python library for working with the ClickHouse database (https://clickhouse.yandex/)
Go to file
2022-06-01 17:58:04 +08:00
docs Added functions for working with external dictionaries 2020-07-14 22:01:50 +03:00
examples Merge pull request #169 from Infinidat/dependabot/pip/examples/db_explorer/jinja2-2.11.3 2021-10-21 12:27:05 +03:00
scripts Support for data skipping indexes 2020-06-06 20:56:32 +03:00
src/clickhouse_orm fix: stream request must read 2022-06-01 17:58:04 +08:00
tests Replacing httpx as the underlying request library 2022-06-01 12:13:56 +08:00
.gitignore add pyproject.toml 2022-05-21 18:08:29 +08:00
CHANGELOG.md Releasing v2.1.1 2021-10-21 14:12:46 +03:00
LICENSE HOSTDEV-2736 change license and add license file 2017-06-18 12:35:33 +03:00
pyproject.toml fix: stream request must read 2022-06-01 17:58:04 +08:00
README.md update readme 2022-05-29 19:33:35 +08:00

A fork of infi.clikchouse_orm aimed at more frequent maintenance and bugfixes.

This repository expects to use more type hints, and will drop support for Python 2.x.

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.

First you have to install like this:

pip install ch-orm

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 clickhouse_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.