如何更新索引的字段?

How to update a field which is indexed?

我想更新 Cassandra 中使用 phantom scala sdk 建立索引的字段,例如:

this.update.where(_.id eqs folderId)
      .and(_.owner eqs owner)
      .modify(_.parent setTo parentId)

父字段是 table 中的索引字段。但是编译代码时不允许操作,会出现编译异常,如:

[error] C:\User\src\main\scala\com\autodesk\platform\columbus\cassandra\DataItem.scala:161: could not find implicit value for evidence parameter of type com.websudos.phantom.column.ModifiableColumn[T]

错误是更新索引的字段引起的。

我的解决方法是删除记录并向 "update" 记录插入一条新记录。

这种情况有更好的办法吗?

您不能更新作为主键一部分的字段,因为如果您这样做,您将导致 Cassandra 无法重新组合您正在更新的行的哈希值。

阅读 here 了解有关该主题的详细信息。本质上,如果您有一个 HashMap[K, V],您尝试做的是更新 K,但这样做您将永远无法再次检索到相同的 V

所以在 Cassandra 中,就像在 HashMap 中一样,对索引的更新是通过 DELETE 完成的,然后是新的 INSERT。这就是为什么 phantom 故意阻止您编译查询的原因,我将这些编译时间限制写在了防止无效 CQL 的特定目的中。