如何使用不同的架构验证Avro格式的数据

时间:2018-06-27 10:20:10

标签: java avro avro-tools

private static final String json = "{" + "\"name\":\"Frank\"," + "\"age\":\"47\"" + "}";
private static final Schema schema1 = new Schema.Parser().parse("{ \"type\":\"record\", \"namespace\":\"foo\", \"name\":\"Person\", \"fields\":[ { \"name\":\"name\", \"type\":\"string\" }, { \"name\":\"age\", \"type\":\"string\" } ] }");
private static final Schema schema2 = new Schema.Parser().setValidate(true).parse("{ \"type\":\"record\", \"namespace\":\"foo\", \"name\":\"Person\", \"fields\":[ { \"name\":\"name\", \"type\":\"string\" }, { \"name\":\"age\", \"type\":\"int\" } ] }");

1)如果我们要使用schema1在json上方进行验证,则应该说为真

2)如果我们要使用schema2在json上方进行验证,则由于“ age”字段根据模式为int,因此应该说为false,但在json中为字符串

public static boolean validateJson(byte[] data, Schema schema) throws Exception {
    InputStream input = new ByteArrayInputStream(data);
    DataInputStream din = new DataInputStream(input);
    GenericDatumReader<Object> reader = null;
    Object datum = null;
    BinaryDecoder binaryDecoder =
            DecoderFactory.get().binaryDecoder(input, null);
    try {
        reader = new GenericDatumReader<Object>(schema);
        DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema);
        Decoder decoder = DecoderFactory.get().binaryDecoder(input, null);
        reader.read(din, decoder);
        return true;
    } catch (AvroTypeException e) {
        System.out.println(e.getMessage());
        return false;
    }
}

public static byte[] jsonToAvro(String json, Schema schema) throws IOException {
    DatumReader<Object> reader = new GenericDatumReader<>(schema);
    GenericDatumWriter<Object> writer = new GenericDatumWriter<>(schema);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Decoder decoder = DecoderFactory.get().jsonDecoder(schema, json);
    Encoder encoder = EncoderFactory.get().binaryEncoder(output, null);
    Object datum = reader.read(null, decoder);
    writer.write(datum, encoder);
    encoder.flush();
    return output.toByteArray();
}

0 个答案:

没有答案