Creating Model for Cassandra using Phantom DSL

青春壹個敷衍的年華 提交于 2019-12-13 04:57:19

问题


I am reading this piece of source code.

This looks good but what if instead of artist, the field was "artists" where artists was an list<text> in cassandra?

I found this article which talks about using ListColumn

https://github.com/websudos/phantom/wiki/Collection-columns

but I am not sure how will you define index on the ListColumn

  object genre extends ListColumn(this) with Index[List[String]]

The line above does not compile.


回答1:


As far as I'm aware you can only do contains queries with secondary indexes on Set columns, not List.

Here's what you do: object genre extends SetColumn[Table, Record, Int](this) with Index[Set[Int]]. The 2 types Table and Record have to match what you provided when you extended CassandraTable just above, like this:

class MyTable extends CassandraTable[MyTable, MyRecord] {
  object genre extends SetColumn[MyTable, MyRecord, Int](this) with Index[Set[Int]]
}

Hope this makes sense. Careful with ListColumn too, all collection columns need the TableType and RecordType arguments.

Update

In more recent versions of phantom, you don't need to provide the type of the table and the record. Just do the following:

class MyTable extends CassandraTable[MyTable, MyRecord] {
  object genre extends SetColumn[Int](this) with Index[Set[Int]]
}

Look at this test for examples on using indexed collections, and then at this table for an example on how to define such tables.

Regards.



来源:https://stackoverflow.com/questions/35692508/creating-model-for-cassandra-using-phantom-dsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!