【发布时间】:2021-10-29 12:39:14
【问题描述】:
我正在通过 Spark 对 Kafka 进行一些批处理。与 Avro 一样序列化的记录。我正在尝试使用消息本身中的确切模式来反序列化该值,但我得到了一个格式错误的记录异常。这是我的代码:
Dataset<Row> load = sparkSession
.read()
.format("kafka")
.option("kafka.bootstrap.servers", (String) kafkaConfiguration.consumerProperties().get("bootstrap.servers"))
.option("subscribe", kafkaConfiguration.topicsAsCSV(","))
.load();
var schema = new String(Files.readAllBytes(Paths.get("schema.avsc")));
load.select(from_avro(col("value"), schema)).write().json("/tmp/spark/json");
请注意,架构是从记录的值本身复制而来的。
但是,我得到以下异常:
Caused by: org.apache.avro.AvroRuntimeException: Malformed data. Length is negative: -20
at org.apache.avro.io.BinaryDecoder.doReadBytes(BinaryDecoder.java:336)
at org.apache.avro.io.BinaryDecoder.readString(BinaryDecoder.java:263)
at org.apache.avro.io.BinaryDecoder.readString(BinaryDecoder.java:272)
at org.apache.avro.io.ResolvingDecoder.readString(ResolvingDecoder.java:214)
此错误背后的原因是什么,我该如何解决?谢谢!
【问题讨论】:
标签: apache-spark avro confluent-schema-registry spark-avro