如何在Scala中从Cassandra的resultset中访问一个<text,text>映射?

huangapple go评论55阅读模式
英文:

How to access a map<text,text> from Cassandra inside a resultset in Scala?

问题

You can read map type values from Cassandra by specifying the key and value types explicitly in your code. Here's the corrected code snippet:

val mapColumn = row.getMap[String, String]("mapColumn", classOf[String], classOf[String])

This code should allow you to read the map type values correctly from the "mapColumn" in your Cassandra database.

英文:

So, basically, I'm using Spark to connect to a Cassandra database who have a column of type "map<text, text>". I can read the values from others columns, but not this one. Basically, my code is:

   val query = &quot;SELECT * FROM mydatabase.mytable;&quot;
    val conf = spark.sparkContext.getConf
    val connector = CassandraConnector.apply(conf);
    val session = connector.openSession();
    val rs = session.execute(query)  //resultset

    val rows = rs.all().map { row =&gt;
    val pk = row.getString(&quot;pk&quot;)
    val column2= row.getString(&quot;column2&quot;)
    val column3 = row.getInt(&quot;column3&quot;)
    val mapColumn = row.getMap[String, String](&quot;mapColumn&quot;)
    Row(pk , column2, column3 , mapColumn )
    }

I can read the 3 other columns, except the Map one. Can't find the solution online. How can I read the map type values?

This is the error i get when i try to run

> error: overloaded method value getMap with alternatives: [KeyT, ValueT](x$1: com.datastax.oss.driver.api.core.CqlIdentifier, x$2: Class[KeyT], x$3: Class[ValueT])java.util.Map[KeyT,ValueT] <and> [KeyT, ValueT](x$1: String, x$2: Class[KeyT], x$3: Class[ValueT])java.util.Map[KeyT,ValueT] <and> [KeyT, ValueT](x$1: Int, x$2: Class[KeyT], x$3: Class[ValueT])java.util.Map[KeyT,ValueT] cannot be applied to (String) val mapColumn= row.getMap("mapColumn")

答案1

得分: 1

错误消息已经说明了问题:没有名为getMap("columnName")的方法,但有以下方法:

getMap("columnName",
  classOf[String], // key class
  classOf[String]) // value class
英文:

The error message says it all: there's no such method getMap(&quot;columnName&quot;) but there is the following one:

getMap(&quot;columnName&quot;,
  classOf[String], // key class
  classOf[String]) // value class

答案2

得分: 0

我成功地使用以下代码实现了:

val mapObject = row.getObject("mapColumn")

val mapColumn = mapObject.asInstanceOf[java.util.Map[String, String]].toMap

注册以供他人参考。

英文:

I managed to make it using:

val mapObject = row.getObject(&quot;mapColumn&quot;)

val mapColumn = mapObject.asInstanceOf[java.util.Map[String, String]].toMap

Registering in case it can help someone.

huangapple
  • 本文由 发表于 2023年5月30日 05:22:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定