Search in sources :

Example 1 with BaseComputeCollectorFunc

use of edu.iu.dsc.tws.api.tset.fn.BaseComputeCollectorFunc in project twister2 by DSC-SPIDAL.

the class KeyedAddInputsExample method execute.

@Override
public void execute(WorkerEnvironment workerEnv) {
    BatchEnvironment env = TSetEnvironment.initBatch(workerEnv);
    KeyedSourceTSet<String, Integer> src0 = dummyKeyedSource(env, COUNT, PARALLELISM);
    KeyedSourceTSet<String, Integer> src1 = dummyKeyedSourceOther(env, COUNT, PARALLELISM);
    KeyedCachedTSet<String, Integer> cache0 = src0.cache();
    KeyedCachedTSet<String, Integer> cache1 = src1.cache();
    ComputeTSet<String> comp = cache0.keyedDirect().compute(new BaseComputeCollectorFunc<Iterator<Tuple<String, Integer>>, String>() {

        private Map<String, Integer> input1 = new HashMap<>();

        @Override
        public void prepare(TSetContext ctx) {
            super.prepare(ctx);
            // populate the hashmap with values from the input
            DataPartitionConsumer<Tuple<String, Integer>> part = (DataPartitionConsumer<Tuple<String, Integer>>) getInput("input").getConsumer();
            while (part.hasNext()) {
                Tuple<String, Integer> next = part.next();
                input1.put(next.getKey(), next.getValue());
            }
        }

        @Override
        public void compute(Iterator<Tuple<String, Integer>> input, RecordCollector<String> output) {
            while (input.hasNext()) {
                Tuple<String, Integer> next = input.next();
                output.collect(next.getKey() + " -> " + next.getValue() + ", " + input1.get(next.getKey()));
            }
        }
    }).addInput("input", cache1);
    comp.direct().forEach(i -> LOG.info("comp: " + i));
    LOG.info("Test lazy cache!");
    ComputeTSet<Object> forEach = comp.direct().lazyForEach(i -> LOG.info("comp-lazy: " + i));
    for (int i = 0; i < 4; i++) {
        LOG.info("iter: " + i);
        env.eval(forEach);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    env.finishEval(forEach);
}
Also used : RecordCollector(edu.iu.dsc.tws.api.tset.fn.RecordCollector) BatchEnvironment(edu.iu.dsc.tws.tset.env.BatchEnvironment) BaseComputeCollectorFunc(edu.iu.dsc.tws.api.tset.fn.BaseComputeCollectorFunc) TSetContext(edu.iu.dsc.tws.api.tset.TSetContext) Iterator(java.util.Iterator) DataPartitionConsumer(edu.iu.dsc.tws.api.dataset.DataPartitionConsumer) HashMap(java.util.HashMap) Map(java.util.Map) Tuple(edu.iu.dsc.tws.api.comms.structs.Tuple)

Aggregations

Tuple (edu.iu.dsc.tws.api.comms.structs.Tuple)1 DataPartitionConsumer (edu.iu.dsc.tws.api.dataset.DataPartitionConsumer)1 TSetContext (edu.iu.dsc.tws.api.tset.TSetContext)1 BaseComputeCollectorFunc (edu.iu.dsc.tws.api.tset.fn.BaseComputeCollectorFunc)1 RecordCollector (edu.iu.dsc.tws.api.tset.fn.RecordCollector)1 BatchEnvironment (edu.iu.dsc.tws.tset.env.BatchEnvironment)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1