1

我正在尝试 Cassandra 并寻找在其中建模数据的方法。我已经描述了我们的数据存储要求以及我对如何在 Cassandra 中建模的想法。请让我知道这是否有意义并建议更改。

在网上做了很多搜索,但没有清楚地了解如何对多值列需求建模并对其进行索引,这是一个很常见的需求。

任何帮助将不胜感激。

我们每条记录的当前数据:

{
  ‘id’ : <some uuid>,
  ‘title’ : text,
  ‘description’ text,
  ‘images’ : [{id : id1, ‘caption’: cap1}, {id : id2, ‘caption’: cap2}, ... ],
  ‘videos’ : [‘video id1’, video id2’, …],
  ‘keywords’ [‘keyword1’, ‘keyword2’,...]
  updated_at: <timestamp>
}

我们需要的查询

  • 按 id 查找
  • 通过 images.id 查找
  • 按关键字查找
  • 所有更新的记录>

我们目前的模型

  1. 列族:文章 id:uuid 标题:varchar 描述:varchar 图片:视频:关键字:updated_at:updated_date:[例如:'2013-05-06:02']

  2. 列族:图像-文章索引

    {
      ‘id’ : <image id>, 
      ‘article1 uuid’ : null, 
      ‘article2 uuid’ : null,
      ...
    }
    
  3. 列族:关键词-文章索引

    {
      ‘id’ : <keyword>, 
      ‘article1 uuid’ : null, 
      ‘article2 uuid’ : null,
      ...
    }
    

示例查询:

  1. 按 id 查找 => 直截了当

  2. 通过 images.id 查找 =>

    ids = select * from ‘Image-Article Index’ where id=<image id>
    select * from Article where id in (ids)
    
  3. 按关键字查找 =>

    ids = select * from ‘Keyword-Article Index’ where id=<image id>
    select * from Article where id in (ids)
    
  4. 所有记录在哪里updated_at > <some timestamp>

    Cassandra 不支持范围查询,除非其中一个索引列存在一个相等条件。

    从给定的时间戳中提取日期和小时;

    for each date:hour in start to current time
        ids = select * from Article where update_date=date:hour and timestamp > <some timestamp>
        select * from Article where id in (ids)
    
4

0 回答 0