有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用类生成Avro消息

现在,我正在从avsc模式文件创建avro消息。使用下面的代码片段

static byte[] fromJasonToAvro(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();
    }


    public static void main(String[] args) throws Exception {

        StringBuilder sb = new StringBuilder();
        StringBuilder jsb = new StringBuilder();

        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream is = classloader.getResourceAsStream("RsvpAvroSchema.avsc");
        InputStream js = classloader.getResourceAsStream("JsonMessage.dat");
        
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        InputStreamReader jisr = new InputStreamReader(js, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        BufferedReader jbr = new BufferedReader(jisr);
        br.lines().forEach(line -> sb.append(line));
        jbr.lines().forEach(line -> jsb.append(line));

        System.out.println(sb);
        System.out.println(jsb);
        
        System.out.println(new String(fromJasonToAvro(jsb.toString(), sb.toString()), StandardCharsets.UTF_8));

但我也使用maven插件从avsc创建了avro类(数据结构)。但是现在不确定如何使用avro message data structurestring json消息的主类来生成avro消息

有人能分享一下怎么做吗

更新:

如何从字符串Json创建Avro对象?我的项目中已经有了avro课程

第二次更新

public class AvroInstance {

    static DecoderFactory DEFAULT_FACTORY = new DecoderFactory();
    static DatumReader<Object> reader = new GenericDatumReader<Object>(RSVP.SCHEMA$);
    static Object rsvpOB;

    public Object avroInstance(String JsonString) {

        try {
            rsvpOB = reader.read(null, DEFAULT_FACTORY.jsonDecoder(RSVP.SCHEMA$, JsonString));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rsvpOB;

    }

共 (1) 个答案

  1. # 1 楼答案

    您可以用生成的类中存储的静态SCHEMA字符串替换AVSC文件读取器,以获得相同的字符串

    你也可以使用已经做到这一点的库