ilinux_one
scala中json与对象的转换
遇到的问题
因为要把spark从es读出来的json数据转换为对象,开始想用case class定义类型,通过fastjson做转换。如下
复制代码
复制代码
case class Book (author: String, content: String, id: String, time: Long, title: String)
val json = "{\"author\":\"hll\",\"content\":\"ES即etamsports\",\"id\":\"693\",\"time\":1490165237200,\"title\":\"百度百科\"}"
val mapper: ObjectMapper = new ObjectMapper()
val book: Book = (json, classOf[Book])
结果抛出了异常:: No suitable constructor found for type [simple type, class JsonTest$Book]
换成fastjson也会有相似的异常。
恍然大悟,case class没有空参构造函数,跟fastjson这些库不太兼容。
解决办法
然而又不想就java class,然后就找到了json4s-jackson,可以完美兼容scala的case class。
pom依赖:
<dependency>
<groupId>org.json4s</groupId>
<artifactId>json4s-jackson_2.10</artifactId>
<version>3.2.10</version>
</dependency>
使用的样例代码:
//隐式转换必须要导入
import org.json4s._
import org.._
class Book(val author: String,val content: String,val id: String, val time: Long, val title: String)
object JsonTest {
def main(args: Array[String]) {
val json = "{\"author\":\"hll\",\"content\":\"ES即etamsports\",\"id\":\"693\",\"time\":1490165237200,\"title\":\"百度百科\"}"
//导入隐式值
implicit val formats = DefaultFormats
val book: Book = parse(json).extract[Book]
println()
}
}
实际使用与思考
spark程序中的应用:
implicit val formats = DefaultFormats
(_._2).map(parse(_).extract[Book]).sortBy(_.time, false).take(10).foreach(println)
spark里面解析json数据有一个经典的问题,ObjectMapper对象的创建很重。一般使用mapPartition来对一个分区复用ObjectMapper对象。
我们来看一下parse方法的源码:
private[this] lazy val _defaultMapper = {
val m = new ObjectMapper()
(new Json4sScalaModule)
m
}
def mapper = _defaultMapper
def parse(in: JsonInput, useBigDecimalForDouble: Boolean = false): JValue = {
(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, useBigDecimalForDouble)
in match {
case StringInput(s) => (s, classOf[JValue])
case ReaderInput(rdr) => (rdr, classOf[JValue])
case StreamInput(stream) => (stream, classOf[JValue])
case FileInput(file) => (file, classOf[JValue])
}
}
实际使用的ObjectMapper对象是lazy初始化的而且是复用的,避免了ObjectMapper对象的重复创建,很nice。
转自/ilinuxer/p/
个人实现
import .{SparkConf, SparkContext}
import ._
import org.json4s._
import org.._
object SparkOnEs extends Serializable {
implicit val formats = DefaultFormats
case class cc(vin: String)
def main(args: Array[String]): Unit = {
val conf: SparkConf = new SparkConf().setMaster("local").setAppName("aaa")
("", "xxx")
("", "9200")
("", "false")
("", "true")
val sc = new SparkContext(conf)
val value = ("xxxx")
(_._2)
.map(parse(_).extract[cc])
.take(10).foreach(println)
}
}
相关引包
<dependency>
<groupId>org.json4s</groupId>
<artifactId>json4s-jackson_2.11</artifactId>
<version>3.6.7</version>
</dependency>