在spark scala中从cassandra中检索集合会导致类型不匹配java.utils

f4t66c6m  于 12个月前  发布在  Cassandra
关注(0)|答案(1)|浏览(139)

我在cassandra中有一个表,它是一个集合类型,并在scala中创建了case类:

case class test(
  
  var status: Option[Set[String]]
)

字符串
现在我尝试使用cassandra java driver core将cassandra中的数据检索到scala中的case类:

Option(row.getSet("status", classOf[String]))

Error :
 found   : Set[String] (in java.util) 
 required: Set[String] (in scala.collection.immutable)


如何从cassandra到scala case set访问set类型?
我完整的方法来访问它是:

def getData(id: String): Option[test] = {
    try {
      val result = session.execute(
        s"SELECT * FROM $Keyspace.table WHERE id=?",
        id
      )

      if (result.getAvailableWithoutFetching != 1) {
        logger.error(s"Failed to find $Id")
        None
      } else {
        val row = result.one()
        Some(test(
          id,
         Option(row.getSet("customer_status", classOf[String]))
        ))
      }
    } catch {
      case e: IllegalArgumentException =>
        logger.error(s"Requested column names incorrect when attempting to get customer record: $e")
        None
      case e: Exception =>
        logger.error(s"Exception in getting customer record for customer $id: $e")
        None
    }
  }

y3bcpkx1

y3bcpkx11#

我把它和

Option(row.getSet("status", classOf[String]).asScala.toSet)

字符串
不得不进口:

import scala.collection.JavaConverters._

相关问题