Json String to Java Object Avro

 超级独自旅行也快乐 发布于 2022-12-04 15:18

我正在尝试使用Avro架构将Json字符串转换为通用Java对象.

以下是我的代码.

String json = "{\"foo\": 30.1, \"bar\": 60.2}";
String schemaLines = "{\"type\":\"record\",\"name\":\"FooBar\",\"namespace\":\"com.foo.bar\",\"fields\":[{\"name\":\"foo\",\"type\":[\"null\",\"double\"],\"default\":null},{\"name\":\"bar\",\"type\":[\"null\",\"double\"],\"default\":null}]}";

InputStream input = new ByteArrayInputStream(json.getBytes());
DataInputStream din = new DataInputStream(input);

Schema schema = Schema.parse(schemaLines);

Decoder decoder = DecoderFactory.get().jsonDecoder(schema, din);

DatumReader reader = new GenericDatumReader(schema);
Object datum = reader.read(null, decoder);


我得到"org.apache.avro.AvroTypeException:期望的start-union.得到VALUE_NUMBER_FLOAT"异常.

如果模式中没有联合,则相同的代码可以工作.有人可以解释并给我一个解决方案.

3 个回答
  • 感谢Reza.我找到了这个网页.它介绍了如何将Json字符串转换为avro对象.

    http://rezarahim.blogspot.com/2013/06/import-org_26.html

    他的代码的关键是:

    static byte[] fromJsonToAvro(String json, String schemastr) throws Exception {
      InputStream input = new ByteArrayInputStream(json.getBytes());
      DataInputStream din = new DataInputStream(input);
    
      Schema schema = Schema.parse(schemastr);
    
      Decoder decoder = DecoderFactory.get().jsonDecoder(schema, din);
    
      DatumReader<Object> reader = new GenericDatumReader<Object>(schema);
      Object datum = reader.read(null, decoder);
    
      GenericDatumWriter<Object>  w = new GenericDatumWriter<Object>(schema);
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    
      Encoder e = EncoderFactory.get().binaryEncoder(outputStream, null);
    
      w.write(datum, e);
      e.flush();
    
      return outputStream.toByteArray();
    }
    
    String json = "{\"username\":\"miguno\",\"tweet\":\"Rock: Nerf paper, scissors is fine.\",\"timestamp\": 1366150681 }";
    
    String schemastr ="{ \"type\" : \"record\", \"name\" : \"twitter_schema\", \"namespace\" : \"com.miguno.avro\", \"fields\" : [ { \"name\" : \"username\", \"type\" : \"string\", \"doc\"  : \"Name of the user account on Twitter.com\" }, { \"name\" : \"tweet\", \"type\" : \"string\", \"doc\"  : \"The content of the user's Twitter message\" }, { \"name\" : \"timestamp\", \"type\" : \"long\", \"doc\"  : \"Unix epoch time in seconds\" } ], \"doc:\" : \"A basic schema for storing Twitter messages\" }";
    
    byte[] avroByteArray = fromJsonToAvro(json,schemastr);
    
    Schema schema = Schema.parse(schemastr);
    DatumReader<Genericrecord> reader1 = new GenericDatumReader<Genericrecord>(schema);
    
    Decoder decoder1 = DecoderFactory.get().binaryDecoder(avroByteArray, null);
    GenericRecord result = reader1.read(null, decoder1);
    

    2022-12-11 02:08 回答
  • 对于使用Avro-1.8.2的任何人,JsonDecoder现在都无法在程序包外部直接实例化org.apache.avro.io。您可以使用DecoderFactory它,如以下代码所示:

    String schemaStr = "<some json schema>";
    String genericRecordStr = "<some json record>";
    Schema.Parser schemaParser = new Schema.Parser();
    Schema schema = schemaParser.parse(schemaStr);
    DecoderFactory decoderFactory = new DecoderFactory();
    Decoder decoder = decoderFactory.jsonDecoder(schema, genericRecordStr);
    DatumReader<GenericData.Record> reader =
                new GenericDatumReader<>(schema);
    GenericRecord genericRecord = reader.read(null, decoder);
    

    2022-12-11 02:10 回答
  • 使用Avro 1.4.1,这适用于:

    private static GenericData.Record parseJson(String json, String schema)
        throws IOException {
      Schema parsedSchema = Schema.parse(schema);
      Decoder decoder = new JsonDecoder(parsedSchema, json);
    
      DatumReader<GenericData.Record> reader =
          new GenericDatumReader<>(parsedSchema);
      return reader.read(null, decoder);
    }
    

    可能需要对以后的Avro版本进行一些调整.

    2022-12-11 02:57 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有