簡體   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