英文:
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 = "SELECT * FROM mydatabase.mytable;"
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 =>
val pk = row.getString("pk")
val column2= row.getString("column2")
val column3 = row.getInt("column3")
val mapColumn = row.getMap[String, String]("mapColumn")
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("columnName")
but there is the following one:
getMap("columnName",
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("mapColumn")
val mapColumn = mapObject.asInstanceOf[java.util.Map[String, String]].toMap
Registering in case it can help someone.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论