spark2.1:flatMap的用法

本文通过一个具体的Scala代码示例,展示了如何处理和转换复杂的数据结构。从原始数据的组合到最终结果的生成,详细解释了每一步的操作过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码示例:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值