- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做简单的 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) { }
}
}
}
最佳答案
如果不为空,则应指定要使用的联合分支。请引用Unions和 Json 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/
我正在尝试找到一种方法来构建我的 $params 数组,使用 native 语法来搜索名称等于“test”的帖子: $params = [ "index" => "post_index",
Elasticsearch scroll api 给了我这个错误 { "error": { "root_cause": [ { "type"
我正在尝试使用 fasterxml jackson 反序列化 json,Json 中的一个字段是一个字符串,但我需要在 Bean 和主类的字节数组 PFB 中读取相同内容 public class S
我开发了一个软件,它运行正常,直到我的 mysql 服务器崩溃。崩溃后,我格式化了我的服务器,然后安装了我需要的一切,就像过去的美好时光一样。格式化后,所有软件似乎运行正常,但我开发的软件开始出现一些
启动 Elasticsearch 服务器时出现以下异常 Exception in thread "main" org.elasticsearch.common.settings.SettingsExc
我有一个使用 Jersey 构建并部署在 AppEngine 中的 REST 服务。 REST 服务实现了使用 application/json 媒体类型的动词 PUT。数据绑定(bind)由 Jac
搜索时,我将elasticsearch用于城市的下拉列表。 Error: Elasticsearch::Transport::Transport::Errors::BadRequest Unknown
我有以下 avsc(Avro 架构): { "type": "record", "name": "DataEventId", "fields": [ { "name":
我正在开发一个应用程序,我想将一些数据发送到服务器并获得响应。我应该使用的格式如下。 { "userName" :"testuser2@test.com", "targetUserName
我试图使用 Spring 休息模板获取 getAccessToken ,如下所示。我在调用 oAuthRestTemplate.getAccessToken() 时收到以下错误 Exception i
我正在使用 Jackson 反序列化来自不属于我的服务器的 json 响应。我正在使用 JsonTypeInfo 注释来处理多态数据类型。这是我的基本类型(在本例中为 Thing)的配置: @Json
无法从 VALUE_STRING token [...] 中反序列化 java.lang.String[] 的实例(通过引用链:[...].model.User["ethnicities"]) 我有一
我在堆栈溢出中寻找过其他帖子,但没有一个适合我。这是一段代码: public class Forward implements Serializable { private List freq;
我正在做简单的 json 到 Avro Record 的转换,但是我遇到了这个问题,我尝试了很多方法,我应用了来自 stackoverflow 和在线的超过 15 个解决方案。 我的文件看起来像这样
我有这样的xml: ... ... 还有我的 POJO 类: @JsonIgnoreProperties(ignoreUnknown = true) pub
使用Elasticsearch 5.5,发布这个批量请求时出现以下错误,无法弄清楚请求有什么问题。 "type": "illegal_argument_exception", "reason": "M
我有这个对象树 一个 B 扩展 A C 扩展 B D 扩展 B E 扩展 C F 扩展了 A 并且有一个对 A 的引用 A有如下注解 @JsonTypeInfo(use=JsonTypeInfo.Id
我已经浏览了以下几个链接,但它并没有解决我的问题: Java Jackson: deserialize complex polymorphic object model: JsonMappingExc
使用 Jackson,我正在尝试用 JSON 编码数据,但它给出了异常。 我尝试了 String 数据和 byte[] 数据: String representation of same data i
我在这里阅读了很多关于将 JSON 解析为 Java 对象的帖子,在我引入 LocalDateTime 之前,我的解析工作正常。我曾尝试使用 Java 8 解析器、JSR310 模块并构建自定义 -
我是一名优秀的程序员,十分优秀!