我试图拥有我创建的 AutoValue 定义对象的 PCollection,并且我添加了适当的注释来通过以下方式推断架构DefaultSchema(AutoValueSchema.class)
。就像这样:
@DefaultSchema(AutoValueSchema.class)
@AutoValue
public abstract class MyAutoClas {
public abstract String getMyStr();
public abstract Integer getMyInt();
@CreateSchema
public static MyAutoClass create(String myStr, Integer myInt) {
return new AutoValue_MyAutoClass(myStr, myInt);
}
}
我有一个小测试用例,如下所示:
PCollection<KV<String, MyAutoClass>> result = pipeline
.apply(Create.of(MyAutoClass.create("abc", 1)))
.apply(WithKeys.of(in -> in.getMyStr()));
PAssert.that(result).containsInAnyOrder(KV.of("abc", MyAutoClass.create("abc", 1)));
pipeline.run().waitUntilFinish();
当我尝试运行此程序时,我看到以下错误:
[ERROR] testMyAutoValueClass(.....) Time elapsed: 1.891 s <<< ERROR!
java.lang.RuntimeException: Creator parameter arg0 Doesn't correspond to a schema field
at org.apache.beam.sdk.schemas.utils.ByteBuddyUtils$InvokeUserCreateInstruction.<init>(ByteBuddyUtils.java:717)
at org.apache.beam.sdk.schemas.utils.ByteBuddyUtils$StaticFactoryMethodInstruction.<init>(ByteBuddyUtils.java:660)
at org.apache.beam.sdk.schemas.utils.JavaBeanUtils.createStaticCreator(JavaBeanUtils.java:284)
at org.apache.beam.sdk.schemas.utils.JavaBeanUtils.lambda$getStaticCreator$4(JavaBeanUtils.java:273)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1664)
at org.apache.beam.sdk.schemas.utils.JavaBeanUtils.getStaticCreator(JavaBeanUtils.java:269)
at org.apache.beam.sdk.schemas.AutoValueSchema.lambda$schemaTypeCreatorFactory$673bce5b$1(AutoValueSchema.java:80)
at org.apache.beam.sdk.schemas.UserTypeCreatorFactory.create(UserTypeCreatorFactory.java:21)
....... [ETCETERA] ......
发生此错误的原因是 ByteBuddy 和 Java 反射实用程序无法推断您的参数名称@SchemaCreate
方法(因此抱怨一些未知参数arg0
- 这是默认名称)。
为了确保 Java 反射可以找到参数名称,您需要指示编译器包含此信息。如果您使用 Maven 进行构建,可以这样做:
<properties>
<!-- PLUGIN VERSIONS -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<!-- OTHER PROPERTIES -->
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<!-- Original answer -->
<compilerArgument>-parameters</compilerArgument>
<!-- Or, if you use the plugin version >= 3.6.2 -->
<parameters>true</parameters>
<testCompilerArgument>-parameters</testCompilerArgument>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
Note:要使用 Gradle 完成同样的事情,请使用这个答案 https://stackoverflow.com/a/37482148/1255356.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)