【问题标题】:Phantom DSL and Cassandra custom column typePhantom DSL 和 Cassandra 自定义列类型
【发布时间】:2017-02-19 09:29:32
【问题描述】:

我已经定义了一个自定义 Cassandra 类型和一个表格,例如:

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


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

如何在 Scala 的 Cassandra 表定义中定义这种用户类型?

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

如何为UserType 实现自定义UserColumn?我检查了列实现的 Phantom 代码,但任何示例和/或解释都会很棒。

【问题讨论】:

    标签: scala cassandra phantom-dsl


    【解决方案1】:

    仅在 phantom pro 中。

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

    然后你使用UDTColumn:

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

    这将为您提供自动模式生成和其他任何功能,包括 UDT 的自动初始化。

    【讨论】:

      【解决方案2】:

      因此,根据库作者的说法,phantom 的开源版本不支持用户定义类型:https://github.com/outworkers/phantom/issues/496

      但是,您可以通过扩展 MapColumn 来部分克服这一点,如下所述:Phantom-DSL cassandra with frozen type。当然,这并不完美,例如您将无法为模式创建生成 CQL,您将不得不进行一些手动管道。

      或多或少可能是这样的:

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

      【讨论】:

        猜你喜欢
        • 2016-04-10
        • 2018-01-04
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 2016-09-11
        • 1970-01-01
        • 2017-08-06
        • 1970-01-01
        相关资源
        最近更新 更多