2016-07-24 42 views
0

我該怎麼做Cassandra多態性? 例如,如下圖所示存儲json? 第一種類型,當附件添加的照片Cassandra多態性

{ 
    "id": 1, 
    "text": "Hello", 
    "read": true, 
    "attachment_type": 1, 
    "attachment": { 
    "photo_id": 1, 
    "photo_big_url": "htt://photo.com/1_big.jpg", 
    "photo_medium_url": "htt://photo.com/1_medium.jpg", 
    "photo_small_url": "htt://photo.com/1_small.jpg" 
    } 
} 

和第二類,當附件添加視頻

{ 
    "id": 2, 
    "text": "Hi", 
    "read": true, 
    "attachment_type": 2, 
    "attachment": { 
    "video_id": 1, 
    "video_url": "htt://video.com/1.mpg", 
    } 
} 

回答

1

當然,你可以在一個單一的卡桑德拉表結合您字段:

CREATE TABLE media (
    id int, 
    name text, 
    read boolean, 
    attachment_type int, 
    photo_id int, 
    photo_big_url text, 
    photo_medium_url text, 
    photo_small_url" text, 
    video_id int, 
    video_url text, 
    PRIMARY KEY ((id), attachment_type) 
); 

未完成的字段未存儲在Cassandra中,因此您不會浪費硬盤空間。

根據您使用的驅動程序,您可以爲照片和視頻聲明不同的實體,每個實體只有相關的字段,並將結果映射到相應的實體。

隨着DataStax驅動程序,C#這將是這樣的:

mapper.Fetch<Photo>("SELECT * FROM media WHERE id = 1 and attachment_type = 1"); 
mapper.Fetch<Video>("SELECT * FROM media WHERE id = 1 and attachment_type = 2"); 
+0

存在,也可以實現除了使用單個表這一任務的可能性? – Dev

+0

對不起,我不太瞭解你的問題。你能重新表達一些例子嗎? – medvekoma