繁体   English   中英

Cassandra中的Astyanax复合键

[英]Astyanax Composite Keys in Cassandra

我试图创建一个架构,使我只能使用row_key的一部分访问行。 例如,密钥的格式为user_id:machine_os:machine_arch

行键的示例:12242:“ windows2000”:“ x86”

从文档中,我无法理解这是否将使我能够查询具有userid = 12242的所有行或查询具有“ windows2000”的所有行

有没有可行的方法来实现这一目标?

谢谢,

Yadid

好的,这是正在发生的事情:根据您的模式,您正在有效地使用复合主键复合行 创建列族。 这意味着,除了最后一个具有严格相等关系的键之外,您将需要限制组合键的每个组件。 复合键的最后一个组件可以使用不等式和IN关系,但不能使用第一个和第二个组件。

此外,如果要使用任何类型的过滤,则必须指定所有三个部分。 这是必需的,因为没有分区键的所有部分,协调器节点将不知道数据在集群中的哪个节点上(请记住,Cassandra使用分区键来确定副本和数据放置)。

实际上,这意味着您不能执行以下任何操作:

select * from datacf where user_id = 100012; # missing 2nd and 3rd key components
select * from datacf where user_id = 100012; and machine_arch = 'x86'; # missing 3rd key component
select * from datacf where machine_arch = 'x86'; # you have to specify the 1st
select * from datacf where user_id = 100012 and machine_arch in ('x86', 'x64'); # nope, still want 3rd

但是,您将能够运行以下查询:

select * from datacf where user_id = 100012 and machine_arch = 'x86'
   and machine_os = "windows2000"; # yes! all 3 parts are there

select * from datacf where user_id = 100012 and machine_os = "windows2000"
   and machine_arch in ('x86', 'x64'); # the last part of the key can use the 'IN' or other equality relations

为了回答您的第一个问题,使用现有数据模型,您将既不能查询userid = 12242数据,也不能查询所有以“ windows2000”作为machine_os

如果您可以确切地告诉我将要运行哪种查询,那么我可能可以帮助您尝试相应地设计表。 从数据检索的角度来看,Cassandra数据模型通常可以更好地工作。 长话短说-仅将user_id用作主键,并在要查询的其他列上使用二级索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM