use of org.apache.flink.table.runtime.operators.window.assigners.TumblingWindowAssigner in project flink by apache.
the class WindowTableFunctionOperatorTest method testTumblingWindows.
@Test
public void testTumblingWindows() throws Exception {
final TumblingWindowAssigner assigner = TumblingWindowAssigner.of(Duration.ofSeconds(3));
OneInputStreamOperatorTestHarness<RowData, RowData> testHarness = createTestHarness(assigner, shiftTimeZone);
testHarness.setup(OUT_SERIALIZER);
testHarness.open();
// process elements
ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();
testHarness.processElement(insertRecord("key1", 1, 20L));
testHarness.processElement(insertRecord("key2", 1, 3999L));
testHarness.processWatermark(new Watermark(999));
// append 3 fields: window_start, window_end, window_time
expectedOutput.add(insertRecord("key1", 1, 20L, localMills(0L), localMills(3000L), 2999L));
expectedOutput.add(insertRecord("key2", 1, 3999L, localMills(3000L), localMills(6000L), 5999L));
expectedOutput.add(new Watermark(999));
ASSERTER.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput());
// late element would not be dropped
testHarness.processElement(insertRecord("key2", 1, 80L));
expectedOutput.add(insertRecord("key2", 1, 80L, localMills(0L), localMills(3000L), 2999L));
ASSERTER.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput());
}
use of org.apache.flink.table.runtime.operators.window.assigners.TumblingWindowAssigner in project flink by apache.
the class WindowTableFunctionUtil method createWindowAssigner.
/**
* Creates window assigner based on input window strategy.
*
* @param windowingStrategy input window strategy
* @return new created window assigner
*/
public static WindowAssigner<TimeWindow> createWindowAssigner(TimeAttributeWindowingStrategy windowingStrategy) {
WindowSpec windowSpec = windowingStrategy.getWindow();
boolean isProctime = windowingStrategy.isProctime();
if (windowSpec instanceof TumblingWindowSpec) {
TumblingWindowSpec tumblingWindowSpec = (TumblingWindowSpec) windowSpec;
TumblingWindowAssigner windowAssigner = TumblingWindowAssigner.of(tumblingWindowSpec.getSize());
if (isProctime) {
windowAssigner = windowAssigner.withProcessingTime();
}
if (tumblingWindowSpec.getOffset() != null) {
windowAssigner = windowAssigner.withOffset(tumblingWindowSpec.getOffset());
}
return windowAssigner;
} else if (windowSpec instanceof HoppingWindowSpec) {
HoppingWindowSpec hoppingWindowSpec = (HoppingWindowSpec) windowSpec;
SlidingWindowAssigner windowAssigner = SlidingWindowAssigner.of(hoppingWindowSpec.getSize(), hoppingWindowSpec.getSlide());
if (isProctime) {
windowAssigner = windowAssigner.withProcessingTime();
}
if (hoppingWindowSpec.getOffset() != null) {
windowAssigner = windowAssigner.withOffset(hoppingWindowSpec.getOffset());
}
return windowAssigner;
} else if (windowSpec instanceof CumulativeWindowSpec) {
CumulativeWindowSpec cumulativeWindowSpec = (CumulativeWindowSpec) windowSpec;
CumulativeWindowAssigner windowAssigner = CumulativeWindowAssigner.of(cumulativeWindowSpec.getMaxSize(), cumulativeWindowSpec.getStep());
if (isProctime) {
windowAssigner = windowAssigner.withProcessingTime();
}
if (cumulativeWindowSpec.getOffset() != null) {
windowAssigner = windowAssigner.withOffset(cumulativeWindowSpec.getOffset());
}
return windowAssigner;
} else {
throw new TableException(String.format("Unknown window spec: %s", windowSpec.getClass().getSimpleName()));
}
}
use of org.apache.flink.table.runtime.operators.window.assigners.TumblingWindowAssigner in project flink by apache.
the class WindowTableFunctionOperatorTest method testProcessingTimeTumblingWindows.
@Test
public void testProcessingTimeTumblingWindows() throws Exception {
final TumblingWindowAssigner assigner = TumblingWindowAssigner.of(Duration.ofSeconds(3)).withProcessingTime();
OneInputStreamOperatorTestHarness<RowData, RowData> testHarness = createTestHarness(assigner, shiftTimeZone);
testHarness.setup(OUT_SERIALIZER);
testHarness.open();
// process elements
ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();
// timestamp is ignored in processing time
testHarness.setProcessingTime(20L);
testHarness.processElement(insertRecord("key1", 1, Long.MAX_VALUE));
testHarness.setProcessingTime(3999L);
testHarness.processElement(insertRecord("key2", 1, Long.MAX_VALUE));
// append 3 fields: window_start, window_end, window_time
expectedOutput.add(insertRecord("key1", 1, Long.MAX_VALUE, localMills(0L), localMills(3000L), 2999L));
expectedOutput.add(insertRecord("key2", 1, Long.MAX_VALUE, localMills(3000L), localMills(6000L), 5999L));
ASSERTER.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput());
}
Aggregations