与Cassandra和Phantom DSL的部分插入 [英] Partial inserts with Cassandra and Phantom DSL

查看:287
本文介绍了与Cassandra和Phantom DSL的部分插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个简单的Scala Play应用程序,它使用Phantom DSL驱动程序将数据存储在Cassandra DB中。 Cassandra的一个很好的功能是可以进行部分更新,即只要提供键列,就不必为表中的所有其他列提供值。 Cassandra将根据密钥将数据合并到现有记录中。

I'm building a simple Scala Play app which stores data in a Cassandra DB using the Phantom DSL driver for Scala. One of the nice features of Cassandra is that you can do partial updates i.e. so long as you provide the key columns, you do not have to provide values for all the other columns in the table. Cassandra will merge the data into your existing record based on the key.

不幸的是,这似乎不适用于Phantom DSL。我有一个包含多个列的表,我希望能够进行更新,只为键和一个数据列指定值,并让Cassandra像往常一样将它合并到记录中,同时保留所有其他数据列

Unfortunately, it seems this doesn't work with Phantom DSL. I have a table with several columns, and I want to be able to do an update, specifying values just for the key and one of the data columns, and let Cassandra merge this into the record as usual, while leaving all the other data columns for that record unchanged.

但是,如果您未在insert / update语句中指定值,Phantom DSL会用NULL替换现有的列。

But Phantom DSL overwrites existing columns with null if you don't specify values in your insert/update statement.

有人知道一个解决方法吗?我不想每次都读/写所有的数据列,因为最终的数据列会很大。

Does anybody know of a work-around for this? I don't want to have to read/write all the data columns every time, as eventually the data columns will be quite large.

我使用相同的方法如下例所示:

FYI I'm using the same approach to my Phantom coding as in these examples:

https://github.com/thiagoandrade6/cassandra-phantom/blob/master/src/main/scala/com/ cassandra / phantom / modeling / model / GenericSongsModel.scala

推荐答案

看到一些代码会很好,可以使用幻影进行部分更新。 Phantom是一个不可变的构建器,默认情况下它不会覆盖任何null。如果没有指定值,它不会做任何事情。

It would be great to see some code, but partial updates are possible with phantom. Phantom is an immutable builder, it will not override anything with null by default. If you don't specify a value it won't do anything about it.

database.table.update.where(_.id eqs id).update(_.bla setTo "newValue")

只有您明确设置为某些值的值将设置为null。请提供一些代码示例,您的问题看起来很奇怪,因为查询不跟踪表列自动添加缺少的。

will produce a query where only the values you've explicitly set to something will be set to null. Please provide some code examples, your problem seems really strange as queries don't keep track of table columns to automatically add in what's missing.

更新

如果你想删除列的值,例如在Cassandra里面设置 null ,phantom不同的语法做同样的事情:

If you would like to delete column values, e.g set them to null inside Cassandra basically, phantom offers a different syntax which does the same thing:

database.table.delete(_.col1, _.col2).where(_.id eqs id)`

此外,您甚至可以以相同的方式删除地图条目: p>

Furthermore, you can even delete map entries in the same fashion:

database.table.delete(_.props("test"), _.props("test2").where(_.id eqs id)

这假设 props props.apply(key:T )是一种 MapColumn [Table,Record,String,_] ,因此它会遵守您为地图列定义的键类型。

This assumes props is a MapColumn[Table, Record, String, _], as the props.apply(key: T) is typesafe, so it will respect the keytype you define for the map column.

这篇关于与Cassandra和Phantom DSL的部分插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆