在AVRO模式中将外部java类用作类型
是否可以在avsc文件中引用已经实现的Java类 我的想法是有如下内容:在AVRO模式中将外部java类用作类型,java,avro,Java,Avro,是否可以在avsc文件中引用已经实现的Java类 我的想法是有如下内容: { "namespace": "com.myCompany.myProject.schemas", "type": "record", "name": "SimpleTest", "fields": [ {"name": "text","type": "string"}, {"name": "myOtherObj","type": "com.myCompany.myP
{
"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插件:,我与此一起构建了avro类SimpleTest
。
通过这种方式,Avro将创建SimpleTest.java
和MyClass.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的真正实现将作为依赖项提供,或者在我包含这个generate
SimpleTest.java
文件的项目中提供。您是在编译模式还是使用genericrecords?我正在将模式编译为java。(我注意到我可以将一些“com.myCompany.myProject.MyClass”记录定义为占位符,然后在构建后将其删除。您可以提供到目前为止的编译方式以及占位符的含义吗?谢谢!