烫伤:在groupBy之后保留所有字段

时间:2014-04-08 15:19:20

标签: apache-pig cascading scalding

我正在使用groupBy来计算一个值,但似乎当我分组时,我会丢失所有不在聚合键中的字段:

filtered.filterNot('site) {s:String => ...}
        .filterNot('date) {s:String => ...}
aggr = filtered.groupBy('id, 'contentHost) { group =>
    group.min('timestamp -> 'min)
    //how do I keep original fields? (eg: site, date)
}

aggr.store(Tsv(...)) //eg: field "site" won't be here
在猪身上,它会是这样的:

aggr = group filtered by concat('id, 'contentHost);

result = foreach aggr {
  generate flatten(filtered), //how to do this in scalding?
           min(filtered.timestamp) as min;
}

1 个答案:

答案 0 :(得分:4)

我遇到了与元组API相同的问题,并且只能通过使用类型化的API来解决它。

您可以使用Scala元组,也可以在工作之外定义自己的案例类。 E.g:

case class Data(id: String, site: String, date: String, contentHost: String)

然后你就像这样处理它:

val filtered: TypedPipe[Data] = TypedPipe.from(Seq(Data("...", "2014-04-14", "...", "...")))

filtered
  .filterNot ( data => data.site == "fr" )
  .filterNot ( data => data.date == "2014-02-01" )
  .groupBy (data => (data.id, data.contentHost)) // (String,String) -> Data
  .min // or .minBy { ... }
  .toTypedPipe
  .write(TypedTsv[((String, String), Data)]("/path/"))