簡體   English   中英

使用外部 java 類作為 AVRO 模式中的類型

[英]Use of an external java class as a type in an AVRO schema

是否可以在 avsc 文件中引用已實現的 Java 類?

我的想法是有類似的東西:

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

其中字段myOtherObj類型是已經定義的已構建的 java 類MyClass

我的解決方案是:我定義了一個文件夾,在其中通過MyClass.avsc定義了基本類型MyClass的虛擬版本:

{
  "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"}
    ]
}

然后,使用 gradle 插件: https : //github.com/commercehub-oss/gradle-avro-plugin ,我與它一起構建了 avro 類SimpleTest 通過這種方式,Avro 將創建SimpleTest.javaMyClass.java並正確解析它們的依賴關系。 最后,我從類路徑中刪除了 Avro 插件創建的MyClass.java的實現。

//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')
}

這個 MyClass 的真正實現將作為依賴項或在我包含這個生成SimpleTest.java文件的項目中提供。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM