gpt4 book ai didi

java - 使用外部 java 类作为 AVRO 模式中的类型

转载 作者:行者123 更新时间:2023-11-30 06:15:30 27 4
gpt4 key购买 nike

是否可以在 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 文件的项目中提供。

关于java - 使用外部 java 类作为 AVRO 模式中的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49298361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com