在scala中获取卡桑德拉的冻结地图

get frozen map of cassandra in scala

我有以下 Cassandra table 模式:-

CREATE TABLE test (
id text,
stats frozen<map<text, text>> )

我创建了 scala 应用程序来从 cassandra 中提取数据,经过一些操作后将再次将数据更新到 cassandra。

val result =  session.execute("Select * from test where id= 'testid'")
val resultList = result.all()
val rows = resultList.iterator()

if (resultList.size() > 0) {
 while (rows.hasNext()) {
   val curRow = rows.next()
   val ID = curRow.getString("id")
   val statistics = curRow.getMap[String,String] ??????
 }
}

cassandra中的数据行table是这样的:-

('testid',{'a1': '10', 'a2': '0', 'a3': '0', 'a4': '22', 'd1': '0', 'd2': '1', 'd3': '1', 'd4': '0', 'exb': '0', 'inb': '6', 'satno': '10'})
('id123',{'a1': '10', 'a2': '0', 'd1': '0', 'd2': '1', 'd3': '1', 'd4': '0'})

我想将我的统计字段准确地映射到统计数据中。我应该怎么做我的统计列中的字段对于 1 行是动态的它可能有 10 个键值对其他行,它可能有 7 个键值对。

谢谢,

你需要这样写:

val statistics = curRow.getMap[String,String]("stats", 
    classOf[String], classOf[String])

例如,

val cluster = Cluster.builder().addContactPoint("127.0.0.1").build()
val session = cluster.connect()

val result =  session.execute("Select * from test.fmtest where id= 'id123'")
val resultList = result.all()
val rows = resultList.iterator()

if (resultList.size() > 0) {
  while (rows.hasNext()) {
    val curRow = rows.next()
    val ID = curRow.getString("id")
    val statistics = curRow.getMap[String,String]("stats", 
        classOf[String], classOf[String])
    println("id=" + ID + ", stats=" + statistics)
  }
}
cluster.close()

将打印:

id=id123, stats={a1=10, a2=0, d1=0, d2=1, d3=1, d4=0}