package tap.sample; import java.util.StringTokenizer; import tap.CommandOptions; import tap.Pipe; import tap.Tap; import tap.TapMapper; import tap.TapReducer; import tap.core.*; public class WordCountProtobufIntermediate { public static void main(String[] args) throws Exception { CommandOptions o = new CommandOptions(args); /* Set up a basic pipeline of map reduce */ Tap wordcount = new Tap(o).named("wordcount"); /* Parse options - just use the standard options - input and output location, time window, etc. */ if (o.input == null) { System.err.println("Must specify input directory"); return; } if (o.output == null) { System.err.println("Must specify output directory"); return; } wordcount.createPhase().reads(o.input).writes(o.output).map(Mapper.class). groupBy("word").reduce(Reducer.class); wordcount.make(); } public static class CountRec { public String word; public int count; } public static class Mapper extends TapMapper<String,Protos.CountRec> { @Override public void map(String line, Pipe<Protos.CountRec> out) { StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { out.put(Protos.CountRec.newBuilder() .setWord(tokenizer.nextToken()) .setCount(1) .build()); } } } public static class Reducer extends TapReducer<Protos.CountRec,Protos.CountRec> { // ProtobufWritable<Protos.CountRec> protoWritable = ProtobufWritable.newInstance(Protos.CountRec.class); @Override public void reduce(Pipe<Protos.CountRec> in, Pipe<Protos.CountRec> out) { String word = null; int count = 0; for (Protos.CountRec rec : in) { if(word == null) word = rec.getWord(); count += rec.getCount(); } out.put(Protos.CountRec.newBuilder() .setWord(word) .setCount(count) .build()); } } }