本文整理汇总了Java中org.apache.flink.streaming.api.windowing.windows.TimeWindow类的典型用法代码示例。如果您正苦于以下问题:Java TimeWindow类的具体用法?Java TimeWindow怎么用?Java TimeWindow使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TimeWindow类属于org.apache.flink.streaming.api.windowing.windows包,在下文中一共展示了TimeWindow类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMergeConsecutiveWindows
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
@Test
public void testMergeConsecutiveWindows() {
MergingWindowAssigner.MergeCallback callback = mock(MergingWindowAssigner.MergeCallback.class);
EventTimeSessionWindows assigner = EventTimeSessionWindows.withGap(Time.milliseconds(5000));
assigner.mergeWindows(
Lists.newArrayList(
new TimeWindow(0, 1),
new TimeWindow(1, 2),
new TimeWindow(2, 3),
new TimeWindow(4, 5),
new TimeWindow(5, 6)),
callback);
verify(callback, times(1)).merge(
(Collection<TimeWindow>) argThat(containsInAnyOrder(new TimeWindow(0, 1), new TimeWindow(1, 2), new TimeWindow(2, 3))),
eq(new TimeWindow(0, 3)));
verify(callback, times(1)).merge(
(Collection<TimeWindow>) argThat(containsInAnyOrder(new TimeWindow(4, 5), new TimeWindow(5, 6))),
eq(new TimeWindow(4, 6)));
verify(callback, times(2)).merge(anyCollection(), Matchers.anyObject());
}
示例2: main
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
public static void main(String... args) throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<WikipediaEditEvent> edits = env.addSource(new WikipediaEditsSource());
edits
.timeWindowAll(Time.minutes(1))
.apply(new AllWindowFunction<WikipediaEditEvent, Tuple3<Date, Long, Long>, TimeWindow>() {
@Override
public void apply(TimeWindow timeWindow, Iterable<WikipediaEditEvent> iterable, Collector<Tuple3<Date, Long, Long>> collector) throws Exception {
long count = 0;
long bytesChanged = 0;
for (WikipediaEditEvent event : iterable) {
count++;
bytesChanged += event.getByteDiff();
}
collector.collect(new Tuple3<>(new Date(timeWindow.getEnd()), count, bytesChanged));
}
})
.print();
env.execute();
}
示例3: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
/**
* Evaluates the window and outputs none or several elements.
*
* @param window The window that is being evaluated.
* @param values The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(TimeWindow window, Iterable<WindowWordWithCount> values, Collector<WindowWordRanking> out) throws Exception {
this.ranking.setWStart(window.getStart());
this.ranking.setWEnd(window.getEnd());
List<WindowWordWithCount> rank = this.ranking.getRank();
rank.clear();
values.forEach(rank::add);
rank.sort((e1,e2) -> Long.compare(e2.getCount(), e1.getCount()));
int size = rank.size();
rank.subList(Math.min(this.rankSize, size), size).clear();
LOG.debug("WOUT: {}", this.ranking);
out.collect(this.ranking);
}
示例4: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
/**
* Evaluates the window and outputs none or several elements.
*
* @param window The window that is being evaluated.
* @param values The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(TimeWindow window, Iterable<PlayerSpeedStatistics> values, Collector<PlayersSpeedRanking> out) throws Exception {
this.ranking.setTsStart(window.getStart());
this.ranking.setTsStop(window.getEnd());
List<RankingElement> rank = this.ranking.getRank();
rank.clear();
values.forEach(e -> rank.add(new RankingElement(e.getPid(), e.getAverageSpeed())));
rank.sort(Comparator.reverseOrder());
int size = rank.size();
rank.subList(Math.min(this.rankSize, size), size).clear();
//LOG.debug("WOUT: {}", this.ranking);
out.collect(this.ranking);
}
示例5: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
/**
* @param key the key to calculate by (address or id in this section)
* @param window the time window
* @param lamps the Iterable<{@link StreetLamp}>
* @param collector the collector to handle the hand off between streams
* @throws Exception
*/
@Override
public void apply(Integer key, TimeWindow window, Iterable<StreetLamp> lamps, Collector<LampEMAConsumption> collector) throws Exception {
// creating lamp consumption
LampEMAConsumption lampConsumption = new LampEMAConsumption();
lampConsumption.setId(key);
lampConsumption.setComputed(Instant.now().getEpochSecond());
// iterating over lamps in window
for (Object lamp : lamps) {
if (lampConsumption.getConsumption() == 0) {
// initializing average
lampConsumption.setConsumption(((StreetLamp) lamp).getConsumption());
lampConsumption.setAddress(((StreetLamp) lamp).getAddress());
} else {
// using EMA to calculate average on the selected window
// the formula is: S_t = alpha * Y_t + (1 - alpha) * S_t-1
lampConsumption.setConsumption(ALPHA * ((StreetLamp) lamp).getConsumption() + (1 - ALPHA) * lampConsumption.getConsumption());
}
}
// returning the consumption
collector.collect(lampConsumption);
}
示例6: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
/**
* @param key the key to calculate by (address or id in this section)
* @param window the time window
* @param lamps the Iterable<{@link StreetLamp}>
* @param collector the collector to handle the hand off between streams
* @throws Exception
*/
@Override
public void apply(String key, TimeWindow window, Iterable<StreetLamp> lamps, Collector<LampEMAConsumptionStreet> collector) throws Exception {
// creating lamp consumption
LampEMAConsumptionStreet streetConsumption = new LampEMAConsumptionStreet();
streetConsumption.setAddress(key);
streetConsumption.setComputed(Instant.now().getEpochSecond());
// iterating over lamps in window
for (Object lamp : lamps) {
if (streetConsumption.getConsumption() == 0) {
// initializing average
streetConsumption.setConsumption(((StreetLamp) lamp).getConsumption());
} else {
// using EMA to calculate average on the selected window
// the formula is: S_t = alpha * Y_t + (1 - alpha) * S_t-1
streetConsumption.setConsumption(ALPHA * ((StreetLamp) lamp).getConsumption() + (1 - ALPHA) * streetConsumption.getConsumption());
}
}
// returning the consumption
collector.collect(streetConsumption);
}
示例7: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void apply(
Tuple key,
TimeWindow window,
Iterable<Tuple2<Integer, Boolean>> gridCells,
Collector<Tuple4<Integer, Long, Boolean, Integer>> out) throws Exception {
int cellId = ((Tuple2<Integer, Boolean>)key).f0;
boolean isStart = ((Tuple2<Integer, Boolean>)key).f1;
long windowTime = window.getEnd();
int cnt = 0;
for(Tuple2<Integer, Boolean> c : gridCells) {
cnt += 1;
}
out.collect(new Tuple4<>(cellId, windowTime, isStart, cnt));
}
示例8: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
@Override
public void apply(TimeWindow timeWindow, Iterable<Tuple2<String, Long>> iterable, Collector<Tuple2<Date, List<Tuple2<String, Long>>>> collector) throws Exception {
// put words in list
List<Tuple2<String, Long>> words = new ArrayList<>();
for(Tuple2<String, Long> word: iterable) {
words.add(word);
}
if(words.size() > 0) {
// sort list
Collections.sort(words, (o1, o2) -> -1*Long.compare(o1.f1, o2.f1));
// return top n
List<Tuple2<String, Long>> sublist = new ArrayList<>(words.subList(0, Math.min(n, words.size())));
collector.collect(Tuple2.of(new Date(timeWindow.getStart()), sublist));
}
}
示例9: evaluateWindow
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
@Override
public void evaluateWindow(Collector<Result> out, final TimeWindow window,
AbstractStreamOperator<Result> operator) throws Exception {
if (previousPanes.isEmpty()) {
// optimized path for single pane case (tumbling window)
for (KeyMap.Entry<Key, ArrayList<Type>> entry : latestPane) {
Key key = entry.getKey();
operator.setCurrentKey(key);
context.globalState = operator.getKeyedStateStore();
function.process(entry.getKey(), window, context, entry.getValue(), out);
}
}
else {
// general code path for multi-pane case
WindowFunctionTraversal<Key, Type, Result> evaluator = new WindowFunctionTraversal<>(
function, window, out, operator, context);
traverseAllPanes(evaluator, evaluationPass);
}
evaluationPass++;
}
示例10: testMergeCoveringWindow
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
@Test
public void testMergeCoveringWindow() {
MergingWindowAssigner.MergeCallback callback = mock(MergingWindowAssigner.MergeCallback.class);
ProcessingTimeSessionWindows assigner = ProcessingTimeSessionWindows.withGap(Time.milliseconds(5000));
assigner.mergeWindows(
Lists.newArrayList(
new TimeWindow(1, 1),
new TimeWindow(0, 2),
new TimeWindow(4, 7),
new TimeWindow(5, 6)),
callback);
verify(callback, times(1)).merge(
(Collection<TimeWindow>) argThat(containsInAnyOrder(new TimeWindow(1, 1), new TimeWindow(0, 2))),
eq(new TimeWindow(0, 2)));
verify(callback, times(1)).merge(
(Collection<TimeWindow>) argThat(containsInAnyOrder(new TimeWindow(5, 6), new TimeWindow(4, 7))),
eq(new TimeWindow(4, 7)));
verify(callback, times(2)).merge(anyCollection(), Matchers.anyObject());
}
示例11: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
@Override
public void apply(TimeWindow window, Iterable<Tuple2<String, Integer>> hashTags, Collector<TweetsCount> out) throws Exception {
Tuple2<String, Integer> topHashTag = new Tuple2<>("", 0);
for (Tuple2<String, Integer> hashTag : hashTags) {
if (hashTag.f1 > topHashTag.f1) {
topHashTag = hashTag;
}
}
out.collect(new TweetsCount(window.getStart(), window.getEnd(), topHashTag.f0, topHashTag.f1));
}
示例12: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
/**
* Evaluates the window and outputs none or several elements.
*
* @param key The key for which this window is evaluated.
* @param window The window that is being evaluated.
* @param input The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(String key, TimeWindow window, Iterable<WindowWordWithCount> input, Collector<WindowWordWithCount> out) throws Exception {
WindowWordWithCount value = input.iterator().next();
value.setWStart(window.getStart());
value.setWEnd(window.getEnd());
value.setWord(key);
LOG.debug("WOUT: {}", value);
out.collect(value);
}
示例13: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
/**
* Evaluates the window and outputs none or several elements.
*
* @param key The key for which this window is evaluated.
* @param window The window that is being evaluated.
* @param inputs The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(Long key, TimeWindow window, Iterable<PlayerRunningStatistics> inputs, Collector<PlayerRunningStatistics> out) throws Exception {
PlayerRunningStatistics stats = inputs.iterator().next();
stats.setPid(key);
stats.setTsStart(window.getStart());
stats.setTsStop(window.getEnd());
//LOG.debug("OUT: {}", stats);
out.collect(stats);
}
示例14: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
/**
* Evaluates the window and outputs none or several elements.
*
* @param key The key for which this window is evaluated.
* @param window The window that is being evaluated.
* @param inputs The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(Long key, TimeWindow window, Iterable<PlayerSpeedStatistics> inputs, Collector<PlayerSpeedStatistics> out) throws Exception {
PlayerSpeedStatistics stats = inputs.iterator().next();
stats.setPid(key);
stats.setTsStart(window.getStart());
stats.setTsStop(window.getEnd());
//LOG.info("OUT: {}", stats);
out.collect(stats);
}
示例15: apply
import org.apache.flink.streaming.api.windowing.windows.TimeWindow; //导入依赖的package包/类
/**
* Evaluates the window and outputs none or several elements.
*
* @param key The key for which this window is evaluated.
* @param window The window that is being evaluated.
* @param inputs The elements in the window being evaluated.
* @param out A collector for emitting elements.
* @throws Exception The function may throw exceptions to fail the program and trigger recovery.
*/
@Override
public void apply(Long key, TimeWindow window, Iterable<PlayerGridStatistics> inputs, Collector<PlayerGridStatistics> out) throws Exception {
PlayerGridStatistics stats = inputs.iterator().next();
stats.setPid(key);
stats.setTsStart(window.getStart());
//LOG.info("OUT: {}", stats);
out.collect(stats);
}