我正在 Github 上探索 GoogleCloudPlatform 提供的 Apache Beam 数据流模板.
特别是,我正在转换 PubSubToBigQuery从 Java 到 Kotlin 的模板。
通过这样做,我在 MapElements.input(...).via(...)
在线转换中收到重载歧义解析错误 274
。错误信息是:
Error:(62, 22) Kotlin: Cannot choose among the following candidates without completing type inference:
public final fun <NewInputT : Any!> via(fn: ((input: BigQueryInsertError!) -> FailsafeElement<String!, String!>!)!): MapElements<BigQueryInsertError!, FailsafeElement<String!, String!>!>! defined in org.apache.beam.sdk.transforms.MapElements
public final fun <NewInputT : Any!> via(fn: ((input: BigQueryInsertError!) -> FailsafeElement<String!, String!>!)!): MapElements<BigQueryInsertError!, FailsafeElement<String!, String!>!>! defined in org.apache.beam.sdk.transforms.MapElements
相关的Java代码片段是:
/*
* Step 3 Contd.
* Elements that failed inserts into BigQuery are extracted and converted to FailsafeElement
*/
PCollection<FailsafeElement<String, String>> failedInserts =
writeResult
.getFailedInsertsWithErr()
.apply(
"WrapInsertionErrors",
MapElements.into(FAILSAFE_ELEMENT_CODER.getEncodedTypeDescriptor())
.via((BigQueryInsertError e) -> wrapBigQueryInsertError(e)))
.setCoder(FAILSAFE_ELEMENT_CODER);
Kotlin 转换如下所示:
/*
* Step 3 Contd.
* Elements that failed inserts into BigQuery are extracted and converted to FailsafeElement
*/
val failedInserts: PCollection<FailsafeElement<String, String>> =
writeResult.failedInsertsWithErr
.apply(
"WrapInsertionErrors",
MapElements.into(FAILSAFE_ELEMENT_CODER.encodedTypeDescriptor)
.via { e: BigQueryInsertError -> wrapBigQueryInsertError(e) })
.setCoder(FAILSAFE_ELEMENT_CODER)
我不知道如何解决这个问题。任何帮助都会很好。
最佳答案
原因是 Java 和 Kotlin 之间的重载规则略有不同,这意味着在 Kotlin 中存在两个匹配的重载;
public <NewInputT> MapElements<NewInputT, OutputT> via(ProcessFunction<NewInputT, OutputT> fn)
public <NewInputT> MapElements<NewInputT, OutputT> via(SerializableFunction<NewInputT, OutputT> fn)
最简单的修复方法是直接将 lambda 显式指定为 SerializedFunction
以获得正确的重载;
.via<BigQueryInsertError> (SerializableFunction { e: BigQueryInsertError -> wrapBigQueryInsertError(e) }))
关于java - 我收到错误 : "Overload resolution ambiguity" from MapElements transform in Apache Beam when using Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57511611/