use of edu.iu.dsc.tws.tset.links.batch.KeyedGatherTLink in project twister2 by DSC-SPIDAL.
the class KGatherExample method execute.
@Override
public void execute(WorkerEnvironment workerEnv) {
BatchEnvironment env = TSetEnvironment.initBatch(workerEnv);
int start = env.getWorkerID() * 100;
SourceTSet<Integer> src = dummySource(env, start, COUNT, PARALLELISM);
KeyedGatherTLink<Integer, Integer> klink = src.mapToTuple(i -> new Tuple<>(i % 10, i)).keyedGather();
LOG.info("test foreach");
klink.forEach((ApplyFunc<Tuple<Integer, Iterator<Integer>>>) data -> LOG.info(data.getKey() + " -> " + iterToString(data.getValue())));
LOG.info("test map");
klink.map((MapFunc<Tuple<Integer, Iterator<Integer>>, String>) input -> {
int s = 0;
while (input.getValue().hasNext()) {
s += input.getValue().next();
}
return input.getKey() + " -> " + s;
}).direct().forEach(s -> LOG.info("map: " + s));
LOG.info("test compute");
klink.compute((ComputeFunc<Iterator<Tuple<Integer, Iterator<Integer>>>, String>) input -> {
StringBuilder s = new StringBuilder();
while (input.hasNext()) {
Tuple<Integer, Iterator<Integer>> next = input.next();
s.append(" [").append(next.getKey()).append(" -> ").append(iterToString(next.getValue())).append("] ");
}
return s.toString();
}).direct().forEach(s -> LOG.info("compute: concat " + s));
LOG.info("test computec");
klink.compute((ComputeCollectorFunc<Iterator<Tuple<Integer, Iterator<Integer>>>, String>) (input, output) -> {
while (input.hasNext()) {
Tuple<Integer, Iterator<Integer>> next = input.next();
output.collect(next.getKey() + " -> " + iterToString(next.getValue()));
}
}).direct().forEach(s -> LOG.info("computec: " + s));
// Test byte[] key value pairs for KeyedGather
SourceTSet<String> srcString = dummyStringSource(env, 25, PARALLELISM);
KeyedGatherTLink<byte[], Integer> keyedGatherLink = srcString.mapToTuple(s -> new Tuple<>(s.getBytes(), 1)).keyedGather();
LOG.info("test foreach");
keyedGatherLink.forEach((ApplyFunc<Tuple<byte[], Iterator<Integer>>>) data -> LOG.info(new String(data.getKey()) + " -> " + iterToString(data.getValue())));
}
Aggregations