【问题标题】:Unable to store Scala Set with User Defined Type in Cassandra无法在 Cassandra 中存储具有用户定义类型的 Scala 集
【发布时间】:2018-07-12 15:42:07
【问题描述】:

我有以下型号

case class TagPartitionsInfo (
                               year:Int,
                               month:Int
                             )

case class TagPartitions(tag:String,
                         partition_info:Set[TagPartitionsInfo])

Cassandra 表中的数据存储方式如下:

tag        | partition_info
------------+--------------------------------------------------
 javascript | {{year: 2018, month: 1}, {year: 2018, month: 2}}

当我在表中插入数据时,出现以下错误

Value 1 of type class scala.collection.convert.Wrappers$SetWrapper does not correspond to any CQL3 type

向表中插入数据的代码是

def insertValues(tableName:String, model:TagPartitions):Insert = {
    QueryBuilder.insertInto(tableName).value("tag",model.tag)
      .value("partition_info",setAsJavaSet(model.partition_info))
      .ifNotExists(); 
  }

我做错了什么?在控制台中,我可以看到以下打印表明我的数据是正确的inserting in table partitions_of_a_tag with partition key List(tag) and values TagPartitions(testtag,Set(TagPartitionsInfo(2018,6)))

表架构是

CREATE TABLE codingjedi.partitions_of_a_tag (
    tag text PRIMARY KEY,
    partition_info set<frozen<tag_partitions>>
)

tag_partitionsUDT 类型

{
   year bigint,
    month bigint
);

【问题讨论】:

    标签: cassandra datastax-java-driver


    【解决方案1】:

    问题不是Set,而是UDTCassandra 不知道如何存储 UDT 值。我的模型具有UDT 的形状,但我必须从模型中专门创建UDT,如下所示:

    //get the definition of udt
     val partitionInfoType:UserType = session.getCluster().getMetadata.getKeyspace("myks").getUserType("tag_partitions")
    
    //create udt value from model 
        //the logic below assumes that there is only one element in the set
        val partitionsInfo:UDTValue = partitionInfoType.newValue()
            .setLong("year",model.partition_info.head.year)
            .setLong("month",model.partition_info.head.month)
    
    
        QueryBuilder.insertInto(tableName).value("tag",model.tag)
          .value("partition_info",setAsJavaSet(Set(partitionsInfo)))
    

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2017-05-22
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 2019-11-09
      • 2014-12-31
      相关资源
      最近更新 更多