|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.table.toolbox; |
| 20 | + |
| 21 | +import org.apache.flink.api.common.eventtime.Watermark; |
| 22 | +import org.apache.flink.api.common.eventtime.WatermarkGenerator; |
| 23 | +import org.apache.flink.api.common.eventtime.WatermarkOutput; |
| 24 | +import org.apache.flink.api.common.eventtime.WatermarkStrategy; |
| 25 | +import org.apache.flink.streaming.api.functions.source.SourceFunction; |
| 26 | +import org.apache.flink.table.data.GenericRowData; |
| 27 | +import org.apache.flink.table.data.RowData; |
| 28 | +import org.apache.flink.table.data.StringData; |
| 29 | +import org.apache.flink.table.data.TimestampData; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +/** |
| 36 | + * A source function used to test. |
| 37 | + * |
| 38 | + * <p>For simplicity, the deprecated source function method is used to create the source. |
| 39 | + */ |
| 40 | +@SuppressWarnings("deprecation") |
| 41 | +public class TestSourceFunction implements SourceFunction<RowData> { |
| 42 | + |
| 43 | + public static final List<List<Object>> DATA = new ArrayList<>(); |
| 44 | + |
| 45 | + static { |
| 46 | + DATA.add(Arrays.asList(StringData.fromString("Bob"), 1L, TimestampData.fromEpochMillis(1))); |
| 47 | + DATA.add( |
| 48 | + Arrays.asList( |
| 49 | + StringData.fromString("Alice"), 2L, TimestampData.fromEpochMillis(2))); |
| 50 | + } |
| 51 | + |
| 52 | + private final WatermarkStrategy<RowData> watermarkStrategy; |
| 53 | + |
| 54 | + public TestSourceFunction(WatermarkStrategy<RowData> watermarkStrategy) { |
| 55 | + this.watermarkStrategy = watermarkStrategy; |
| 56 | + } |
| 57 | + |
| 58 | + private volatile boolean isRunning = true; |
| 59 | + |
| 60 | + @Override |
| 61 | + public void run(SourceContext<RowData> ctx) { |
| 62 | + WatermarkGenerator<RowData> generator = |
| 63 | + watermarkStrategy.createWatermarkGenerator(() -> null); |
| 64 | + WatermarkOutput output = new TestWatermarkOutput(ctx); |
| 65 | + |
| 66 | + int rowDataSize = DATA.get(0).size(); |
| 67 | + int index = 0; |
| 68 | + while (index < DATA.size() && isRunning) { |
| 69 | + List<Object> list = DATA.get(index); |
| 70 | + GenericRowData row = new GenericRowData(rowDataSize); |
| 71 | + for (int i = 0; i < rowDataSize; i++) { |
| 72 | + row.setField(i, list.get(i)); |
| 73 | + } |
| 74 | + generator.onEvent(row, Long.MIN_VALUE, output); |
| 75 | + generator.onPeriodicEmit(output); |
| 76 | + |
| 77 | + ctx.collect(row); |
| 78 | + |
| 79 | + index++; |
| 80 | + } |
| 81 | + ctx.close(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void cancel() { |
| 86 | + isRunning = false; |
| 87 | + } |
| 88 | + |
| 89 | + private static class TestWatermarkOutput implements WatermarkOutput { |
| 90 | + |
| 91 | + private final SourceFunction.SourceContext<RowData> ctx; |
| 92 | + |
| 93 | + public TestWatermarkOutput(SourceFunction.SourceContext<RowData> ctx) { |
| 94 | + this.ctx = ctx; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void emitWatermark(Watermark watermark) { |
| 99 | + ctx.emitWatermark( |
| 100 | + new org.apache.flink.streaming.api.watermark.Watermark( |
| 101 | + watermark.getTimestamp())); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void markIdle() {} |
| 106 | + |
| 107 | + @Override |
| 108 | + public void markActive() {} |
| 109 | + } |
| 110 | +} |
0 commit comments