我需要修改数据以向 CEP 系统输入,我当前的数据如下所示
val rdd = {"var":"system-ready","value":0.0,"objectID":"2018","partnumber":2,"t":"2017-08-25 11:27:39.000"}
我需要像
这样的输出t = "2017-08-25 11:27:39.000
Check = { var = "system-ready",value = 0.0, objectID = "2018", partnumber = 2 }
如果有人建议更好的选择,我必须编写 RDD 映射操作来实现这一点。 colcount 是列数。
rdd.map(x => x.split("\":").mkString("\" ="))
.map((f => (f.dropRight(1).split(",").last.toString, f.drop(1).split(",").toSeq.take(colCount-1).toString)))
.map(f => (f._1, f._2.replace("WrappedArray(", "Check = {")))
.map(f => (f._1.drop(0).replace("\"t\"", "t"), f._2.dropRight(1).replace("(", "{"))) /
.map(f => f.toString().split(",C").mkString("\nC").replace(")", "}").drop(0).replace("(", "")) // replacing , with \n, droping (
.map(f => f.replace("\" =\"", "=\"").replace("\", \"", "\",").replace("\" =", "=").replace(", \"", ",").replace("{\"", "{"))
最佳答案
Scala 的 JSON 解析器似乎是解决这个问题的不错选择:
import scala.util.parsing.json
rdd.map( x => {
JSON.parseFull(x).get.asInstanceOf[Map[String,String]]
})
这将产生一个RDD[Map[String, String]]
。然后,您可以从 JSON 访问 t
字段,例如使用:
.map(dict => "t = "+dict("t"))
关于regex - 如何从 RDD 中的键中删除双引号并将 JSON 分成两行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49486351/