org.apache.avro.AvroTypeException:预期的记录开始。得到了VALUE_STRING

问题描述 投票:0回答:1

我正在将简单的json转换为Avro Record,但是我遇到了这个问题,我尝试了很多方法,我从stackoverflow和在线应用了超过15种解决方案。

我的文件看起来像这样

{
    "namespace": "test",
    "type": "record",
    "name": "root",
    "doc": "This stream contains raw data.",
    "fields": [
        {
            "name": "aaa",
            "doc": "You should not edit this portion.",
            "type": {
                "type": "record",
                "name": "EnterpriseEventEnvelopeRecord",
                "fields": [
                    {
                        "name": "eventId",
                        "type": "string",
                        "default": "",
                        "doc": "Unique Identifier."
                    },
                    {
                        "name": "eventAction",
                        "type": [
                            "null",
                            {
                                "type": "enum",
                                "name": "actionTypes",
                                "symbols": [
                                    "Updated",
                                    "Created",
                                    "Requested",
                                    "Deleted",
                                    "Verified",
                                    "Received",
                                    "Completed",
                                    "Failed",
                                    "Abandoned"
                                ]
                            }
                        ],
                        "default": null,
                        "doc": "A verb indicating what happened."
                    }
                ]
            }
        }
    ]
}

我的输入json:

{“ aaa”:{“ eventId”:“ omar”,“ eventAction”:“ Requested”}}]

我的班级:

import org.apache.avro.Schema;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.Encoder;

import java.io.*;

import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Paths.get;

public class FeedbackEvent {
    public static void main(String args[]) throws Exception{
        String jsonFile = "d:/aaa.txt";
        String scemaFile = "d:/aaa.avsc";
        Schema schema = new Schema.Parser().parse(new File(scemaFile));
        String json = new String(readAllBytes(get(jsonFile)));
        jsonToAvro(json,schema);
        System.out.println("Done....");
    }

    public static byte[] jsonToAvro(String json, Schema schema) throws IOException {
        InputStream input = null;
        DataFileWriter<GenericRecord> writer = null;
        Encoder encoder = null;
        ByteArrayOutputStream output = null;
        try {
            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();
            System.out.println(output);
            return output.toByteArray();
        } finally {
            try { input.close(); } catch (Exception e) { }
        }
    }
}

我花了超过5个小时仍没有解决方案,需要帮助

apache-spark apache-kafka avro spark-avro
1个回答
0
投票

如果不为空,则应指定要使用的联合分支。请参考UnionsJson Encoding

根据您的情况,actionTypes。所以json应该看起来像

{
  "aaa": {
    "eventId": "omar",
    "eventAction": {
        "test.actionTypes": "Requested"
    }
  }
}

您可能会注意到我们也使用了namespace和union分支; this Stackoverflow线程中提供清晰的说明。

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.