From c9697de56c05313e0d164c4c51a9d8b60833905a Mon Sep 17 00:00:00 2001 From: emakarov Date: Tue, 7 Feb 2017 19:19:50 +0300 Subject: [PATCH] Buffer engine initial commit --- src/infi/clickhouse_orm/engines.py | 27 +++++++++++++++++++++++++++ src/infi/clickhouse_orm/models.py | 15 +++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/infi/clickhouse_orm/engines.py b/src/infi/clickhouse_orm/engines.py index 3f4870b..d07b2ed 100644 --- a/src/infi/clickhouse_orm/engines.py +++ b/src/infi/clickhouse_orm/engines.py @@ -62,3 +62,30 @@ class SummingMergeTree(MergeTree): params.append('(%s)' % ', '.join(self.summing_cols)) return params + +class Buffer(Engine): + """Here we define Buffer engine + Read more here https://clickhouse.yandex/reference_en.html#Buffer + """ + + #Buffer(database, table, num_layers, min_time, max_time, min_rows, max_rows, min_bytes, max_bytes) + def __init__(self, table, num_layers=16, min_time=10, max_time=100, min_rows=10000, max_rows=1000000, min_bytes=10000000, max_bytes=100000000): + self.table = table + self.num_layers = num_layers + self.min_time = min_time + self.max_time = max_time + self.min_rows = min_rows + self.max_rows = max_rows + self.min_bytes = min_bytes + self.max_bytes = max_bytes + + + def create_table_sql(self, db_name, target): + # Overriden create_table_sql example: + #sql = 'ENGINE = Buffer(merge, hits, 16, 10, 100, 10000, 1000000, 10000000, 100000000)' + sql = 'ENGINE = Buffer(%s, %s, %d, %d, %d, %d, %d, %d, %d)' % ( + db_name, target.table_name(), self.num_layers, + self.min_time, self.max_time, self.min_rows, + self.max_rows, self.min_bytes, self.max_bytes + ) + return sql diff --git a/src/infi/clickhouse_orm/models.py b/src/infi/clickhouse_orm/models.py index 6fae876..da79073 100644 --- a/src/infi/clickhouse_orm/models.py +++ b/src/infi/clickhouse_orm/models.py @@ -156,3 +156,18 @@ class Model(with_metaclass(ModelBase)): ''' data = self.__dict__ return '\t'.join(field.to_db_string(data[name], quote=False) for name, field in self._fields) + + +class BufferModel(Model): + + main_model = None # table's Model should be defined in implementation. It's a table where data will be flushed + + @classmethod + def create_table_sql(cls, db_name): + ''' + Returns the SQL command for creating a table for this model. + ''' + parts = ['CREATE TABLE IF NOT EXISTS `%s`.`%s` AS `%s`.`%s`' % (db_name, cls.table_name(), db_name, cls.main_model.table_name())] + engine_str = cls.engine.create_table_sql(db_name, cls) + parts.append(engine_str) + return ' '.join(parts)