-1

我正在使用 avro maven 插件为 avro .avsc 模式文件生成 java 代码,我有一个通用模式,它在多个地方作为单独的记录使用,当我在每个地方提供不同的命名空间时,它能够生成 java 代码,但是生成的代码位于不同的文件夹中,尽管两个类的代码相同

有没有办法像上面的方案那样只生成单个类以供共同参考...这是我的 avsc

{
"namespace": "exmaple.avro",
"type": "record",
"name": "TopRecord",
"fields": [{
        "name": "id",
        "type": "string"
    },
    {
        "name": "amount",
        "type": "double"
    },
    {
        "name": "AC",
        "type": {
            "type": "record",
            "name": "AC_SCHEMA",
            "fields": [{
                    "name": "id",
                    "type": "string"
                },
                {
                    "name": "amount",
                    "type": "double"
                },
                {
                    "name": "InnerCommon",
                    "type": {
                        "type": "record",
                        "name": "InnerSchema",
                        "fields": [{
                            "name": "id",
                            "type": "string"
                        }]
                    }
                }
            ]
        }

    }, {
        "name": "BC",
        "type": {
            "type": "record",
            "name": "BC_SCHEMA",
            "fields": [{
                    "name": "id",
                    "type": "string"
                },
                {
                    "name": "amount",
                    "type": "double"
                },
                {
                    "name": "InnerCommon",
                    "type": {
                        "type": "record",
                        "name": "InnerSchema",
                        "fields": [{
                            "name": "id",
                            "type": "string"
                        }]
                    }
                }
            ]
        }

    }
]
}

如果我在两个位置都为 InnerCommon 模式提供不同的命名空间,它能够生成代码,但在 2 个文件夹中具有相同代码的类:(

这是使用命名空间的 avsc

{
"namespace": "exmaple.avro",
"type": "record",
"name": "TopRecord",
"fields": [{
        "name": "id",
        "type": "string"
    },
    {
        "name": "amount",
        "type": "double"
    },
    {
        "name": "AC",
        "type": {
            "type": "record",
            "name": "AC_SCHEMA",
            "fields": [{
                    "name": "id",
                    "type": "string"
                },
                {
                    "name": "amount",
                    "type": "double"
                },
                {
                    "name": "InnerCommon",
                    "type": {
                        "type": "record",
                        "name": "InnerSchema",
                        "namespace": "inner1",
                        "fields": [{
                            "name": "id",
                            "type": "string"
                        }]
                    }
                }
            ]
        }

    }, {
        "name": "BC",
        "type": {
            "type": "record",
            "name": "BC_SCHEMA",
            "fields": [{
                    "name": "id",
                    "type": "string"
                },
                {
                    "name": "amount",
                    "type": "double"
                },
                {
                    "name": "InnerCommon",
                    "type": {
                        "type": "record",
                        "name": "InnerSchema",
                        "namespace": "inner2",
                        "fields": [{
                            "name": "id",
                            "type": "string"
                        }]
                    }
                }
            ]
        }

    }
]
}

这是生成的文件夹结构

生成的文件夹结构

有什么办法可以将所有常见的生成的东西放在单个文件夹中并具有相同的命名空间来删除​​重复项?

编辑1:我需要将它注册到模式注册表并检查进化,我想知道是否有任何方法可以告诉插件不要覆盖生成的代码并且只放置一个类

4

1 回答 1

0

您可以将公共定义移动到它们自己的 avsc 文件中,然后在您的 pom.xml 规范中为 avro-maven-plugin 导入公共文件:

    <plugins>
        <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>${avro.schema.dir}</sourceDirectory>
                        <imports>
                            <import>${my.common.dir}/my_common_type_1.avsc</import>
                            <import>${my.common.dir}/my_common_type_2.avsc</import>
                        </imports>
                    </configuration>
                </execution>
            </executions>
        </plugin>
于 2019-08-08T15:12:04.860 回答