Skip to main content
 首页 » 编程设计

python之cassandra cqlenengine 映射键上的列索引

2025年05月04日10dudu

使用 Cassandra python 驱动程序映射器 cqlengine,在使用 map 集合创建模型时,似乎只能在 map 值上创建索引

class Foo(Model): 
    id = columns.UUID(partition_key=True) 
    time_id = columns.UUID(primary_key=True, clustering_order='DESC') 
    bar = columns.Map(columns.Ascii(), columns.Double(), index=True) 

会产生一个像这样的表格

cqlsh:baz> DESCRIBE foo; 
    CREATE TABLE bar.asset_metric ( 
        id uuid, 
        time_id timeuuid, 
        bar map<ascii, double>, 
        PRIMARY KEY (id, time_id) 
    ) WITH CLUSTERING ORDER BY (time_id DESC) 
CREATE INDEX index_foo_bar ON baz.foo (values(bar)); 

如何让 cqlengine 在映射键上创建索引?

请您参考如下方法:

理查德, 当前,cqlengine 不支持索引映射键。如果您有兴趣,创建运行的 cql 索引查询的代码位于此处。

https://github.com/datastax/python-driver/blob/master/cassandra/cqlengine/management.py#L197-L201

这将形成将要执行的 cql 语句。 它将沿着以下行形成查询。

在 keyspace.model_name_here ("map_name_here") 上创建索引 index_name_here。

默认情况下,这将索引值。有一种使用 KEYS 前缀的纯 cql 索引映射键的方法。

该查询看起来像这样 CREATE INDEX index_name_here ON keyspace.model_name_here(KEYS(map_name_here));

您可以在这篇博文中阅读有关集合索引的更多信息。 http://www.datastax.com/dev/blog/cql-in-2-1

不幸的是,cqlengine 尚不支持该功能。您将不得不进入基本驱动程序并直接使用 cql 来创建该类型的索引。