gpt4 book ai didi

java - org.apache.avro.AvroTypeException : Expected record-start. 得到 VALUE_STRING

转载 作者:行者123 更新时间:2023-12-02 08:49:07 28 4
gpt4 key购买 nike

我正在做简单的 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": "已请求"}}

我的类(class):

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) { }
}
}
}

最佳答案

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

就您而言,actionTypes。所以 json 应该看起来像

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

您可能会注意到我们还使用了命名空间以及联合分支;清楚的解释可以在 this Stackoverflow 中找到。线程。

希望这有帮助。

关于java - org.apache.avro.AvroTypeException : Expected record-start. 得到 VALUE_STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60899195/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com