【问题标题】:How do you make cassandra store tuples of of values in one row, without actually storing a list? Is that even possible?如何让 cassandra 将值的元组存储在一行中,而不实际存储列表?这甚至可能吗?
【发布时间】:2014-03-28 22:23:38
【问题描述】:
【问题讨论】:
标签:
cassandra
cql
cassandra-2.0
【解决方案1】:
以上示例使用动态单元格(内部列)名称(即 123、456、111、222)。
您可以通过使用复合主键在 CQL 中实现这一点:
cqlsh:test> DESC TABLE user_by_item ;
CREATE TABLE test.user_by_item (
item_id int,
user_id int,
user_name text,
PRIMARY KEY (item_id, user_id)
);
cqlsh:test> select * from user_by_item WHERE item_id = 111;
item_id | user_id | user_name
---------+---------+-----------
111 | 123 | Jay
111 | 456 | John
(2 rows)
cqlsh:test> select * from user_by_item WHERE item_id = 111 and user_id = 123;
item_id | user_id | user_name
---------+---------+-----------
111 | 123 | Jay
(1 rows)
cqlsh:test> DESCRIBE TABLE item_by_user ;
CREATE TABLE test.item_by_user (
user_id int,
item_id int,
item_name text,
PRIMARY KEY (user_id, item_id)
);
cqlsh:test> SELECT * from item_by_user WHERE user_id = 123;
user_id | item_id | item_name
---------+---------+-----------
123 | 111 | iphone
123 | 222 | ipad
(2 rows)
cqlsh:test> SELECT * from item_by_user WHERE user_id = 123 and item_id = 111;
user_id | item_id | item_name
---------+---------+-----------
123 | 111 | iphone
(1 rows)
主键的第一部分将是您的“内部行键”,第二部分将用作“集群键”,即作为“内部单元格/列”名称的一部分。
因此,表格将以与示例中的相似方式在内部/物理上存储,WITH COMPACT STORAGE 选项将为您提供与示例中完全相同的物理布局
查看http://www.datastax.com/dev/blog/thrift-to-cql3 和http://thelastpickle.com/blog/2013/01/11/primary-keys-in-cql.html 了解更多详情。