0

我有以下文件:

#user.avsc
{"namespace": "example.avro",
 "type": "record",
 "name": "user",
 "fields": [
     {"name": "f1", "type": "string"},
     {"name": "f2", "type": ["null", "string"], "default": null}
 ]
}

#user.json
{"f1": "Alyssa"}

然后当我做

ajava -jar ~/avro-tools-1.10.1.jar fromjson --schema-file user.avsc user.json > user.avro

我会得到错误:

    21/03/06 14:32:56 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Exception in thread "main" org.apache.avro.AvroTypeException: Expected start-union. Got END_OBJECT
        at org.apache.avro.io.JsonDecoder.error(JsonDecoder.java:511)
        at org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:430)
        at org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:282)
        at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:187)
        at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:160)
        at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:259)
        at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:247)
        at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:179)
        at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:160)
        at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
        at org.apache.avro.tool.DataFileWriteTool.run(DataFileWriteTool.java:89)
        at org.apache.avro.tool.Main.run(Main.java:67)
        at org.apache.avro.tool.Main.main(Main.java:56)

我想我已经正确设置了可选字段的架构,但是为什么仍然存在这个错误?当未提供 f2 时,应将其设置为默认 null 对吗?

谢谢

4

0 回答 0