下面是一个RDD,每一个元素是一个Array,这个Array由几个tuple组成, 现在需要将其中第二个元素是13的tuple(比如(-36522769,13))的一个元素后面追加字符串".000",其余不变, 实现方法如下:
val data = RDD[Array[(String, Int)]] = RDD(
Array((-36522769,13), (8014,14), (2014-09-19 00:10:47,17), (3,18), (FF-693960651,134)),
Array((-905910021.0000000000,13), (200,14), (2014-09-19 00:02:11,17), (3,18), (FF--112226702,134)),
Array((-2033866158.0000000000,13), (200,14), (2014-09-19 00:07:12,17), (3,18), (FF--112226702,134)),
Array((1712341836.0000000000,13), (200,14), (2014-09-19 00:07:06,17), (3,18), (FF--112226702,134)),
Array((-896432680.0000000000,13), (8014,14), (2014-09-19 00:12:17,17), (3,18), (FF-693960651,134)),
Array((-1205034451.0000000000,13), (200,14), (2014-09-19 00:07:21,17), (3,18), (FF--112226702,134)),
Array((-2117505875.0000000000,13), (15089,14), (2014-09-19 00:41:47,17), (2,18), (FF-1139491304,134)))
val res = data.map(x => x.map { case (y,13) => (y + ".000", 13)
case z => z
})
如果把13变为一个变量,比如val BAD_NUM = 13
, 这时直接改写为case (y, BAD_NUM) => ...
,将出现错误,
因为解析器将BAD_NUM视为一个自由变量,而不是外部等于13的那个变量,解决方法是用反引号把BAD_NUM扩起来,写为:
case (y, `BAD_NUM`) => (y + ".000", BAD_NUM)
推荐文章:
Playing with Scala’s pattern matching;
Pattern Matching in Scala Documenation
Pattern matching section in Scala cheatsheet