Search in sources :

Example 1 with CumulativeWindowAssigner

use of org.apache.flink.table.runtime.operators.window.assigners.CumulativeWindowAssigner in project flink by apache.

the class WindowTableFunctionOperatorTest method testCumulativeWindows.

@Test
public void testCumulativeWindows() throws Exception {
    final CumulativeWindowAssigner assigner = CumulativeWindowAssigner.of(Duration.ofSeconds(3), Duration.ofSeconds(1));
    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(0), localMills(1000L), 999L));
    expectedOutput.add(insertRecord("key1", 1, 20L, localMills(0), localMills(2000L), 1999L));
    expectedOutput.add(insertRecord("key1", 1, 20L, localMills(0L), localMills(3000L), 2999L));
    expectedOutput.add(insertRecord("key2", 1, 3999L, localMills(3000L), localMills(4000L), 3999L));
    expectedOutput.add(insertRecord("key2", 1, 3999L, localMills(3000L), localMills(5000L), 4999L));
    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(0), localMills(1000L), 999L));
    expectedOutput.add(insertRecord("key2", 1, 80L, localMills(0), localMills(2000L), 1999L));
    expectedOutput.add(insertRecord("key2", 1, 80L, localMills(0L), localMills(3000L), 2999L));
    ASSERTER.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput());
}
Also used : RowData(org.apache.flink.table.data.RowData) CumulativeWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.CumulativeWindowAssigner) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Example 2 with CumulativeWindowAssigner

use of org.apache.flink.table.runtime.operators.window.assigners.CumulativeWindowAssigner in project flink by apache.

the class WindowTableFunctionOperatorTest method testProcessingCumulativeWindows.

@Test
public void testProcessingCumulativeWindows() throws Exception {
    final CumulativeWindowAssigner assigner = CumulativeWindowAssigner.of(Duration.ofSeconds(3), Duration.ofSeconds(1)).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(0), localMills(1000L), 999L));
    expectedOutput.add(insertRecord("key1", 1, Long.MAX_VALUE, localMills(0), localMills(2000L), 1999L));
    expectedOutput.add(insertRecord("key1", 1, Long.MAX_VALUE, localMills(0L), localMills(3000L), 2999L));
    expectedOutput.add(insertRecord("key2", 1, Long.MAX_VALUE, localMills(3000L), localMills(4000L), 3999L));
    expectedOutput.add(insertRecord("key2", 1, Long.MAX_VALUE, localMills(3000L), localMills(5000L), 4999L));
    expectedOutput.add(insertRecord("key2", 1, Long.MAX_VALUE, localMills(3000L), localMills(6000L), 5999L));
    ASSERTER.assertOutputEqualsSorted("Output was not correct.", expectedOutput, testHarness.getOutput());
}
Also used : RowData(org.apache.flink.table.data.RowData) CumulativeWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.CumulativeWindowAssigner) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 3 with CumulativeWindowAssigner

use of org.apache.flink.table.runtime.operators.window.assigners.CumulativeWindowAssigner 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()));
    }
}
Also used : SlidingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.SlidingWindowAssigner) TableException(org.apache.flink.table.api.TableException) CumulativeWindowSpec(org.apache.flink.table.planner.plan.logical.CumulativeWindowSpec) CumulativeWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.CumulativeWindowAssigner) TumblingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.TumblingWindowAssigner) TumblingWindowSpec(org.apache.flink.table.planner.plan.logical.TumblingWindowSpec) WindowSpec(org.apache.flink.table.planner.plan.logical.WindowSpec) HoppingWindowSpec(org.apache.flink.table.planner.plan.logical.HoppingWindowSpec) CumulativeWindowSpec(org.apache.flink.table.planner.plan.logical.CumulativeWindowSpec) HoppingWindowSpec(org.apache.flink.table.planner.plan.logical.HoppingWindowSpec) TumblingWindowSpec(org.apache.flink.table.planner.plan.logical.TumblingWindowSpec)

Aggregations

CumulativeWindowAssigner (org.apache.flink.table.runtime.operators.window.assigners.CumulativeWindowAssigner)3 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 RowData (org.apache.flink.table.data.RowData)2 Test (org.junit.Test)2 Watermark (org.apache.flink.streaming.api.watermark.Watermark)1 TableException (org.apache.flink.table.api.TableException)1 CumulativeWindowSpec (org.apache.flink.table.planner.plan.logical.CumulativeWindowSpec)1 HoppingWindowSpec (org.apache.flink.table.planner.plan.logical.HoppingWindowSpec)1 TumblingWindowSpec (org.apache.flink.table.planner.plan.logical.TumblingWindowSpec)1 WindowSpec (org.apache.flink.table.planner.plan.logical.WindowSpec)1 SlidingWindowAssigner (org.apache.flink.table.runtime.operators.window.assigners.SlidingWindowAssigner)1 TumblingWindowAssigner (org.apache.flink.table.runtime.operators.window.assigners.TumblingWindowAssigner)1