Skip to main content

A simple ORM for working with the Clickhouse database. Maintainance fork of infi.clickhouse_orm.

Project description

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.

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

clickhouse_orm-3.2.0.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

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

clickhouse_orm-3.2.0-py2.py3-none-any.whl (43.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file clickhouse_orm-3.2.0.tar.gz.

File metadata

  • Download URL: clickhouse_orm-3.2.0.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for clickhouse_orm-3.2.0.tar.gz
Algorithm Hash digest
SHA256 78e43d2833462a767512f6aab1bc8bc1560c63a0732bd107da2759119782d56c
MD5 4871a1142c80b33a7d147d493c04d7e3
BLAKE2b-256 00b505d80e3b2fbb2f7a9c090fc1e6eec7e4d21d4871db5d6e23faa5914a010d

See more details on using hashes here.

File details

Details for the file clickhouse_orm-3.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for clickhouse_orm-3.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 878ded49c45adfaae8daa4642478b3b1fa8cd71a64863f25b842713b2d5c0f37
MD5 9b9c7827639e301e4f10fec1249b46fc
BLAKE2b-256 3e0e397c408df369930e0d16097185053ab34c1e8805e6ffe5c2bebb0bb844b9

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