Apache Beam - 无法使用多个输出标记推断DoFn上的编码器

Jac*_*Jac 5 java-8 google-cloud-dataflow apache-beam

我试图使用Apache Beam执行管道但是在尝试放置一些输出标签时出现错误:

import com.google.cloud.Tuple;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.io.gcp.pubsub.PubsubIO;
import org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.transforms.DoFn;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.transforms.windowing.FixedWindows;
import org.apache.beam.sdk.transforms.windowing.Window;
import org.apache.beam.sdk.values.TupleTag;
import org.apache.beam.sdk.values.TupleTagList;
import org.joda.time.Duration;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * The Transformer.
 */
class Transformer {
    final static TupleTag<Map<String, String>> successfulTransformation = new TupleTag<>();
    final static TupleTag<Tuple<String, String>> failedTransformation = new TupleTag<>();

    /**
     * The entry point of the application.
     *
     * @param args the input arguments
     */
    public static void main(String... args) {
        TransformerOptions options = PipelineOptionsFactory.fromArgs(args)
                .withValidation()
                .as(TransformerOptions.class);

        Pipeline p = Pipeline.create(options);

        p.apply("Input", PubsubIO
                .readMessagesWithAttributes()
                .withIdAttribute("id")
                .fromTopic(options.getTopicName()))
                .apply(Window.<PubsubMessage>into(FixedWindows
                        .of(Duration.standardSeconds(60))))
                .apply("Transform",
                        ParDo.of(new JsonTransformer())
                                .withOutputTags(successfulTransformation,
                                        TupleTagList.of(failedTransformation)));

        p.run().waitUntilFinish();
    }

    /**
     * Deserialize the input and convert it to a key-value pairs map.
     */
    static class JsonTransformer extends DoFn<PubsubMessage, Map<String, String>> {

        /**
         * Process each element.
         *
         * @param c the processing context
         */
        @ProcessElement
        public void processElement(ProcessContext c) {
            String messagePayload = new String(c.element().getPayload());
            try {
                Type type = new TypeToken<Map<String, String>>() {
                }.getType();
                Gson gson = new Gson();
                Map<String, String> map = gson.fromJson(messagePayload, type);
                c.output(map);
            } catch (Exception e) {
                LOG.error("Failed to process input {} -- adding to dead letter file", c.element(), e);
                String attributes = c.element()
                        .getAttributeMap()
                        .entrySet().stream().map((entry) ->
                                String.format("%s -> %s\n", entry.getKey(), entry.getValue()))
                        .collect(Collectors.joining());
                c.output(failedTransformation, Tuple.of(attributes, messagePayload));
            }

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

显示的错误是:

线程"main"中的异常java.lang.IllegalStateException:无法返回Transform.out1 [PCollection]的默认编码器.更正以下根本原因之一:未手动指定编码器; 你可以使用.setCoder()来完成.从CoderRegistry推断编码器失败:无法为V提供编码器.使用注册的CoderProvider构建编码器失败.查看详细故障的抑制异常.使用生成PTransform的默认输出Coder失败:无法为V提供编码器.使用已注册的CoderProvider构建编码器失败.

我尝试了不同的方法来解决问题,但我想我只是不明白是什么问题.我知道这些行会导致错误发生:

.withOutputTags(successfulTransformation,TupleTagList.of(failedTransformation))
Run Code Online (Sandbox Code Playgroud)

但是我没有得到它的哪一部分,哪部分需要特定的编码器以及错误中的"V"(来自"无法为V提供编码器").

为什么会发生错误?我也试着看看Apache Beam的文档,但他们似乎没有解释这样的用法,也不是我在讨论编码器的部分中理解得太多.

谢谢

Ben*_*ers 9

首先,我建议如下 - 更改:

final static TupleTag<Map<String, String>> successfulTransformation = 
    new TupleTag<>();
final static TupleTag<Tuple<String, String>> failedTransformation = 
    new TupleTag<>();
Run Code Online (Sandbox Code Playgroud)

进入这个:

final static TupleTag<Map<String, String>> successfulTransformation = 
    new TupleTag<Map<String, String>>() {};
final static TupleTag<Tuple<String, String>> failedTransformation = 
    new TupleTag<Tuple<String, String>>() {};
Run Code Online (Sandbox Code Playgroud)

这应该有助于编码器推断确定侧输出的类型.此外,您是否已正确注册CoderProviderTuple

  • 之所以与Java泛型和擦除有关.在SDK中,我们需要弄清楚类型以推断使用哪个编码器.`new TupleTag <>()`在擦除后没有类型信息,这使得这不可能.`new TupleTag <String>(){}`实际上创建了一个没有泛型参数的匿名子类,它允许我们用反射来玩弄技巧并确定实际类型是`String`,然后让我们查找`Coder <String >`. (3认同)