Knows Not Much Knows Not Much - 3 months ago 17
Scala Question

Creating Model for Cassandra using Phantom DSL

I am reading this piece of source code

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

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.

Answer

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.

Comments