Avro模式中的多态性和继承 [英] Polymorphism and inheritance in Avro schemas

查看:120
本文介绍了Avro模式中的多态性和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写Avro模式/IDL来生成Java类,该Java类可以扩展基类或实现接口? 似乎生成的Java类扩展了org.apache.avro.specific.SpecificRecordBase.因此,工具可能是必经之路.但是,我不知道这是否可能.

Is it possible to write an Avro schema/IDL that will generate a Java class that either extends a base class or implements an interface? It seems like the generated Java class extends the org.apache.avro.specific.SpecificRecordBase. So, the implements might be the way to go. But, I don't know if this is possible.

我已经看到了一些示例,这些示例提出了在每个特定模式中定义一个显式类型"字段的建议,并且具有比继承语义更多的关联性.

I have seen examples with suggestions to define an explicit "type" field in each specific schema, with more of an association than inheritance semantics.

我在工厂类和代码的其他部分中大量使用基类,并使用诸如<T extends BaseObject>之类的泛型.目前,我已经从支持继承的JSON模式生成了代码.

I use my base class heavily in my factory classes and other parts of the code with generics like <T extends BaseObject>. Currently, I had it code generated from the JSON Schema, which supports inheritance.

另一个问题:您可以使用IDL来定义没有协议定义的记录吗?我认为答案是否定的,因为编译器抱怨缺少协议关键字.

Another side question: can you use IDL to define just records without the protocol definition? I think the answer is no because the compiler complains about the missing protocol keyword.

帮助表示赞赏!谢谢.

推荐答案

我找到了解决此问题的更好方法.查看Avro中的Schema生成源,我发现在内部,类生成逻辑使用Velocity模式生成类.

I found a better way to solve this problem. Looking at the Schema generation source in Avro, I figured out that internally the class generation logic uses Velocity schemas to generate the classes.

我修改了record.vm模板以也实现我的特定接口.有一种方法可以使用Maven构建插件中的templateDirectory配置指定速度目录的位置.

I modified the record.vm template to also implement my specific interface. There is a way to specify the location of velocity directory using the templateDirectory configuration in the maven build plugin.

我还改用了SpecificDatumWriter而不是reflectDatumWriter.

<plugin>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-maven-plugin</artifactId>
   <version>${avro.version}</version>
   <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>schema</goal>
      </goals>
      <configuration>
         <sourceDirectory>${basedir}/src/main/resources/avro/schema</sourceDirectory>
         <outputDirectory>${basedir}/target/java-gen</outputDirectory>
         <fieldVisibility>private</fieldVisibility>
         <stringType>String</stringType>
         <templateDirectory>${basedir}/src/main/resources/avro/velocity-templates/</templateDirectory>
       </configuration>
    </execution>
  </executions>
</plugin>

这篇关于Avro模式中的多态性和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆