Apache Beam不支持Kotlin Iterable?

问题描述 投票:1回答:2

阿帕奇梁似乎拒绝承认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,相同的代码工作得很好。我究竟做错了什么?

依赖项版本:

  • kotlin-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的Immutable集合实际上只是界面,底层集合仍然是可变的。但是,尝试用Iterable替换MutableIterable继续引发错误。

kotlin google-cloud-dataflow apache-beam
2个回答
1
投票

我对kotlin不是很熟悉,但似乎你需要在代码中使用它之前导入import java.lang.Iterable


1
投票

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

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.