级联 - 合并2个聚合

时间:2011-08-10 10:17:17

标签: java hadoop cascading

我有以下问题whicj我试图用级联解决:我有csv文件的记录结构:o,a,f,i,c

我需要通过o,a,f来汇总记录,并将每个组的i和c相加。

例如:

100,200,300,5,1

100,200,300,6,2

101,201,301,20,5

101,201,301,21,6

应该产生:

100,200,300,11,3

101,201,301,41,11

我无法理解如何合并我拥有的每个实例(我可以在同一时间聚合两个字段吗?)。

你有什么想法吗?

Yosi

public class CascMain {

public static void main(String[] args){

    Scheme sourceScheme = new TextLine(new Fields("line"));
    Tap source = new Lfs(sourceScheme, "/tmp/casc/group.csv");

    Scheme sinkScheme = new TextDelimited(new Fields("o", "a", "f", "ti", "tc"), ",");
    Tap sink = new Lfs(sinkScheme, "/tmp/casc/output/", SinkMode.REPLACE);

    Pipe assembly = new Pipe("agg-pipe");

    Function function = new RegexSplitter(new Fields("o", "a", "f", "i", "c"), ",");
    assembly = new Each(assembly, new Fields("line"), function);

    Pipe groupAssembly = new GroupBy("group", assembly, new Fields("o", "a", "f"));

    Sum impSum = new Sum(new Fields("ti"));
    Pipe i = new Every(groupAssembly, new Fields("i"), impSum);

    Sum clickSum = new Sum(new Fields("tc"));
    Pipe c = new Every(groupAssembly, new Fields("c"), clickSum);

    // WHAT SHOULD I DO HERE

    Properties properties = new Properties();
    FlowConnector.setApplicationJarClass(properties, CascMain.class);

    FlowConnector flowConnector = new FlowConnector(properties);
    Flow flow = flowConnector.connect("agg", source, sink, assembly);
    flow.complete();

}

}

1 个答案:

答案 0 :(得分:7)

使用AggregateBy同时聚合多个字段:

SumBy impSum = new SumBy(new Fields("i"), new Fields("ti"), long.class);
SumBy clickSum = new SumBy(new Fields("c"), new Fields("tc"), long.class);
assembly = new AggregateBy("totals", Pipe.pipes(assembly), new Fields("o", "a", "f"), 2, impSum, clickSum);