0

我在可选列上有一个二级索引:

class Sessions extends CassandraTable[ConcreteSessions, Session] {
  object matchId extends LongColumn(this) with PartitionKey[Long]
  object userId extends OptionalLongColumn(this) with Index[Option[Long]]
  ...
}

但是,indexedToQueryColumn隐式转换不适用于可选列,因此无法编译:

def getByUserId(userId: Long): Future[Seq[Session]] = {
  select.where(_.userId eqs userId).fetch()
}

这也不是:

select.where(_.userId eqs Some(userId)).fetch()

或者改变索引的类型:

object userId extends OptionalLongColumn(this) with Index[Long]

有没有办法使用幻像执行这样的查询?

我知道我可以进行非规范化,但这会涉及一些非常混乱的内务管理,并使我们的(大量)数据大小增加三倍。查询通常只返回少数结果,所以在这种情况下我愿意使用二级索引。

4

1 回答 1

1

简短的回答:您不能使用可选字段来查询幻像中的内容。

长详细答案:

但是,如果您真的想使用辅助可选列,您应该将您的实体字段声明为 Option,但您的幻像表示不应该是查询的选项。

object userId extends LongColumn(this) with Index[Long]

在 fromRow(r: Row) 中,您可以像这样创建对象:

Sessions(matchId(r), Some(userId(r)))

然后在服务部分,您可以执行以下操作:

.value(_.userId, t.userId.getOrElse(0))

你也有更好的方法来做到这一点。您可以复制该表,进行一种新的查询,例如 sessions_by_user_id 在此表中您的 user_id 将是主键,而 match_id 是集群键。

由于 user_id 是可选的,因此您会以仅包含有效用户 ID 的表结束,这样查找起来既简单又快速。

Cassandra 依赖于查询,所以使用它对你有利。

查看我的 github 项目,它可以帮助您在同一个表中处理多个查询。

https://github.com/iamthiago/cassandra-phantom

于 2015-12-22T12:16:07.607 回答