Skip to main content

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

Tests PyPI

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

Metadata

Release files for clickhouse-orm 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clickhouse-orm 4.0.0
File Size Uploaded
clickhouse_orm-4.0.0.tar.gz 57.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for clickhouse-orm 4.0.0
File Interpreter ABI Platform
clickhouse_orm-4.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 121.3 kB

Release files / clickhouse_orm-4.0.0.tar.gz

Download URL clickhouse_orm-4.0.0.tar.gz
Size 57.2 kB
Tags Source
SHA-256 checksum
How to use checksums
417827e9406de8e49b36d7d78b9e2a331ffa3051b1eb261d5873d864cd7e5ac8
BLAKE2b-256 checksum
How to use checksums
ceb809a39ccb0155e3d7751032812569416ea2df0135b1849e10795ed8f2cea6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release files / clickhouse_orm-4.0.0-py2.py3-none-any.whl

Download URL clickhouse_orm-4.0.0-py2.py3-none-any.whl
Size 64.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
fdc08d77eeb085fcd3f4d15a9ec46d228062d1549705f29372804f5be88c0ffa
BLAKE2b-256 checksum
How to use checksums
65c00d2606c949917bd4cf275569b9e8a95c5ab5d54e88a7f565bbe5548cdef2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release history Release notifications | RSS feed

This release

4.0.0 This release

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.1

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page