如果你想在Scalding中从一个小的管道创建一个超过22个字段的管道,你会受到Scala元组的限制,因为它们不能有超过22个项目。
是否可以使用集合代替元组?我想象中的方法如下例所示,但可惜并不起作用:
input.read.mapTo('line -> aLotOfFields) { line: String =>
(1 to 24).map(_.toString)
}.write(output)
实际上你可以。在常见问题解答中有相关说明 - https://github.com/twitter/scalding/wiki/Frequently-asked-questions#what-if-i-have-more-than-22-fields-in-my-data-set
val toFields = (1 to 24).map(f => Symbol("field_" + f)).toList
input
.read
.mapTo('line -> toFields) { line: String =>
new Tuple((1 to 24).map(_.toString).map(_.asInstanceOf[AnyRef]): _*)
}
最后一个 map(_.asInstanceOf[AnyRef]) 看起来很丑,如果你有更好的解决方案,请告诉我。
将你的元组封装到case类中。这样做不仅可以使你的代码更易读和类型安全,而且比分别使用元组和集合更好。