2

我正在阅读以下有关 Cassandra 的文章:

http://www.ebaytechblog.com/2012/07/16/cassandra-data-modeling-best-practices-part-1/#.UzIcL-ddVRw

这似乎意味着您可以在 cassandra 中为给定的行键使用不同的列键。真的吗?如果它是真的,你如何允许不同的行键。

我认为这可能是真的原因是因为假设我们有一个用户,它可以喜欢很多项目,我们只是希望 userId 是行键。我们让这个 rowKey (userID) 映射到特定用户可能喜欢的所有项目。每个特定用户可能喜欢不同数量的项目。因此,如果我们可以有多个列键,每个用户喜欢的每个 itemID 都有一个,那么我们可以这样解决问题。

因此,是否可以为特定的 rowKey 设置不同长度的 cassandra 列键?(你是怎么做的)

提供一个示例和/或一些 cql 代码会很棒!

让我感到困惑的是,我已经看到了一些 .cql 文件,它们事先定义了键空间,并且在如何使其动态化方面似乎非常不灵活,即允许它随意添加额外的列。例如:

CREATE TABLE IF NOT EXISTS results (
    test blob,
    tid timeuuid,
    result text,
    PRIMARY KEY(test, tid)
);

这怎么能允许增长的列?我们不需要事先指定名称吗?或者应用程序需要的其他自定义列?

4

2 回答 2

2

是的,每个 row_key 可以有不同数量的列。从关系的角度来看,tid 是变量的名称并不明显。它充当变量列键的占位符。请注意,在下面的插入语句中,语句中从未提及“tid”、“result”和“data”。

CREATE TABLE IF NOT EXISTS results (
    data blob,
    tid timeuuid,
    result text,
    PRIMARY KEY(test, tid)
);

因此,在您的示例中,您需要识别表的 row_key、c​​olumn_key 和有效负载。主键同时包含 row_key 和 column_key。

测试是你的row_key。tid 是您的 column_key。数据是你的有效载荷。

以下插入都是有效的:

INSERT your_keyspace.results('row_key_1', 'a4a70900-24e1-11df-8924-001ff3591711', 'blob_1');
INSERT your_keyspace.results('row_key_1', 'a4a70900-24e1-11df-8924-001ff3591712', 'blob_2');
#notice that the column_key changed but the row_key remained the same
INSERT your_keyspace.results('row_key_2', 'a4a70900-24e1-11df-8924-001ff3591711', 'blob_3');

这里

于 2014-03-26T17:56:24.223 回答
1

Did you thought of exploring collection support in cassandra for handling such relations in colocated way{e.g. on same data node}.

Not sure if it helps, but what about keeping user id as row key and a map containing item id as key and some value?

-Vivel

于 2014-03-26T07:57:11.530 回答