spark job服务器不以正确的格式返回json

时间:2017-09-14 20:59:55

标签: scala apache-spark spark-jobserver

case class Response(jobCompleted:String,detailedMessage:String)

override def runJob(sc: HiveContext, runtime: JobEnvironment, data: 
JobData): JobOutput = {
  val generateResponse= new GenerateResponse(data,sc)
  val response=generateResponse.generateResponse()
  response.pettyPrint
}

我正试图从我的scala代码中以这种格式从spark作业服务器获取输出。

" result":{
     "jobCompleted":true,
    "detailedMessage":"all good"
   }  

然而,返回给我的是以下结果:{"{\"jobCompleted\":\"true\",\"detailedMessage.."}.

有人可以指出我做错了什么以及如何获得正确的格式。我也尝试了response.toJson,它返回AST格式

"result": [{
    "jobCompleted": ["true"],
    "detailedMessage": ["all good"]
  }],

1 个答案:

答案 0 :(得分:0)

我终于明白了。基于此堆栈溢流问题。如果有更好的方式在这里发帖,因为我是scala和spark job服务器的新手。

Convert DataFrame to RDD[Map] in Scala

所以关键是将响应转换为Map [String,JsValue]。下面是我正在玩的示例代码。

case class Response(param1:String,param2:String,param3:List[SubResult])
case class SubResult(lst:List[String])
object ResultFormat extends  DefaultJsonProtocol{
implicit val subresultformat=jsonFormat1(SubResult)
implicit val responsefomat=jsonFormat3(Response)
}

type JobOutput=Map[String,JsValue]
def runJob(....){

val xlst=List("one","two")
val ylst=List("three","four")
val subresult1=SubResult(xlst)
val subresult2=SubResult(ylst)
val subResultlist=List(subresult1,subresult2)
val r=Result("xxxx","yyy",subResultlist)
 r.toJson.asJsObject.fields
//Returns output type of Map[String,JsValue] which the spark job server serializes correctly.
}