0
votes

I've defined a custom Cassandra type and a table,e.g:

CREATE TYPE my.usertype (
  id text,
  firstname text,
  lastname text
);


CREATE TABLE mytable (
  user frozen <usertype>,
  ...,
  PRIMARY KEY(user)
);

How can I define this user type in the Cassandra table definition in Scala?

class MyTable extends CassandraTable[X, Y] {
  object user extends UserColumn(this) with PartitionKey[User]
                      ^^^^^???                           ^^^???

How can I implement a custom UserColumn for the UserType? I checked the Phantom code for the column implementations, but any example and/or explanation would be great.

2

2 Answers

2
votes

So according to authors of the library, user defined types are unsupported in the open source edition of phantom: https://github.com/outworkers/phantom/issues/496

However, you may be able to partially overcome that by extending MapColumn, as it is described here: Phantom-DSL cassandra with frozen type . Of course, that's not perfect, e.g. you will not be able to generate CQL for schema creation and you will have to do some manual piping.

So more or less that could like like this:

class MyTable extends CassandraTable[MyTable , Y] {
    object user extends MapColumn[MyTable , Y, String, String](this) with PartitionKey[MapColumn...]
2
votes

In phantom pro only.

@Udt case class User(
  id: String,
  firstname: String,
  lastname: String
)

And then you use UDTColumn:

class MyTable extends Table[MyTable , Y] {
    object user extends Col[User] with PartitionKey
}

This will give you automated schema generation and whatever else, including automated initialisation of your UDT.