Apache Beam中不支持Kotlin Iterable?

Apache Beam中不支持Kotlin Iterable?,kotlin,google-cloud-dataflow,apache-beam,Kotlin,Google Cloud Dataflow,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() pri

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)
}
所以,这提醒我Kotlin的不可变集合实际上是唯一的接口,而底层集合仍然是可变的。但是,尝试将
Iterable
替换为
MutableIterable
会继续引发错误

更新2

我使用上述
MutableList
部署了Kotlin数据流作业,作业失败,原因如下:

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

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

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

当我们从groupbykey.create()获取iterable时,我可以知道如何解决此问题吗。我无法像您那样对javalang iterable进行分组。对于遇到此问题并在这里找到解决方法的人,我目前在kotlin中编写管道的解决方法是创建一个Java静态类,该类具有创建、包含和处理iterable的函数。然后,结果(以不可编辑的格式)可以传递回kotlin。

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

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

类波束管{
类目录:DoFn(){
@过程元素
fun processElement(@元素输入:千伏,接收器:输出接收器){
receiver.output(千伏of(input.key,input.value.joinToString(“\n”)))
}
}
趣味管道(选项:管道选项){
val文件=
“testFile.txt”
val p=管道。创建(选项)
p、 应用(TextIO.read().from(文件))
.apply(“第一个字符的关键行”,
WithKeys.of{line:String->line[0].toString()}
.withKeyType(TypeDescriptors.strings()))
.apply(“按第一个字符分组行”,GroupByKey.create())
.apply(“连接行”,第页,共页(ConcatLines())
.apply(“写入文件”,FileIO.writeDynamic()
.by{it.key}
.withDestinationCoder(StringUtf8Coder.of())
.via(Contextful.fn(ProcessFunction{it.value}),TextIO.sink()
.至(“任何”)
.withNaming{key->FileIO.Write.defaultNaming(key,.txt”)}
)
p、 运行()
}
}

这是在运行时还是在编译时?您可以共享更多的堆栈跟踪吗?@mkobit堆栈跟踪已添加到。谢谢谢谢,但根据我的问题,如果我做导入,它确实有效。Kotlin不需要这样做,因为
Iterable
相当于
java.lang.Iterable
。这个问题也表现在其他领域。例如,当使用返回一个
Iterable
(我无法设置)的
GroupByKey
时,与
ParDo
组合时会产生相同的错误。谢谢@mxm。我试过了,但还是得到了同样的
IllegalArgumentException
。在上面的要点中添加了示例代码和stacktrace。与另一个示例类似,
MutableList
工作正常。明白了。谢谢你的更新!我们将对此进行调查。这不是问题的答案question@HardWorker您应该用一些示例代码提问,其他示例代码可以轻松运行。“我可以试着提供更多的信息来帮助您。”Hardwerker刚刚注意到您遇到了类似的问题。我上面的回答可能对你有用。
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)
    }
}
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)