我在编组/解组avro生成的类时遇到了一个奇怪的问题。我得到的错误是抛出一个not enum错误-除了我的类中没有任何枚举。
错误具体如下:
com.fasterxml.jackson.databind.JsonMappingException: Not an enum:{"type":"记录","name":"TimeUpdateTopic","namesspace":"org.company.mmd.time","field":[{"name":"时间","type":"双精度"}]}(通过引用链:org.company.mmd.time.TimeUpdateTopic["模式"]-
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.");
}
}
}
}
}
我是在做一些愚蠢和/或错误的事情吗?或者这是一个真正的bug
我可以使用此函数获取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在将avro生成的对象序列化为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)
我选择对象::class.java
而不是实际的类,因为它应该适用于所有类。可能更好的解决方案是将其应用于所有AvroGenerated东西都有的共享基类。
这实际上是我采取的第一种方法,因为它看起来更“不合适”。
1) 查看avro项目2)Copyenum。vm已修复。vm,协议。vm,记录。vm
进入项目主根目录下的avro\u templates目录3)添加com。fasterxml。杰克逊。注释。模板的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类
(现在一切正常)
有一个网站这样做,但我想要一个图书馆或CLI。 谢了!
上面的代码是,我从容器里面实例化的一个类,AdminService,invoke返回的是一个mixed任意类型,这样我就出现了一个问题了。 如果我不写@var 标注,我的编辑器就无法识别这个$adminService 实例下的方法了。 请问如何将$adminService 转换成AdminServcie 类型!
我正在尝试将Hudi与Kafka主题集成。 接下来的步骤是: 在ConFluent中创建了Kafka主题,并在模式注册表中定义了模式。 使用kafka-avro控制台生产者,我正在尝试生成数据。 以连续模式运行Hudi Delta Streamer以消耗数据。 基础设施: AWS EMR 火花2.4.4 Hudi实用程序(用0.6.0和0.7.0尝试) Avro(已尝试avro-1.8.2、avr
我在将简单的JSON消息转换为映射时遇到问题 Example_1.json: Spring配置: 错误: org.springframework.integration.handler.LoggingHandler]-org.springframework.messaging.MessageHandling异常:未能将消息有效载荷写入文件;嵌套异常是java.lang.IllegalArgumen
我正在使用confluent JDBC连接器连接到postgres数据库,以检索更改并将其放在Kafka主题中。现在,我想使用spring boot消费者来使用这些消息。这些消息采用AVRO格式。我从连接器中获得了模式,并使用avro-maven插件为其生成了一个POJO类。 但是当侦听器启动时,只有以下错误 当我不使用avro对数据进行反序列化时,我会收到数据但不可读。 在pom中。xml我有以
我正在尝试使用avro-tools将avro avdl文件(http://avro.apache.org/docs/1.7.6/idl.html#example)转换为avro模式文件(example.avsc)。我下载了avro-tools 1.7.6和1.6.3 example.avdl 生成的example.avsc 我在mac上使用以下命令生成它 java-jar avro-tools-1