繁体   English   中英

如何在 Apache Beam 中为我的 PCollection 使用 AutoValue 数据类型?

[英]How do I use an AutoValue data type for my PCollection in Apache Beam?

我想在我的 PCollection 中使用我的 AutoValue 数据类作为 object 类型,但是我在使用自动编码器时遇到了问题:

@AutoValue
public abstract class MyPersonClass {
  public abstract String getName();
  public abstract Integer getAge();
  public abstract Float getHeight();

  public static MyPersonClass create(String name, Integer age, Float height) {
    return new AutoValue_MyPersonClass(name, age, height);
  }
}

每当我使用它时,我都会从 Beam 尝试选择编码器时收到错误消息。 我不想为它定义我自己的编码器。

如何使用编码器来推断我的 AutoValue class 的架构? 或者可以自动推断出不同的编码器吗?

Beam 有一个实用程序可以自动推断不同数据类的模式,包括 Java Bean、带有 Getter 和 Setter 的 Bean、Avro 记录、协议缓冲区和 AutoValue 类。

您只需要使用适当的SchemaProvider添加DefaultSchema注释( 请参阅 SchemaProvider javadoc 并在那里发现子类)。

此注释适用于 AutoValue 构建器,因此如果您使用 AutoValue.Builder 模式,则不需要其他任何东西

如果您使用的是create function 代替,就像在这种情况下,您可以添加SchemaCreate注释,如下所示:

import org.apache.beam.sdk.schemas.AutoValueSchema;
import org.apache.beam.sdk.schemas.annotations.DefaultSchema;
import org.apache.beam.sdk.schemas.annotations.SchemaCreate;

@DefaultSchema(AutoValueSchema.class)
@AutoValue
public abstract class MyPersonClass {
  public abstract String getName();
  public abstract Integer getAge();
  public abstract Float getHeight();

  @SchemaCreate
  public static MyPersonClass create(String name, Integer age, Float height) {
    return new AutoValue_MyPersonClass(name, age, height);
  }
}

最后,如果您不能自己修改 class(可能是因为您没有包含 AutoValue 类的源代码),您可以手动注册它,如下所示:

pipeline.getSchemaRegistry().registerSchemaProvider(
    MyPersonClass.class, new AutoValueSchema());

公认的答案非常好。

我的 2 美分,在 AutoValueSchema 中存在一个约束,ReflectionUtils#isGtter,它期望 AutoValue 的字段遵循 get* 约定。 如果您遵循将 getter 命名为 field() 而不是 getField() 的约定,则 AutoValueSchema 不会将它们注册为实际的 getter 方法,因此不会作为用于创建模式的属性。 (最后一点对我来说有点模糊,因为我不确定通过 getter 识别属性的完整流程,必须更详细地阅读源代码)。

因此,到目前为止,您必须将所有 AutoValue getter 命名为 get*() 才能正确使用 Beam 的 AutoValueSchema。

有关详细信息,请参阅: https://github.com/apache/beam/pull/7334https://github.com/apache/beam/pull/7334#issuecomment-453560743

暂无
暂无

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

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