目前测试使用MapState的DoFn的最佳实践是什么

时间:2018-02-14 22:40:43

标签: java google-cloud-platform google-cloud-dataflow apache-beam

我正在编写一个使用org.apache.beam.sdk.state.MapState来实现缓存功能的数据流转换。然而,在引入MapState时,单元测试开始出现功能障碍。例外情况说:java.lang.UnsupportedOperationException: Parameter StateParameter{referent=StateDeclaration{id=cache, field=private final org.apache.beam.sdk.state.StateSpec xxxxFn.cache, stateType=org.apache.beam.sdk.state.MapState<java.lang.String, object>}} not supported by DoFnTester

因此,如果DoFnTester不再是一个选项,那么测试使用MapState的DoFn的当前最佳做法是什么?

P.S。我现在使用的是Beam 2.0.0,我无法升级到2.2.0,因为https://issues.apache.org/jira/browse/BEAM-3693

这是完整的跟踪:

java.lang.UnsupportedOperationException: Parameter StateParameter{referent=StateDeclaration{id=cache, field=private final org.apache.beam.sdk.state.StateSpec xxxxFn.cache, stateType=org.apache.beam.sdk.state.MapState<java.lang.String, object>}} not supported by DoFnTester
    at org.apache.beam.sdk.transforms.DoFnTester$5.dispatchDefault(DoFnTester.java:725)
    at org.apache.beam.sdk.transforms.DoFnTester$5.dispatchDefault(DoFnTester.java:710)
    at org.apache.beam.sdk.transforms.reflect.DoFnSignature$Parameter$Cases$WithDefault.dispatch(DoFnSignature.java:255)
    at org.apache.beam.sdk.transforms.reflect.DoFnSignature$Parameter.match(DoFnSignature.java:193)
    at org.apache.beam.sdk.transforms.DoFnTester.<init>(DoFnTester.java:709)
    at org.apache.beam.sdk.transforms.DoFnTester.of(DoFnTester.java:92)
    at xxxxFnTest.testNormalRun(GetPredictionsFnTest.java:50)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

1 个答案:

答案 0 :(得分:1)

Java Schedules是测试DirectRunner的最佳方式,无论它使用什么功能。

使用DoFn可以让您在错过可能发生的情况时测试不可行的方案,从而获得错误的信心。出于这个原因,以及错误/缺少维护,DoFnTester现在已被弃用,您应该尝试离开它。

要控制水印,处理时钟和元素捆绑,您可以使用Beam blog中所述的DoFnTester