Avro Maven 插件:不支持类型

csc*_*can 0 maven avro

我有一个Pojo.avsc包含以下声明的文件:

{
  "namespace": "io.fama.pubsub.schema",
  "type": "record",
  "name": "Pojo",
  "fields": [
    {
      "name": "field",
      "type": "string"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我有一个PojoCollection.avsc只包含 Pojo 对象集合的文件。

{
  "namespace": "io.fama.pubsub.schema",
  "type": "record",
  "name": "PojoCollection",
  "fields": [
    {
      "name": "collection",
      "type": {
        "type": "array",
        "items": {
          "name": "pojo",
          "type": "Pojo"
        }
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的 avro-maven-plugin 配置如下:

<plugin>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro-maven-plugin</artifactId>
    <version>1.8.2</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>schema</goal>
            </goals>
            <configuration>
                <imports>
                    <import>${basedir}/src/main/avro/Pojo.avsc</import>
                </imports>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

这会导致以下异常:

Caused by: org.apache.avro.SchemaParseException: Type not supported: Pojo
    at org.apache.avro.Schema.parse(Schema.java:1319)
    at org.apache.avro.Schema.parse(Schema.java:1306)
    at org.apache.avro.Schema.parse(Schema.java:1269)
    at org.apache.avro.Schema$Parser.parse(Schema.java:1032)
    at org.apache.avro.Schema$Parser.parse(Schema.java:997)
    at org.apache.avro.mojo.SchemaMojo.doCompile(SchemaMojo.java:73)
    at org.apache.avro.mojo.AbstractAvroMojo.compileFiles(AbstractAvroMojo.java:223)
    at org.apache.avro.mojo.AbstractAvroMojo.execute(AbstractAvroMojo.java:172)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    ... 21 more
Run Code Online (Sandbox Code Playgroud)

这是 avro maven 插件错误吗?还是我的 avsc 文件有问题?

hla*_*gos 5

这是您的数组定义的问题。它应该看起来像

{
  "namespace": "io.fama.pubsub.schema",
  "type": "record",
  "name": "PojoCollection",
  "fields": [
    {
      "name": "pojosCollection",
      "type": {
        "type": "array",
        "items": "Pojo"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

数组的类型必须在items属性内定义。