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)
)

 

    原文作者:spark
    原文地址: https://www.cnblogs.com/yy3b2007com/p/8515564.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞