Apache Beam 2.9使用writeDynamic将Avro文件写入GCS上的多个目录

时间:2019-02-01 09:26:53

标签: apache-beam

我正在使用Beam 2.9.0将Avro文件写入多个目录。我有一些“事件”类,其中有一个名为“ Id”的字符串字段。我想按“ Id”对它们进行分组,然后写入其各自的目录。

我无法确定如何在

中定义“ DestinationT”
Class FileIO.Write<DestinationT,UserT>

下面是我正在尝试的

FileIO.<String, Five9Event>writeDynamic()
                .by((SerializableFunction<Event, String>) in -> in.getId())
                .via(Contextful.fn(SerializableFunctions.<Event>identity()), //There is no conversion here
                        AvroIO.sink(Event.class))
                .withNumShards(1)
                .withNaming(id -> new CustomeFileNaming(type) //Is this where a directory is specified??
                .withTempDirectory("")//Some Temp Directory
                .withDestinationCoder(AvroCoder.of(Event.class, Event.SCHEMA$))//???

所有目的地都应使用相同的Avro架构编码器。

.withDestinationCoder(AvroCoder.of(Event.class, Event.SCHEMA$)

无法正常工作,因为它期望DestinationT的编码器(在我的情况下为String)而不是UserT的编码器(在我的情况下为Event)。如果DestinationT仅用于分组,我想不出为什么我们需要一个Coder而不是我们正在写入文件的实际有效载荷。

FileIO.write中的签名

FileIO.Write<DestinationT,UserT>    withDestinationCoder(Coder<DestinationT> destinationCoder)
Specifies a Coder for the destination type, if it can not be inferred from by(org.apache.beam.sdk.transforms.SerializableFunction<UserT, DestinationT>).

DestinationT的确切语义是什么,如果它只是某种用户定义的类型,为什么需要使用Coder?

1 个答案:

答案 0 :(得分:0)

我已经开始工作了,但是我仍然无法回答为什么我们需要DestinationCoder

FileIO.<String, Event>writeDynamic()
            .by((SerializableFunction<Event, String>) in -> in.getId())
            .via(Contextful.fn(
                        SerializableFunctions.<Event>identity()
                    ),
                    Contextful.fn(
                            (dest) -> AvroIO.sink(Event.class)
                            ))
            .withNumShards(1)
            .withTempDirectory(getTempDirectory())
            .withDestinationCoder(StringUtf8Coder.of())
            .withNaming((dest) -> new CustomeFileNaming(dest, config))

我相信AroIO.sink会设置有效负载的写入编码器.DestinationT是我刚刚使用过的StringUtfCoder.of()