更好/在cassandra中选择数据的其他方式

时间:2014-11-19 15:16:49

标签: database cassandra

想象一下这个产品示例表

id | price | properties                                                    | description
---------------------------------------------------------------------------|---------------
1  | 22.9  | color=red, weigth=10, width=100                               | mountainbike
2  | 56.3  | shape=rectangle, weight=12, opaque=true                       | small toolbox
3  | 67    | shape=rectangle, weight=15, opaque=false, height=9, width=120 | big toolbox

列"属性"是cassandra集合类型" map"

好的,首先:为什么不将属性用作自己的列?因为它没有指定项目将具有哪些属性,因此这部分是动态的。

我想知道的是,是否有一种通过其属性选择指定项目的高效方法?像

这样的东西
SELECT price FROM products WHERE properties.color = red AND properties.weigth=10 AND properties.width=100

我想匹配一个授予所有要求属性的产品。

所以下列情况不可能

id | price | properties                                                    | description
---------------------------------------------------------------------------|---------------
1  | 22.9  | color=red, weigth=10, width=100                               | mountainbike
9  | 56.3  | color=red, weigth=10, width=100                               | another mountainbike

因此,属性列相当于主键,即使看起来很奇怪其他产品也不能具有相同的属性。

怎么做?

我找到了一个描述,其中较新版本的cassandra中的集合(在本例中是一个映射)可以进行二次索引,因此可以使用WHERE子句。但不建议这样做,因为它不能扩展好吧,我不知道每个产品将有多少属性。事实上,如果有一个产品具有完全所要求的属性,它应该是唯一的。

有没有人想让这成为可能?

1 个答案:

答案 0 :(得分:0)

如果使用索引在性能方面有意义(请参阅When to use and not use an index),请考虑使用用户定义类型。在2.1.2中,您使用frozen关键字。您无法更新用户定义类型值的部分内容。必须覆盖整个值。 Cassandra将冻结的用户定义类型的值视为blob。


    CREATE KEYSPACE abc_ks WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 };
    CREATE TYPE abc_ks.props (
      color text,
      weight text,
      width text,
      prop4 text,
      prop5 text,
      prop6 text,
      prop7 text
    );

    CREATE TABLE abc_ks.prod_line (
      id int,
      price double,
      description text,
      properties frozen<props>,
      PRIMARY KEY (description, id) 
    ); 

    INSERT INTO abc_ks.prod_line (id, price, description, properties) VALUES (1, 22.9, 'mountainbike', {color:'red', weight:'10', width:'100'});

    INSERT INTO abc_ks.prod_line (id, price, description, properties) VALUES (2, 122.5, 'mountainbike', {color:'blue', weight:'12', width:'90', prop4:'speed_12'});

    INSERT INTO abc_ks.prod_line (id, price, description, properties) VALUES (3, 90.5, 'small tool', {prop5:'shape_rectangle', weight:'15'});

    INSERT INTO abc_ks.prod_line (id, price, description, properties) VALUES (4, 100.6, 'mountainbike', {color:'blue', weight:'15', width:'90', prop4:'speed_12'});

    INSERT INTO abc_ks.prod_line (id, price, description, properties) VALUES (5, 106.5, 'mountainbike', {color:'blue', weight:'15', width:'90', prop4:'speed_12'});

    CREATE INDEX ON abc_ks.prod_line (properties);

    SELECT description, id FROM abc_ks.prod_line WHERE properties = {color: 'blue', weight:'15', width:'90', prop4:'speed_12'};

     description  | id
    --------------+----
     mountainbike |  4
     mountainbike |  5