- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在编码/解码 avro 生成的类时遇到了一个奇怪的问题。我得到的错误是抛出一个不是枚举错误 - 除了我的类(class)中没有任何枚举。
错误具体是这样的:
com.fasterxml.jackson.databind.JsonMappingException: Not an enum: {"type":"record","name":"TimeUpdateTopic","namespace":"org.company.mmd.time","fields":[{"name":"time","type":"double"}]} (through reference chain: org.company.mmd.time.TimeUpdateTopic["schema"]->org.apache.avro.Schema$RecordSchema["enumDefault"])
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.junit.Test
class TimeUpdateTopicTest {
val objectMapper = ObjectMapper().registerModule(JavaTimeModule())
@Test
fun decode() {
val t = TimeUpdateTopic(1.0)
objectMapper.writeValueAsString(t)
}
}
@namespace("org.company.mmd.time")
protocol TimeMonitor {
record TimeUpdateTopic {
double time;
}
}
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package org.company.mmd.time;
import org.apache.avro.generic.GenericArray;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.util.Utf8;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
@org.apache.avro.specific.AvroGenerated
public class TimeUpdateTopic extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = -4648318619505855037L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"TimeUpdateTopic\",\"namespace\":\"org.company.mmd.time\",\"fields\":[{\"name\":\"time\",\"type\":\"double\"}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder<TimeUpdateTopic> ENCODER =
new BinaryMessageEncoder<TimeUpdateTopic>(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder<TimeUpdateTopic> DECODER =
new BinaryMessageDecoder<TimeUpdateTopic>(MODEL$, SCHEMA$);
/**
* Return the BinaryMessageEncoder instance used by this class.
* @return the message encoder used by this class
*/
public static BinaryMessageEncoder<TimeUpdateTopic> getEncoder() {
return ENCODER;
}
/**
* Return the BinaryMessageDecoder instance used by this class.
* @return the message decoder used by this class
*/
public static BinaryMessageDecoder<TimeUpdateTopic> getDecoder() {
return DECODER;
}
/**
* Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
* @param resolver a {@link SchemaStore} used to find schemas by fingerprint
* @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
*/
public static BinaryMessageDecoder<TimeUpdateTopic> createDecoder(SchemaStore resolver) {
return new BinaryMessageDecoder<TimeUpdateTopic>(MODEL$, SCHEMA$, resolver);
}
/**
* Serializes this TimeUpdateTopic to a ByteBuffer.
* @return a buffer holding the serialized data for this instance
* @throws java.io.IOException if this instance could not be serialized
*/
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
return ENCODER.encode(this);
}
/**
* Deserializes a TimeUpdateTopic from a ByteBuffer.
* @param b a byte buffer holding serialized data for an instance of this class
* @return a TimeUpdateTopic instance decoded from the given buffer
* @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
*/
public static TimeUpdateTopic fromByteBuffer(
java.nio.ByteBuffer b) throws java.io.IOException {
return DECODER.decode(b);
}
@Deprecated public double time;
/**
* Default constructor. Note that this does not initialize fields
* to their default values from the schema. If that is desired then
* one should use <code>newBuilder()</code>.
*/
public TimeUpdateTopic() {}
/**
* All-args constructor.
* @param time The new value for time
*/
public TimeUpdateTopic(java.lang.Double time) {
this.time = time;
}
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
public java.lang.Object get(int field$) {
switch (field$) {
case 0: return time;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
// Used by DatumReader. Applications should not call.
@SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
switch (field$) {
case 0: time = (java.lang.Double)value$; break;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
/**
* Gets the value of the 'time' field.
* @return The value of the 'time' field.
*/
public double getTime() {
return time;
}
/**
* Sets the value of the 'time' field.
* @param value the value to set.
*/
public void setTime(double value) {
this.time = value;
}
/**
* Creates a new TimeUpdateTopic RecordBuilder.
* @return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder() {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
}
/**
* Creates a new TimeUpdateTopic RecordBuilder by copying an existing Builder.
* @param other The existing builder to copy.
* @return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder(org.company.mmd.time.TimeUpdateTopic.Builder other) {
if (other == null) {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
} else {
return new org.company.mmd.time.TimeUpdateTopic.Builder(other);
}
}
/**
* Creates a new TimeUpdateTopic RecordBuilder by copying an existing TimeUpdateTopic instance.
* @param other The existing instance to copy.
* @return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder(org.company.mmd.time.TimeUpdateTopic other) {
if (other == null) {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
} else {
return new org.company.mmd.time.TimeUpdateTopic.Builder(other);
}
}
/**
* RecordBuilder for TimeUpdateTopic instances.
*/
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<TimeUpdateTopic>
implements org.apache.avro.data.RecordBuilder<TimeUpdateTopic> {
private double time;
/** Creates a new Builder */
private Builder() {
super(SCHEMA$);
}
/**
* Creates a Builder by copying an existing Builder.
* @param other The existing Builder to copy.
*/
private Builder(org.company.mmd.time.TimeUpdateTopic.Builder other) {
super(other);
if (isValidValue(fields()[0], other.time)) {
this.time = data().deepCopy(fields()[0].schema(), other.time);
fieldSetFlags()[0] = other.fieldSetFlags()[0];
}
}
/**
* Creates a Builder by copying an existing TimeUpdateTopic instance
* @param other The existing instance to copy.
*/
private Builder(org.company.mmd.time.TimeUpdateTopic other) {
super(SCHEMA$);
if (isValidValue(fields()[0], other.time)) {
this.time = data().deepCopy(fields()[0].schema(), other.time);
fieldSetFlags()[0] = true;
}
}
/**
* Gets the value of the 'time' field.
* @return The value.
*/
public double getTime() {
return time;
}
/**
* Sets the value of the 'time' field.
* @param value The value of 'time'.
* @return This builder.
*/
public org.company.mmd.time.TimeUpdateTopic.Builder setTime(double value) {
validate(fields()[0], value);
this.time = value;
fieldSetFlags()[0] = true;
return this;
}
/**
* Checks whether the 'time' field has been set.
* @return True if the 'time' field has been set, false otherwise.
*/
public boolean hasTime() {
return fieldSetFlags()[0];
}
/**
* Clears the value of the 'time' field.
* @return This builder.
*/
public org.company.mmd.time.TimeUpdateTopic.Builder clearTime() {
fieldSetFlags()[0] = false;
return this;
}
@Override
@SuppressWarnings("unchecked")
public TimeUpdateTopic build() {
try {
TimeUpdateTopic record = new TimeUpdateTopic();
record.time = fieldSetFlags()[0] ? this.time : (java.lang.Double) defaultValue(fields()[0]);
return record;
} catch (org.apache.avro.AvroMissingFieldException e) {
throw e;
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
}
@SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumWriter<TimeUpdateTopic>
WRITER$ = (org.apache.avro.io.DatumWriter<TimeUpdateTopic>)MODEL$.createDatumWriter(SCHEMA$);
@Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
@SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumReader<TimeUpdateTopic>
READER$ = (org.apache.avro.io.DatumReader<TimeUpdateTopic>)MODEL$.createDatumReader(SCHEMA$);
@Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
@Override protected boolean hasCustomCoders() { return true; }
@Override public void customEncode(org.apache.avro.io.Encoder out)
throws java.io.IOException
{
out.writeDouble(this.time);
}
@Override public void customDecode(org.apache.avro.io.ResolvingDecoder in)
throws java.io.IOException
{
org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
if (fieldOrder == null) {
this.time = in.readDouble();
} else {
for (int i = 0; i < 1; i++) {
switch (fieldOrder[i].pos()) {
case 0:
this.time = in.readDouble();
break;
default:
throw new java.io.IOException("Corrupt ResolvingDecoder.");
}
}
}
}
}
我是不是在做一些愚蠢和/或错误的事情?或者这是一个实际的错误
我可以使用此函数获取 JSON:
inline fun <reified T: SpecificRecordBase> StringFromAvroGenerated(obj: T) : String {
val schema = obj.schema
val writer = SpecificDatumWriter(T::class.java)
val stream = ByteArrayOutputStream()
var jsonEncoder = EncoderFactory.get().jsonEncoder(schema, stream)
writer.write(obj, jsonEncoder)
jsonEncoder.flush()
return stream.toString("UTF-8")
}
但我假设这对 Jackson 来说应该是自动的
最佳答案
看来有两种方法可以解决我的问题(感谢 JsonMappingException when serializing avro generated object to json)
所以第一个选项要求我创建一个这样的 Mixin:
abstract class AvroMixIn {
@JsonIgnore
abstract fun getSchema(): org.apache.avro.Schema
@JsonIgnore
abstract fun getSpecificData() : org.apache.avro.specific.SpecificData
}
然后当我制作对象映射器时:
val objectMapper = ObjectMapper()
.registerModule(JavaTimeModule())
.addMixIn(Object::class.java, AvroMixIn::class.java)
我选择了 Object::class.java
而不是实际的类,因为它应该适用于所有类。可能更好的解决方案是将它应用于所有 AvroGenerated 的共享基类。
这实际上是我采用的第一种方法,因为它看起来更“无缝”。
1) 查看avro项目2) 复制enum.vm
, fixed.vm
, protocol.vm
, record.vm
到 >/avro_templates
我项目主根目录下的目录3) 将 @com.fasterxml.jackson.annotation.JsonIgnore
属性添加到模板:
#end
@com.fasterxml.jackson.annotation.JsonIgnore
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
@com.fasterxml.jackson.annotation.JsonIgnore
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
4) 更新gradle任务:
avro {
dateTimeLogicalType="JSR310"
templateDirectory = "avro_templates/"
}
5) 重新构建 avro 类
(现在一切正常)
关于java - Avro 使用 json 转换生成类问题 [kotlin],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56742226/
我在名为 commonSourceMetadata.avsc 的 json 文件中定义了一个名为 "some.package.SourceMetadata" 的 Avro 类型: { "type"
我很想了解在 Avro 中编码两种非常特定类型的数据的最佳实践:时间戳和 IP 地址。 我遇到了时间戳 ( https://issues.apache.org/jira/browse/AVRO-739
如何在 Avro Schema 生成中为数据类型设置最大大小/长度限制。例如:在模式中,我想指定一个字段,该字段采用最大 len 25 的字符串。 最佳答案 我相信您可以使用“固定”avro 类型并指
即是否可以使字段需要类似于 ProtoBuf: 消息搜索请求{ 需要 字符串查询 = 1; } 最佳答案 默认情况下,Avro 中的所有字段都是必需的。照原样 mentioned在官方文档中,如果你想
我有用户编写 AVRO 文件,我想使用 Flume 将所有这些文件移动到使用 Flume 的 HDFS 中。所以我以后可以使用 Hive 或 Pig 来查询/分析数据。 在客户端我安装了 flume
我正在为似乎具有多个对象数组的 JSON 有效负载创建 avro 模式。我不确定如何在模式中表示这一点。有问题的关键是 content: { "id": "channel-id", "name
似乎没有任何方法可以将数据附加到现有的 Avro 序列化文件中。我想让多个进程写入一个 avro 文件,但看起来每次打开它时,我都会从头开始。我不想读入所有数据,然后再将其写回。 使用 ruby
我试图定义一个不太平凡的 Avro 模式,但收效甚微;当它不会抛出架构语法错误时,它不会生成我试图在架构中定义的所有类型。 是否有 avsc 定义的可能内容的完整规范?我一直根据我从 Doc 规范中理
我正在尝试使用 avro-tools-1.7.4.jar create schema 命令创建两个 Avro 模式。 我有两个 JSON 模式,如下所示: { "name": "TestAvro",
首先,我创建了一个如下所示的 avro hive 表。 CREATE EXTERNAL TABLE user STORED AS AVRO LOCATION '/work/user' TBLPROPE
我正在读一本书 Hadoop application architectures,这本书很老但很有趣,在阅读时,我注意到 Avro 被认为是数据序列化框架,而 Parquet 被认为是列数据格式。 我
我一直在四处寻找,看到了 jira https://issues.apache.org/jira/browse/AVRO-739对于这个问题,但我对用户文档中的日期时间的 avro 支持没有更好的了解
我尝试在安装了 Spark 2.4.8 的 Cloud Dataproc 集群 1.4 上运行我的 Spark/Scala 代码 2.3.0。我在读取 avro 文件时遇到错误。这是我的代码: spa
我正在处理 JSON 格式的服务器日志,我想以 Parquet 格式将我的日志存储在 AWS S3 上(并且 Parquet 需要 Avro 模式)。首先,所有日志都有一组共同的字段,其次,所有日志都
这是来自教程点的解串器。 public class Deserialize { public static void main(String args[]) throws Exception{
我正在使用 avro-maven-plugin 1.8.1 从 schema 生成 java 代码,所有字段都是公共(public)的且已弃用,如下所示: public class data_el
一个简单的例子说明了我的问题。 本质上,我正在处理一个跨多个存储库拆分代码的大型项目。在 repo 1 中,在 .avdl 文件中定义了一个 Avro 模式“S1”,该文件被编译到其 Avro 生成的
通过套接字发送avro(avro c)编码数据我正在尝试将 avro 编码数据转换为字节数组(使用 memcpy)后通过套接字发送。我所做的如下所示 /客户端:client.c/ avro_datum
我的问题是这样的。我有一个 2GB 的压缩 avro 文件,HDFS 上存储了大约 1000 条 avro 记录。我知道我可以编写代码来“打开这个 avro 文件”并打印出每条 avro 记录。我的问
我看到以下错误 exception Unsupported Avro type. Supported types are null, Boolean, Integer, Long, Float, Do
我是一名优秀的程序员,十分优秀!