简体   繁体   English

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

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

I'm exploring Apache Beam dataflow templates provided by GoogleCloudPlatform on Github . 我正在探究Github上 GoogleCloudPlatform提供的Apache Beam数据流模板。

In particular, I'm converting the PubSubToBigQuery template from Java into Kotlin. 特别是,我正在将PubSubToBigQuery模板从Java转换为Kotlin。

By doing so, I get an Overload ambiguity resolution error in the MapElements.input(...).via(...) transform on line 274 . 这样,我在第274行的MapElements.input(...).via(...)转换中得到了过载歧义度解决错误。 The error message is: 错误消息是:

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

The relevant Java code snippet is: 相关的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);

The Kotlin conversion looks like: 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)

I do not know how to resolve this. 我不知道该如何解决。 Any help would be nice. 你能帮忙的话,我会很高兴。

The reason is that the overload rules are slightly different between Java and Kotlin, which means that in Kotlin there are two matching overloads; 原因是Java和Kotlin之间的重载规则略有不同,这意味着在Kotlin中有两个匹配的重载。

public <NewInputT> MapElements<NewInputT, OutputT> via(ProcessFunction<NewInputT, OutputT> fn)

public <NewInputT> MapElements<NewInputT, OutputT> via(SerializableFunction<NewInputT, OutputT> fn) 

The simplest fix is to just explicitly specify the lambda as a SerializableFunction to get the correct overload; 最简单的解决方法是将lambda明确指定为SerializableFunction以获得正确的重载。

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

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

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