简体   繁体   English

Avro Schema与Kafka,ClassCastException?

[英]Avro Schema with Kafka, ClassCastException?

I've created an Avro schema for records which we are publishing to our Kafka topic. 我已经为我们发布到Kafka主题的记录创建了一个Avro架构。 Our actual Kafka record schema is more complex but I've just attached the relevant parts for brevity. 我们实际的Kafka记录模式更复杂,但为了简洁起见,我附上了相关部分。 We have more than one nested sub class in the record, but for some reason I am getting the following exception when trying to publish the record (Package names have been obscured) : 我们在记录中有多个嵌套子类,但由于某种原因,我在尝试发布记录时遇到以下异常(包名称已被遮盖):

java.lang.ClassCastException: aaa.bbb.ccc.ddd.Amount cannot be cast to org.apache.avro.generic.IndexedRecord

class KafkaRecord {

    private Amount amount;

    class Amount {

        String currency;
        long value;

    }

}

And this is the current subset from the Avro schema I have defined. 这是我定义的Avro架构的当前子集。


{
  "type" : "record",
  "name" : "KafkaRecord",
  "namespace" : "com.company.department",
  "fields" : [ {
    "name" : "amount",
    "type" : {
      "type" : "record",
      "name" : "Amount",
      "namespace" : "aaa.bbb.ccc.ddd",
      "fields" : [ {
        "name" : "value",
        "type" : "long"
      }, {
        "name" : "currency",
        "type" : "string"
      } ]
    }
  }
}

Our JSON representation of the object (KafkaRecord) looks like so: 我们对象的JSON表示(KafkaRecord)看起来像这样:

{
  "amount": {
    "currency": "GBP",
    "value": 12345
  }
}

I can't seem to figure out why Avro doesn't like this nested record, and I'd prefer not to strip apart these nested classes as it would make the JSON record very difficult to read and hard to manage the classes. 我似乎无法弄清楚为什么Avro不喜欢这个嵌套记录,我宁愿不拆开这些嵌套类,因为它会使JSON记录很难阅读并且难以管理类。

If anyone is able to point out what I am doing wrong here that would be great! 如果有人能够指出我在这里做错了什么就会很棒!

Well, you don't need to write your own Java files. 好吧,您不需要编写自己的Java文件。 Sounds like you did, and so the error is as it says - Amount class is not an IndexedRecord 听起来像你做的那样,错误就像它说的那样 - Amount类不是IndexedRecord

For example, if I take your schema and run 例如,如果我采用您的架构并运行

java -jar ~/Downloads/avro-tools-1.8.2.jar compile schema KafkaRecord.avsc .

Then look at the file, we see that it extends some Avro java classes. 然后看一下这个文件,我们看到它扩展了一些Avro java类。

$ head -n 15 aaa/bbb/ccc/ddd/Amount.java
/**
 * Autogenerated by Avro
 *
 * DO NOT EDIT DIRECTLY
 */
package aaa.bbb.ccc.ddd;

import org.apache.avro.specific.SpecificData;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;

@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public class Amount extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {

The way to do this programmatically is documented by the Avro Maven Plugin guide. Avro Maven插件指南记录了以编程方式执行此操作的方法。

And yes, Avro works fine with nested records, and personally I like using IDL to more easily create them 是的,Avro适用于嵌套记录,我个人喜欢使用IDL来更轻松地创建它们

@namespace("com.company.department")
protocol KafkaEventProtocol {
  @namespace("aaa.bbb.ccc.ddd")
  record Amount {
    string currency;
    long value;
  }

  record KafkaValue {
    Amount amount;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Avro 向后模式演化抛出 ClassCastException - Avro backward schema evolution throws ClassCastException Kafka Streams创建没有架构的Avro主题 - Kafka Streams creates avro topic without schema 错误序列化 Avro 消息 - Kafka Schema Registry - Error serializing Avro message - Kafka Schema Registry 从kafka流和Avro反序列化同一类的ClassCastException - ClassCastException on the same class deserializing from kafka stream and Avro 带有嵌套映射的动态模式在 avro 中带有数组 - 获取 ClassCastException - dynamic schema with nested map with array in avro - getting ClassCastException 带有 Avro 记录的 Kafka Streams TopologyTestDriver 的架构注册问题 - Schema Registery Issue with Kafka Streams TopologyTestDriver with Avro record 如何为我的 kafka 消费者使用现有的 avro 模式? - How to utilize existing avro schema for my kafka consumer? 使用 Apache Avro 但具有不同架构的 kafka 发送/接收消息 - Send / Receive message through kafka using Apache Avro but with different schema 在 Spring 启动应用程序中使用来自 Confluent 的模式注册表与 Avro 和 Kafka - Using Schema Registry from Confluent with Avro and Kafka in Spring Boot Applications 如何使用 Avro 序列化程序和架构注册表向 Kafka 发送消息 - How to send message to Kafka with Avro serializer and schema registry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM