Search in sources :

Example 6 with Pair

use of com.tdunning.plume.Pair in project Plume by tdunning.

the class MapReduceTest method mapOnly.

@Test
public void mapOnly() {
    MapReduceBuilder<Integer, Integer, Integer, Integer, Integer> x1 = new MapReduceBuilder<Integer, Integer, Integer, Integer, Integer>();
    MapReduceBuilder<Integer, Integer, Integer, Integer, Integer> x2 = x1.map(new Mapper<Integer, Integer, Integer, Integer>() {

        @Override
        public void map(Integer key, Integer value, Collector<Integer, Integer> out) {
            out.collect(key, value + 1);
        }
    });
    MapReduce<Integer, Integer, Integer, Integer, Integer> mr = x2.build();
    Random gen = new Random();
    List<Pair<Integer, Integer>> in = Lists.newArrayList(Pair.create(gen.nextInt(), 1), Pair.create(gen.nextInt(), 2), Pair.create(gen.nextInt(), 3), Pair.create(gen.nextInt(), 4));
    Iterable<Pair<Integer, Integer>> out = mr.run(in);
    Set<Integer> r = Sets.newTreeSet(Iterables.transform(out, new Function<Pair<Integer, Integer>, Integer>() {

        @Override
        public Integer apply(Pair<Integer, Integer> x) {
            return x.getValue();
        }
    }));
    assertEquals(4, r.size());
    r.removeAll(Lists.newArrayList(2, 3, 4, 5));
    assertEquals(0, r.size());
}
Also used : Function(com.google.common.base.Function) Random(java.util.Random) Pair(com.tdunning.plume.Pair) Test(org.junit.Test)

Example 7 with Pair

use of com.tdunning.plume.Pair in project Plume by tdunning.

the class MapReduceTest method mapAndReduce.

@Test
public void mapAndReduce() {
    List<Pair<Integer, String>> words = Lists.newArrayList();
    Multiset<String> ref = HashMultiset.create();
    int k = 0;
    Random gen = new Random();
    for (String letter : "abcdefghij".split("")) {
        // add 2^k of this letter
        for (int i = 0; i < (1 << k); i++) {
            words.add(Pair.create(gen.nextInt(), letter));
            ref.add(letter);
        }
        k++;
    }
    MapReduce<Integer, String, String, Integer, Integer> mr = new MapReduceBuilder<Integer, String, String, Integer, Integer>().map(new Mapper<Integer, String, String, Integer>() {

        @Override
        public void map(Integer key, String value, Collector<String, Integer> out) {
            out.collect(value, 1);
        }
    }).reduce(new Reducer<String, Integer, Integer>() {

        @Override
        public void reduce(String key, Iterable<Integer> values, Collector<String, Integer> out) {
            int sum = 0;
            for (Integer value : values) {
                sum += value;
            }
            out.collect(key, sum);
        }
    }).build();
    Iterable<Pair<String, Integer>> out = mr.run(words);
    for (Pair<String, Integer> pair : out) {
        assertEquals(ref.count(pair.getKey()), pair.getValue().intValue());
    }
}
Also used : Random(java.util.Random) Pair(com.tdunning.plume.Pair) Test(org.junit.Test)

Aggregations

Pair (com.tdunning.plume.Pair)7 Test (org.junit.Test)4 DoFn (com.tdunning.plume.DoFn)3 EmitFn (com.tdunning.plume.EmitFn)3 Random (java.util.Random)3 PCollection (com.tdunning.plume.PCollection)2 PlumeObject (com.tdunning.plume.local.lazy.MapRedExecutor.PlumeObject)2 DeferredOp (com.tdunning.plume.local.lazy.op.DeferredOp)2 Flatten (com.tdunning.plume.local.lazy.op.Flatten)2 GroupByKey (com.tdunning.plume.local.lazy.op.GroupByKey)2 MultipleParallelDo (com.tdunning.plume.local.lazy.op.MultipleParallelDo)2 ParallelDo (com.tdunning.plume.local.lazy.op.ParallelDo)2 IOException (java.io.IOException)2 Map (java.util.Map)2 Function (com.google.common.base.Function)1 PTable (com.tdunning.plume.PTable)1 Plume (com.tdunning.plume.Plume)1 LocalPlume (com.tdunning.plume.local.eager.LocalPlume)1 OutputChannel (com.tdunning.plume.local.lazy.MSCR.OutputChannel)1 PCollectionType (com.tdunning.plume.types.PCollectionType)1