spark2.1:flatMap的用法

代码示例:

 val sample_data_combine_result=List(
(0,(List(FitModel(4022,1447.92,-8.38983306721434,2.0)),1)),
(1,(List(FitModel(1083,389.88,-11.057139857142857,3.0)),1)),
(2,(List(FitModel(4022,1447.92,-8.38983306721434,2.0)),1))
)


scala> val fitResult = sample_data_combine_result.flatMap(s => {
     |       var fitResultItems: ListBuffer[FitModelWithSceneType] = null
     |       if(s._1==0){
     |         fitResultItems=ListBuffer(
     |           FitModelWithSceneType(0,1,3.0,-3.9110667661835516,0.6989700043360189),
     |           FitModelWithSceneType(0,51,3.0,-3.9110667661835516,1.1760912590556813)
     |         )
     |       }
     |       if(s._1==1){
     |         fitResultItems=ListBuffer(
     |           FitModelWithSceneType(1,1,3.0,-3.9110667661835516,0.6989700043360189),
     |           FitModelWithSceneType(1,51,3.0,-3.9110667661835516,1.1760912590556813),
     |           FitModelWithSceneType(1,101,3.0,-3.9110667661835516,1.3979400086720377)
     |         )
     |       }
     |       if(s._1==2){
     |         fitResultItems=ListBuffer(
     |           FitModelWithSceneType(2,1,3.0,-3.9110667661835516,0.6989700043360189),
     |           FitModelWithSceneType(2,51,3.0,-3.9110667661835516,1.1760912590556813),
     |           FitModelWithSceneType(2,101,3.0,-3.9110667661835516,1.3979400086720377),
     |           FitModelWithSceneType(2,101,3.0,-3.9110667661835516,1.3979400086720377),
     |           FitModelWithSceneType(2,101,3.0,-3.9110667661835516,1.3979400086720377)
     |         )
     |       }
     | 
     |       fitResultItems
     |     })
fitResult: List[FitModelWithSceneType] = 
List(
FitModelWithSceneType(0,1,3.0,-3.9110667661835516,0.6989700043360189), 
FitModelWithSceneType(0,51,3.0,-3.9110667661835516,1.1760912590556813), 
FitModelWithSceneType(1,1,3.0,-3.9110667661835516,0.6989700043360189), 
FitModelWithSceneType(1,51,3.0,-3.9110667661835516,1.1760912590556813), 
FitModelWithSceneType(1,101,3.0,-3.9110667661835516,1.3979400086720377), 
FitModelWithSceneType(2,1,3.0,-3.9110667661835516,0.6989700043360189), 
FitModelWithSceneType(2,51,3.0,-3.9110667661835516,1.1760912590556813), 
FitModelWithSceneType(2,101,3.0,-3.9110667661835516,1.3979400086720377), 
FitModelWithSceneType(2,101,3.0,-3.9110667661835516,1.3979400086720377), 
FitModelWithSceneType(2,101,3.0,-3.9110667661835516,1.3979400086720377)
)

 

posted @ 2018-03-06 17:24  cctext  阅读(1843)  评论(0编辑  收藏  举报