2017-09-14 30 views
0
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代码中以这种格式从火花作业服务器获得输出。火花作业服务器不会返回正确格式的JSON

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

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

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

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

我们可以看到您获得输出的代码吗?或者你解释你如何使用案例课程? –

回答

0

我终于弄明白了。基于这个堆栈溢出问题。如果有更好的方法,请点击这里,因为我是scala和spark job server的新手。

Convert DataFrame to RDD[Map] in Scala

所以关键是要到地图[字符串,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. 
} 
相关问题