Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Infinidat/infi.clickhouse_orm
A Python library for working with the ClickHouse database (https://clickhouse.yandex/)
https://github.com/Infinidat/infi.clickhouse_orm
Last synced: 3 months ago
JSON representation
A Python library for working with the ClickHouse database (https://clickhouse.yandex/)
- Host: GitHub
- URL: https://github.com/Infinidat/infi.clickhouse_orm
- Owner: Infinidat
- License: bsd-3-clause
- Created: 2016-06-26T15:13:52.000Z (over 8 years ago)
- Default Branch: develop
- Last Pushed: 2024-06-03T12:23:17.000Z (5 months ago)
- Last Synced: 2024-07-19T12:18:09.037Z (4 months ago)
- Language: Python
- Size: 686 KB
- Stars: 410
- Watchers: 33
- Forks: 135
- Open Issues: 61
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: docs/contributing.md
- License: LICENSE
Awesome Lists containing this project
- awesome-clickhouse - Infinidat/infi.clickhouse_orm - A Python library for working with the ClickHouse database. (Language bindings / Python)
README
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 import Database, Model, DateTimeField, UInt16Field, Float32Field, Memory, Fclass 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, datetimepsutil.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
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](docs/toc.md).