Knows Not Much Knows Not Much - 1 year ago 95
Scala Question

Creating Model for Cassandra using Phantom DSL

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

in cassandra?

I found this article which talks about using ListColumn

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 Source

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.


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.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download