[英]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.