@Override public Iterable<TimeInterval> assignWindowsToElement(WindowedElement<?, T> el) { return wrap.assignWindowsToElement(el); }
@Override public Collection<Pair<Collection<TimeInterval>, TimeInterval>> mergeWindows(Collection<TimeInterval> actives) { return wrap.mergeWindows(actives); }
CSession(Duration gap) { wrap = Session.of(gap); }
@Test public void testWindowAssignment() { Session<?> windowing = Session.of(Duration.ofMillis(10)); Iterable<TimeInterval> windows = windowing.assignWindowsToElement(new TimestampedElement<>(13)); assertEquals(1, Iterables.size(windows)); assertEquals(new TimeInterval(13, 23), Iterables.getOnlyElement(windows)); }
@Test public void testWindowMerging() { Session<?> windowing = Session.of(Duration.ofMillis(10)); Collection<Pair<Collection<TimeInterval>, TimeInterval>> merged = windowing.mergeWindows(Arrays.asList( new TimeInterval(5, 15), new TimeInterval(12, 22))); assertEquals(1, merged.size()); assertEquals(new TimeInterval(5, 22), Iterables.getOnlyElement(merged).getSecond()); Collection<Pair<Collection<TimeInterval>, TimeInterval>> nonMerged = windowing.mergeWindows(Arrays.asList( new TimeInterval(5, 15), new TimeInterval(16, 22))); assertTrue(nonMerged.isEmpty()); } }
public static <T> Session<T> of(Duration gapDuration) { return new Session<>(gapDuration.toMillis()); }
public static <T> Session<T> of(Duration gapDuration) { return new Session<>(gapDuration.toMillis()); }
CSession(Duration gap) { wrap = Session.of(gap); }
@Override public Iterable<TimeInterval> assignWindowsToElement(WindowedElement<?, T> el) { return wrap.assignWindowsToElement(el); }
@Override public Collection<Pair<Collection<TimeInterval>, TimeInterval>> mergeWindows(Collection<TimeInterval> actives) { return wrap.mergeWindows(actives); }
@Override protected Dataset<Triple<TimeInterval, Integer, Set<String>>> getOutput (Dataset<Pair<String, Integer>> input) { input = AssignEventTime.of(input).using(Pair::getSecond).output(); Dataset<Pair<Integer, Set<String>>> reduced = ReduceByKey.of(input) .keyBy(e -> e.getFirst().charAt(0) - '0') .valueBy(Pair::getFirst) .reduceBy(s -> s.collect(Collectors.toSet())) .windowBy(Session.of(Duration.ofMillis(5))) .output(); return FlatMap.of(reduced) .using((UnaryFunctor<Pair<Integer, Set<String>>, Triple<TimeInterval, Integer, Set<String>>>) (elem, context) -> context.collect(Triple.of((TimeInterval) context.getWindow(), elem.getFirst(), elem.getSecond()))) .output(); }
@Override protected Dataset<Triple<TimeInterval, Integer, Set<String>>> getOutput (Dataset<Pair<String, Integer>> input) { input = AssignEventTime.of(input).using(Pair::getSecond).output(); Dataset<Pair<Integer, Set<String>>> reduced = ReduceByKey.of(input) .keyBy(e -> e.getFirst().charAt(0) - '0') .valueBy(Pair::getFirst) .reduceBy(s -> s.collect(Collectors.toSet())) .windowBy(Session.of(Duration.ofMillis(5))) .output(); return FlatMap.of(reduced) .using((UnaryFunctor<Pair<Integer, Set<String>>, Triple<TimeInterval, Integer, Set<String>>>) (elem, context) -> context.collect(Triple.of((TimeInterval) context.getWindow(), elem.getFirst(), elem.getSecond()))) .output(); }
@Override protected Dataset<Triple<TimeInterval, Integer, String>> getOutput(Dataset<Pair<String, Integer>> input) { input = AssignEventTime.of(input).using(e -> e.getSecond()).output(); Dataset<Pair<Integer, String>> reduced = ReduceStateByKey.of(input) .keyBy(e -> e.getFirst().charAt(0) - '0') .valueBy(Pair::getFirst) .stateFactory((StateFactory<String, String, AccState<String>>) AccState::new) .mergeStatesBy(AccState::combine) .windowBy(Session.of(Duration.ofMillis(5))) .output(); return FlatMap.of(reduced) .using((UnaryFunctor<Pair<Integer, String>, Triple<TimeInterval, Integer, String>>) (elem, context) -> context.collect(Triple.of((TimeInterval) context.getWindow(), elem.getFirst(), elem.getSecond()))) .output(); }
@Override protected Dataset<Triple<TimeInterval, Integer, String>> getOutput(Dataset<Pair<String, Integer>> input) { input = AssignEventTime.of(input).using(e -> e.getSecond()).output(); Dataset<Pair<Integer, String>> reduced = ReduceStateByKey.of(input) .keyBy(e -> e.getFirst().charAt(0) - '0') .valueBy(Pair::getFirst) .stateFactory((StateFactory<String, String, AccState<String>>) AccState::new) .mergeStatesBy(AccState::combine) .windowBy(Session.of(Duration.ofMillis(5))) .output(); return FlatMap.of(reduced) .using((UnaryFunctor<Pair<Integer, String>, Triple<TimeInterval, Integer, String>>) (elem, context) -> context.collect(Triple.of((TimeInterval) context.getWindow(), elem.getFirst(), elem.getSecond()))) .output(); }
@Override protected Dataset<Triple<TimeInterval, String, String>> getOutput(Dataset<Pair<String, Long>> left, Dataset<Pair<String, Long>> right) { left = AssignEventTime.of(left).using(Pair::getSecond).output(); right = AssignEventTime.of(right).using(Pair::getSecond).output(); Dataset<Pair<String, Triple<TimeInterval, String, String>>> joined = Join.of(left, right) .by(p -> "", p -> "", String.class) .using((Pair<String, Long> l, Pair<String, Long> r, Collector<Triple<TimeInterval, String, String>> c) -> c.collect(Triple.of((TimeInterval) c.getWindow(), l.getFirst(), r.getFirst()))) .windowBy(Session.of(Duration.ofMillis(10))) .output(); return MapElements.of(joined).using(Pair::getSecond).output(); }
@Override protected Dataset<Triple<TimeInterval, String, String>> getOutput(Dataset<Pair<String, Long>> left, Dataset<Pair<String, Long>> right) { left = AssignEventTime.of(left).using(Pair::getSecond).output(); right = AssignEventTime.of(right).using(Pair::getSecond).output(); Dataset<Pair<String, Triple<TimeInterval, String, String>>> joined = Join.of(left, right) .by(p -> "", p -> "", String.class) .using((Pair<String, Long> l, Pair<String, Long> r, Collector<Triple<TimeInterval, String, String>> c) -> c.collect(Triple.of((TimeInterval) c.getWindow(), l.getFirst(), r.getFirst()))) .windowBy(Session.of(Duration.ofMillis(10))) .output(); return MapElements.of(joined).using(Pair::getSecond).output(); }