Я делаю простое json преобразование в Avro Record, но я получаю эту проблему, я перепробовал много способов, я применил более 15 решений из stackoverflow и онлайн.
Мой файл выглядит следующим образом
{
"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": "Запрошено"}}
Мой класс:
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) { }
}
}
}