A Python library for working with the ClickHouse database (https://clickhouse.yandex/)
Go to file
2017-04-28 19:10:45 +03:00
docs Add simple engines: TinyLog, Log, Memory 2017-04-28 18:36:40 +03:00
scripts refactor documentation 2017-04-28 19:03:34 +03:00
src/infi Add simple engines: TinyLog, Log, Memory 2017-04-28 18:36:40 +03:00
tests Add simple engines: TinyLog, Log, Memory 2017-04-28 18:36:40 +03:00
.gitignore refactor documentation 2017-04-26 15:47:02 +03:00
buildout.cfg update isolated python version 2017-04-28 18:17:42 +03:00
CHANGELOG.rst Releasing v0.8.2 2017-04-06 15:12:39 +03:00
MIGRATIONS.rst migrations support - documentation 2016-07-11 13:04:48 +03:00
README fix link 2017-04-28 19:10:45 +03:00
setup.in Add Python 3 support 2016-08-01 10:28:10 +03:00

Introduction
============

This project is simple ORM for working with the [ClickHouse database](https://clickhouse.yandex/).
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:

```python
from infi.clickhouse_orm.database import Database
from infi.clickhouse_orm.models import Model
from infi.clickhouse_orm.fields import *
from infi.clickhouse_orm.engines import Memory

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:

```python
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:

```python
# Calculate what percentage of the time CPU 1 was over 95% busy
total = CPUStats.objects_in(db).filter(cpu_id=1).count()
busy = CPUStats.objects_in(db).filter(cpu_id=1, cpu_percent__gt=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 db.select('SELECT cpu_id, avg(cpu_percent) AS average FROM demo.cpustats GROUP BY cpu_id'):
    print 'CPU {row.cpu_id}: {row.average:.2f}%'.format(row=row)
```

To learn more please visit the [documentation](docs/toc.md).