繁体   English   中英

我收到错误:使用Kotlin时,Apache Beam中MapElements转换产生“重载分辨率歧义”

[英]I get error: “Overload resolution ambiguity” from MapElements transform in Apache Beam when using Kotlin

我正在探究Github上 GoogleCloudPlatform提供的Apache Beam数据流模板。

特别是,我正在将PubSubToBigQuery模板从Java转换为Kotlin。

这样,我在第274行的MapElements.input(...).via(...)转换中得到了过载歧义度解决错误。 错误消息是:

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明确指定为SerializableFunction以获得正确的重载。

.via<BigQueryInsertError> (SerializableFunction { e: BigQueryInsertError -> wrapBigQueryInsertError(e) }))

暂无
暂无

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

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