Java Json 到 avro 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29692144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:27:08  来源:igfitidea点击:

Json to avro conversion

javajsonavro

提问by shree11

I'm converting Json to avro. I have json data in JSONArray. So while converting it into byte array i'm facing the problem.

我正在将 Json 转换为 avro。我在 .json 中有 json 数据 JSONArray。因此,在将其转换为字节数组时,我遇到了问题。

below is my code:

下面是我的代码:

static byte [] fromJsonToAvro(JSONArray json, String schemastr) throws Exception {

ExcelToJson ejj = new ExcelToJson();
List<String> list = new ArrayList<String>();


if (json != null) { 
    int len = json.length();
    for (int i=0;i<len;i++){ 
        list.add(json.get(i).toString());
    } 
}


InputStream input = new ByteArrayInputStream(list.getBytes()); //json.toString().getBytes()

 DataInputStream din = new DataInputStream(input); 
                  .
                  . 
                  .//rest of the logic

So how can i do it? How to convert JsonArray object to bytes(i.e., how to use getBytes() method for JsonArray objects). The above code giving an error at list.getBytes()and saying getBytes() is undifined for list.

那么我该怎么做呢?如何将 JsonArray 对象转换为字节(即如何对 JsonArray 对象使用 getBytes() 方法)。上面的代码给出了一个错误list.getBytes()并说 getBytes() 对于列表是 undifined。

回答by Keegan

Avro works at the record level, bound to a schema. I don't think there's such a concept as "convert this JSON fragment to bytes for an Avro field independent of any schema or record".

Avro 在记录级别工作,绑定到模式。我不认为有“将这个 JSON 片段转换为独立于任何模式或记录的 Avro 字段的字节”这样的概念。

Assuming the array is part of a larger JSON record, if you're starting with a string of the record, you could do

假设数组是更大的 JSON 记录的一部分,如果您从记录的字符串开始,您可以这样做

public static byte[] jsonToAvro(String json, String schemaStr) throws IOException {
    InputStream input = null;
    DataFileWriter<GenericRecord> writer = null;
    Encoder encoder = null;
    ByteArrayOutputStream output = null;
    try {
        Schema schema = new Schema.Parser().parse(schemaStr);
        DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema);
        input = new ByteArrayInputStream(json.getBytes());
        output = new ByteArrayOutputStream();
        DataInputStream din = new DataInputStream(input);
        writer = new DataFileWriter<GenericRecord>(new GenericDatumWriter<GenericRecord>());
        writer.create(schema, output);
        Decoder decoder = DecoderFactory.get().jsonDecoder(schema, din);
        GenericRecord datum;
        while (true) {
            try {
                datum = reader.read(null, decoder);
            } catch (EOFException eofe) {
                break;
            }
            writer.append(datum);
        }
        writer.flush();
        return output.toByteArray();
    } finally {
        try { input.close(); } catch (Exception e) { }
    }
}

回答by ppearcy

This discussion is likely useful:

这个讨论可能有用:

http://mail-archives.apache.org/mod_mbox/avro-user/201209.mbox/%3CCALEq1Z8s1sfaAVB7YE2rpZ=v3q1V_h7Vm39h0HsOzxJ+qfQRSg@mail.gmail.com%3E

http://mail-archives.apache.org/mod_mbox/avro-user/201209.mbox/%3CCALEq1Z8s1sfaAVB7YE2rpZ=v3q1V_h7Vm39h0HsOzxJ+qfQRSg@mail.gmail.com%3E

The gist is that there is a special Json schema and you can use JsonReader/Writer to get to and from that. The Json schema you should use is defined here:

要点是有一个特殊的 Json 模式,您可以使用 JsonReader/Writer 来访问和访问它。您应该使用的 Json 架构在此处定义:

https://github.com/apache/avro/blob/trunk/share/schemas/org/apache/avro/data/Json.avsc

https://github.com/apache/avro/blob/trunk/share/schemas/org/apache/avro/data/Json.avsc

回答by Antonios Chalkiopoulos

For an on-line json to avro converter check the following URL

对于在线 json 到 avro 转换器,请检查以下 URL

http://avro4s-ui.landoop.com

http://avro4s-ui.landoop.com

It is using the library avro4sthat offers a lot of conversions including json=>avro

它使用的库avro4s提供了很多转换,包括 json=>avro