Avro架构规范不会采用相同的命名空间

时间:2015-04-20 15:00:56

标签: schema avro

我定义了一个架构:

{ "namespace":"configschemas.avro",
  "type":"record",
  "name":"pathObject",
  "fields":
  [
    {   "name":"pathString", 
        "type" : "string", 
        "default" : "null"
    }
    ,
    {   "name":"needsConversion",
         "type" : "boolean" ,
          "default" : false
    }
    ]
}

编译上述架构后,第二个架构将无法编译。

{ "namespace" : "configschemas.avro",
  "type" : "array" , 
  "items" : configschemas.avro.pathObject
}

所有模式都在同一目录下,名称空间也相同。无法得到这个缺陷。

编译第二个架构时出错:

Input files to compile:
  logPaths.avsc
Exception in thread "main" org.apache.avro.SchemaParseException: org.codehaus.jackson.JsonParseException: Unexpected character ('p' (code 112)): expected a vali
d value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: logPaths.avsc; line: 3, column: 13]
        at org.apache.avro.Schema$Parser.parse(Schema.java:967)
        at org.apache.avro.Schema$Parser.parse(Schema.java:932)
        at org.apache.avro.tool.SpecificCompilerTool.run(SpecificCompilerTool.java:73)
        at org.apache.avro.tool.Main.run(Main.java:84)
        at org.apache.avro.tool.Main.main(Main.java:73)
Caused by: org.codehaus.jackson.JsonParseException: Unexpected character ('p' (code 112)): expected a valid value (number, String, array, object, 'true', 'false
' or 'null')
 at [Source: logPaths.avsc; line: 3, column: 13]
        at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
        at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
        at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
        at org.codehaus.jackson.impl.Utf8StreamParser._handleUnexpectedValue(Utf8StreamParser.java:2090)
        at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:555)
        at org.codehaus.jackson.map.deser.std.BaseNodeDeserializer.deserializeObject(JsonNodeDeserializer.java:192)
        at org.codehaus.jackson.map.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:58)
        at org.codehaus.jackson.map.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:15)
        at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
        at org.codehaus.jackson.map.ObjectMapper.readTree(ObjectMapper.java:1344)
        at org.apache.avro.Schema$Parser.parse(Schema.java:965)
        ... 4 more** 

1 个答案:

答案 0 :(得分:0)

我不确定你是如何调用模式解析器的,但是将两个模式放在同一个模式文件中应该可以正常工作,因为这证明了

@Grapes([
    @Grab(group='org.apache.avro', module='avro', version='1.7.7')
])

import org.apache.avro.Schema;

String schema = '''
{
  "namespace":"configschemas.avro",
  "type":"record",
  "name":"pathObject",
  "fields":[
    {
      "name":"pathString", 
      "type":"string", 
      "default":"null"
    },
    {
      "name":"needsConversion",
      "type":"boolean",
      "default":false
    }
  ]
}

{
  "namespace":"configschemas.avro",
  "type":"array", 
  "items":configschemas.avro.pathObject
}'''

try {
    System.out.println(new Schema.Parser().parse(schema));
} catch (Throwable t) {
    t.printStackTrace();
}

因此,如果要么一起加载命名空间中的所有模式,它应该可以工作(您可以将它们保存在单独的文件中,只需将文件中的文本一起加载)。