如何修复预期的启动工会。在命令行上将JSON转换为Avro时得到了VALUE_NUMBER_INT?
根据道格·切特(Doug Cutting)的解释,
Avro的JSON编码要求将非null联合值标记为其预期类型。这是因为在JSON中,像[“ bytes”,“ string”]和[“ int”,“ long”]这样的并集是模棱两可的,第一个都编码为JSON字符串,而第二个都编码为JSON数字。
http://avro.apache.org/docs/current/spec.html#json_encoding
因此,您的记录必须编码为:
{"name": "Alyssa", "favorite_number": {"int": 7}, "favorite_color": null}
我正在尝试使用Avro模式验证JSON文件并写入相应的Avro文件。首先,我定义了以下名为Avro的架构user.avsc
:
{"namespace": "example.avro","type": "record","name": "user","fields": [
{"name": "name","type": "string"},{"name": "favorite_number","type": ["int","null"]},{"name": "favorite_color","type": ["string","null"]}
]
}
然后创建一个user.json
文件:
{"name": "Alyssa","favorite_number": 256,"favorite_color": null}
然后尝试运行:
java -jar ~/bin/avro-tools-1.7.7.jar fromjson --schema-file user.avsc user.json > user.avro
但是我得到以下异常:
Exception in thread "main" org.apache.avro.AvroTypeException: Expected start-union. Got VALUE_NUMBER_INT
at org.apache.avro.io.JsonDecoder.error(JsonDecoder.java:697)
at org.apache.avro.io.JsonDecoder.readIndex(JsonDecoder.java:441)
at org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:290)
at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
at org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:267)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:155)
at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:193)
at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:183)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:151)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:142)
at org.apache.avro.tool.DataFileWriteTool.run(DataFileWriteTool.java:99)
at org.apache.avro.tool.Main.run(Main.java:84)
at org.apache.avro.tool.Main.main(Main.java:73)
我想念什么吗?为什么会出现“期望的开始工会。得到VALUE_NUMBER_INT”的消息。
你可能想看: