是否可以将AVRO模式中的java类重用为类型?

时间:2018-03-15 11:33:31

标签: avro

是否可以在avsc文件中引用用Java实现的类?

我的想法是让它像以下一样工作:

{
    "namespace": "com.myCompany.myProject.schemas",
    "type": "record",
    "name": "SimpleTest",
    "fields": [
      {"name": "text","type": "string"},
      {"name": "myOtherObj","type": "com.myCompany.myProject.MyClass"}
      ]
}

其中myOtherObj具有已定义的类MyClass的类型。

1 个答案:

答案 0 :(得分:1)

我的解决方案是: 我定义了一个文件夹,我在其中定义了基本类型MyClass.avsc:

myfile

然后,使用gradle插件:https://github.com/commercehub-oss/gradle-avro-plugin,我与此一起构建新的avro类,并在编译后删除实现

{
  "namespace": "com.myCompany.myProject.basicTypes",
  "type": "record",
  "name": "MyClass",
  "doc": "This is only a place-holder needed to let Avro compile correctly the schemas. The real implementation is provided in the java project",
  "fields": [
        {"name": "value", "type": "string"}
    ]
}

此MyClass的实际实现是作为依赖项提供的,或者在我包含此生成//remove the basicTypes as these are only place holder while the real implementation is somewhere else. task removeBasicTypes { dependsOn compileJava doLast { println "Removing java files of all the basic types." //cleanup the generated java classes delete fileTree(dir: 'src/main/java/com/myCompany/myProject/basicTypes' , include: '**/*.java') //cleanup also the build folder that will be used to generate the jar file delete fileTree(dir: 'build/classes/java/main/com/myCompany/myProject/basicTypes' , include: '**/*.class') } } task generateAvro(type: com.commercehub.gradle.plugin.avro.GenerateAvroJavaTask) { source("$rootDir/basicTypes", "src/main/avro") outputDir = file("src/main/java") finalizedBy('removeBasicTypes') } 文件的项目中提供。