Yakshup Goyal
Yakshup Goyal

Reputation: 23

Aggregate query in Cassandra cqlengine python driver

We have a requirement to keep counters for a specific process. Backend is in Cassandra. We are using python as backend and cqlengine drivers to communicate with cassandra.

Table is defined as:

class LogCountRecord(db.model):
    __keyspace__ = "..."
    __table_name__ = "..."
    __options__ = {
        'compaction': {'class': 'SizeTieredCompactionStrategy',
                       'bucket_low': 0.3,
                       'bucket_high': 2,
                       'min_threshold': 2,
                       'max_threshold': 64,
                       'tombstone_compaction_interval': 86400},
        'gc_grace_seconds': 0
    }
    # request_type: can be in [POST, GET, PUT, PATCH, DELETE]
    request_type: str = db.columns.Text(partition_key=True, required=True)
    time_epoch: int = db.columns.Integer(primary_key=True, required=True, clustering_order="DESC")
    request_counter: str = db.columns.Counter()

I am unable to create a filter/get for following Aggregate query for MAX using cqlengine. Also I couldn't find any function in cqlengine which can help me create below logic:

select time_epoch, MAX(request_counter) from ... where request_type = 'GET' and time_epoch > 1613667800

I was able to acheive it in a pythonic way using:

a = LogCountRecord.filter(time_epoch__gt=1613667800,request_type="GET")
b = list(a.all().values_list('time_epoch', 'request_counter'))
print(max(b, key=lambda x: x[1]))

But how to acheive this using cqlengine. So that i won't have to iterate on whole ModelQuerySet ?

Upvotes: 1

Views: 119

Answers (0)

Related Questions