Apache Cassandra架构设计

时间:2012-05-31 18:07:25

标签: cassandra

我有以下设置:
有CF项目和CF关键字 每个项目都有零个,一个或多个关键字,存储在列中 每个关键字都有一个或多个项目,存储在列中 它看起来像这样:


    items {
        dl { name => DELL6400,  keyword:1 => computer, keyword:2 => DELL, keyword:3 => topseller  }
        hp { name => HP12345,   keyword:1 => computer, keyword:2 => HP    }
        no { name => Nokia8210, keyword:1 => phone,    keyword:2 => NOKIA }
    }

    // here I store keys of the items only,
    // in reality I have denormalized most of items columns
    keywords{
        computer  { webpage => www.domain.com/computer , item:dl => dl , item:hp => hp }
        DELL      { webpage => www.domain.com/dell ,     item:dl => dl }
        topseller { webpage => www.domain.com/top ,      item:dl => dl }
        HP        { webpage => www.domain.com/hp ,       item:hp => hp }
        NOKIA     { webpage => www.domain.com/nokia ,    item:no => no }
        phone     { webpage => www.domain.com/phone ,    item:no => no }
    }

当我添加新项目时,如果需要,我会在关键字中添加“网页”列 当我删除一个项目时,我正在删除列“item:xx”以及
问题是如何避免“空”关键字,如果我删除诺基亚项目“否”:


    keywords{
        ...
        NOKIA     { webpage => www.domain.com/nokia }
        phone     { webpage => www.domain.com/phone }
    }

我可以计算切片项目:*,但由于最终的一致性,这可能是错误的方法。

2 个答案:

答案 0 :(得分:1)

您可以向关键字CF添加CounterColumn(http://wiki.apache.org/cassandra/Counters)。在向关键字添加项目时递增,在删除时递减:

keywords{
    computer  { webpage => www.domain.com/computer , count => 2 , item:dl => dl , item:hp => hp }
    ....
}

当读取count == 0的行时,只需将其视为已删除。如果您读取count == 0的行,则实际上不应删除“网页”列,因为可能存在并发添加操作。

答案 1 :(得分:0)

这很有意思,但我想通过其他方式 - 对#34;网页进行反规范化#34;事情,例如:

[代码]

keywords{
    computer  { webpage:dl => www.domain.com/computer , item:dl => dl ,
            webpage:dl => www.domain.com/computer ,  item:hp => hp }
    DELL      { webpage:dl => www.domain.com/dell ,     item:dl => dl }
    topseller { webpage:dl => www.domain.com/top ,      item:dl => dl }
    HP        { webpage:hp => www.domain.com/hp ,       item:hp => hp }
    NOKIA     { webpage:no => www.domain.com/nokia ,    item:no => no }
    phone     { webpage:no => www.domain.com/phone ,    item:no => no }
}

[/代码]

在这种情况下,当我删除项目:xx时,我也删除网页:xx,如果没有字段,则行自动删除(重影)。但是我仍然不确定这是不是一个好主意。