将json文件转换为avro时出错

liwlm1x9  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(456)

我按照avro网站上的说明创建了一个json和一个模式文件,如下所示(都是文本文件):
json文件

{"name": "user", "favorite_number": null, "favorite_color": "red"}
{"name": "user", "favorite_number": null, "favorite_color": "green"}
{"name": "user", "favorite_number": null, "favorite_color": "purple"}
{"name": "user", "favorite_number": null, "favorite_color": null}

以及架构文件:

{"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number",  "type": ["int", "null"]},
{"name":"favorite_color", "type": ["string", "null"]}
]
}

当我尝试使用 avro-tools jar文件,我得到以下错误消息:

Exception in thread "main" org.apache.avro.AvroTypeException: Expected start-uni
on. Got VALUE_STRING
    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:29
0)
    at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
    at org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:2
67)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.ja
va:155)
    at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumRead
er.java:193)
    at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumRea
der.java:183)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.ja
va:151)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.ja
va: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)

有人能帮我弄清楚吗。我做错了什么?

lbsnaicq

lbsnaicq1#

更正json输入的前三行,如下所示,然后重试。

{"name": "user", "favorite_number": null, "favorite_color":{"string": "red"}}
{"name": "user", "favorite_number": null, "favorite_color":{"string": "green"}}
{"name": "user", "favorite_number": null, "favorite_color":{"string":"purple"}}

相关问题