flink的副输出sideoutput单元测试

背景

处理函数中处理输出主输出的数据流数据外,也可以输出多个其他的副输出的数据流数据,当我们的处理函数有副输出时,我们需要测试他们功能的正确性,本文就提供一个测试flink副输出单元测试的例子

测试flink副输出单元测试

首先看一下处理函数,其中包含副输出逻辑

java 复制代码
public class MySideOutputProcessFunction extends ProcessFunction<String, String> {
 
    public static final OutputTag<String> OUTPUT_TAG = new OutputTag<String>("sideoutput") {};
 
    @Override
    public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
        out.collect("normal:" + value);
        ctx.output(OUTPUT_TAG, "side:" + value);
    }
}

其次,看下对应的单元测试

java 复制代码
/**
 * 测试sideOutput的输出功能
 */
@Test
public void testSideOutput() throws Exception {
    MySideOutputProcessFunction mySideOutputProcessFunction = new MySideOutputProcessFunction();
    OneInputStreamOperatorTestHarness<String, String> testHarness =
            ProcessFunctionTestHarnesses.forProcessFunction(mySideOutputProcessFunction);
    testHarness.open();
    testHarness.processElement("hello", 10);
 
    // 测试主输出
    Assert.assertEquals(Lists.newArrayList("normal:hello"), testHarness.extractOutputValues());
    ConcurrentLinkedQueue<StreamRecord<String>> sideOutPutQueue =
            testHarness.getSideOutput(MySideOutputProcessFunction.OUTPUT_TAG);
    // 测试副输出
    Assert.assertEquals(Lists.newArrayList("side:hello"),
            sideOutPutQueue.stream().map(StreamRecord::getValue).collect(Collectors.toList()));
    testHarness.close();
}
相关推荐
程序猿小D21 小时前
第25节 Node.js 断言测试
后端·node.js·log4j·编辑器·vim·apache·restful
tcoding2 天前
《基于Apache Flink的流处理》笔记
笔记·flink·apache
linmoo19862 天前
Flink 系列之二十二 - 高级概念 - 保存点
大数据·flink·savepoint·保存点
Doker 多克3 天前
Flink CDC —部署模式
大数据·flink
libo_20253 天前
HarmonyOS5 UI测试革命:基于ArkUI Inspector的组件精准定位策略
单元测试
酷爱码3 天前
Spring Boot 整合 Apache Flink 的详细过程
spring boot·flink·apache
问道飞鱼3 天前
Flink 高可用集群部署指南
flink·部署·批处理·流式批处理
libo_20253 天前
HarmonyOS5 全设备覆盖:在DevEco Cloud上自动测试Phone+TV+Watch三端兼容性
单元测试
libo_20253 天前
HarmonyOS5 端到端测试:从登录到支付的完整业务流程自动化验证
单元测试