简体   繁体   English

如果选项类型为 None (Scala),则使用 getOrElse 不返回任何内容

[英]Using getOrElse to return nothing if option type is None (Scala)

I am using RDD to create a left outer join as so far I have the following results:到目前为止,我正在使用 RDD 创建左外连接,结果如下:

scala> LeftJoinedDataset.foreach(println)
(300000004,Trevor,Parr,Some((35  Jedburgh Road,PL23 6BA)))
(300000006,Ava,Coleman,None)
(200000008,Lisa,Knox,None)
(100000007,Dorothy,Thomson,None)
(400000002,Jasmine,Miller,Some((68  High Street,LE16 3PH)))
(300000009,Ruth,Campbell,None)
(100000005,Deirdre,Pullman,Some((63  Crown Street,SW99 2HY)))
(100000010,Dominic,Parr,None)
(100000001,Simon,Walsh,Some((99  Newgate Street,PA5 9UY)))
(100000003,Liam,Brown,Some((9  Earls Avenue,ML12 2DY)))

To remove the None and Some I have so far used the below getOrElse code:要删除 None 和 Some 我到目前为止使用了以下 getOrElse 代码:

scala> val LeftJoinedDataset = LeftJoin.map(x=>(x._1,x._2._1._1,x._2._1._2,x._2._2.getOrElse(None)))

This prints out:这打印出来:

scala> LeftJoinedDataset.foreach(println)
(300000004,Trevor,Parr,(35  Jedburgh Road,PL23 6BA))
(300000006,Ava,Coleman,None)
(200000008,Lisa,Knox,None)
(100000007,Dorothy,Thomson,None)
(400000002,Jasmine,Miller,(68  High Street,LE16 3PH))
(300000009,Ruth,Campbell,None)
(100000005,Deirdre,Pullman,(63  Crown Street,SW99 2HY))
(100000010,Dominic,Parr,None)
(100000001,Simon,Walsh,(99  Newgate Street,PA5 9UY))
(100000003,Liam,Brown,(9  Earls Avenue,ML12 2DY))

Although the some has gone, I still want to remove the None and return no data.尽管一些已经消失,但我仍然想删除 None 并且不返回任何数据。 Eg例如

(300000006,Ava,Coleman) instead of (300000006,Ava,Coleman,None) (300000006,Ava,Coleman)而不是(300000006,Ava,Coleman,None)

How can i do this?我怎样才能做到这一点?

Many Thanks非常感谢

You can't have different amount of columns in different rows of the same dataset, so you'll have to either drop that column altogether, all deal with Option values, or fill them with something else (eg empty strings).您不能在同一数据集的不同行中拥有不同数量的列,因此您必须完全删除该列,全部处理 Option 值,或者用其他内容(例如空字符串)填充它们。 But just having an Option in that column seems like the best way - it will show the consumer, that this data may be absent.但是,在该列中添加一个Option似乎是最好的方法——它会向消费者表明,这些数据可能不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM