Cassandra cqlengine python 驱动程序中的聚合查询

问题描述

我们需要为特定进程保留计数器。后端在 Cassandra 中。我们使用 python 作为后端,使用 cqlengine 驱动程序与 cassandra 进行通信。

表定义为:

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()

我无法使用 cqlengineMAX 的以下聚合查询创建过滤器/获取。此外,我在 cqlengine 中找不到任何可以帮助我创建以下逻辑的函数

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

我能够使用pythonic方式实现它:

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]))

但是如何使用 cqlengine 实现这一点。这样我就不必遍历整个 ModelQuerySet ?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)