繁体   English   中英

Apache Beam 不支持 Kotlin Iterable?

[英]Kotlin Iterable not supported in Apache Beam?

Apache beam 似乎拒绝识别 Kotlin 的Iterable 这是一个示例代码:

@ProcessElement
fun processElement(
    @Element input: KV<String, Iterable<String>>, receiver: OutputReceiver<String>
) {
    val output = input.key + "|" + input.value.toString()
    println("output: $output")
    receiver.output(output)
}

我收到以下奇怪的错误:

java.lang.IllegalArgumentException:
   ...PrintString, @ProcessElement processElement(KV, OutputReceiver), @ProcessElement processElement(KV, OutputReceiver):
   @Element argument must have type org.apache.beam.sdk.values.KV<java.lang.String, java.lang.Iterable<? extends java.lang.String>>

果然,如果我将Iterable替换为java.lang.Iterable ,相同的代码就可以正常工作。 我究竟做错了什么?

依赖版本:

  • 科特林-jvm: 1.3.21
  • org.apache.beam: 2.11.0

这是一个包含完整代码和堆栈跟踪的要点:

更新

经过一些试验和错误后,我发现虽然List<String>抛出类似的异常,但MutableList<String>确实有效:

class PrintString: DoFn<KV<String, MutableList<String>>, String>() {
    @ProcessElement
    fun processElement(
        @Element input: KV<String, MutableList<String>>, receiver: OutputReceiver<String>
    ) {
        val output = input.key + "|" + input.value.toString()
        println("output: $output")
        receiver.output(output)
    }
}

所以,这让我想起 Kotlin 的不可变集合实际上只是接口,底层集合仍然是可变的。 但是,尝试用MutableIterable替换Iterable会继续引发错误。

更新 2

我使用上面的MutableList部署了我的 Kotlin Dataflow 作业,但作业失败并显示:

java.lang.RuntimeException: org.apache.beam.sdk.util.UserCodeException: java.lang.ClassCastException:
org.apache.beam.runners.dataflow.worker.util.BatchGroupAlsoByWindowViaIteratorsFn$WindowReiterable cannot be cast to java.util.List
    at org.apache.beam.runners.dataflow.worker.GroupAlsoByWindowsParDoFn$1.output(GroupAlsoByWindowsParDoFn.java:184)
    at org.apache.beam.runners.dataflow.worker.GroupAlsoByWindowFnRunner$1.outputWindowedValue(GroupAlsoByWindowFnRunner.java:102)

我不得不切换回使用java.lang.Iterable

ParDo之后使用GroupByKey时,我也遇到了这个问题。 事实证明,在编写接受GroupByKey结果的转换时,在Iterable泛型类型中需要@JvmWildcard注释。

请参阅下面的人为示例,该示例读取文件并按每行的第一个字符进行分组。

class BeamPipe {
  class ConcatLines : DoFn<KV<String, Iterable<@JvmWildcard String>>, KV<String, String>>() {
    @ProcessElement
    fun processElement(@Element input: KV<String, Iterable<@JvmWildcard String>>, receiver: OutputReceiver<KV<String, String>>) {
      receiver.output(KV.of(input.key, input.value.joinToString("\n")))
    }
  }

  fun pipe(options: PipelineOptions) {
    val file =
        "testFile.txt"
    val p = Pipeline.create(options)
    p.apply(TextIO.read().from(file))
        .apply("Key lines by first character",
            WithKeys.of { line: String -> line[0].toString() }
                .withKeyType(TypeDescriptors.strings()))
        .apply("Group lines by first character", GroupByKey.create<String, String>())
        .apply("Concatenate lines", ParDo.of(ConcatLines()))
        .apply("Write to files", FileIO.writeDynamic<String, KV<String, String>>()
            .by { it.key }
            .withDestinationCoder(StringUtf8Coder.of())
            .via(Contextful.fn(ProcessFunction { it.value }), TextIO.sink())
            .to("whatever")
            .withNaming { key -> FileIO.Write.defaultNaming(key, ".txt") }
        )
    p.run()
  }
}

这看起来像是 Beam Kotlin SDK 中的错误。 @ProcessElement方法的反射分析无法正常工作。 您可以通过使用ProcessContext ctx而不是使用@Element参数来解决此问题。

我对 kotlin 不是很熟悉,但似乎您需要先导入import java.lang.Iterable才能在代码中使用它。

当我们从 groupbykey.create() 获取可迭代对象时,我可以知道如何解决这个问题吗? 我不能像你那样使用 javalang iterable 进行 groupbykey

对于那些遇到这个问题并在这里找到方法的人,我目前继续在 kotlin 中编写管道的解决方法是创建一个 Java 静态类,其中包含创建、包含和处理 Iterable 的函数。 然后可以将结果(以不可迭代的格式)传递回 kotlin。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM