【发布时间】:2021-11-14 06:33:41
【问题描述】:
我有两个 Avro 模式 V1 和 V2,它们在 spark 中读取如下:
import org.apache.spark.sql.avro.functions._
val jsonFormatSchema = new String(Files.readAllBytes(Paths.get("./examples/src/main/resources/V1.avsc")))
val df = spark
.readStream
.format("kafka")
.option("kafka.bootstrap.servers", "host1:port1,host2:port2")
.option("subscribe", "topic1")
.load()
val output = df
.select(from_avro($"value", jsonFormatSchema) as $"avroFields")
V1 有两个字段“一”和“二”
{
"name": "test",
"namespace": "foo.bar",
"type": "record",
"fields": [
{
"name": "one",
"type": [
"null",
"string"
],
"default": null
},
{
"name": "two",
"type": [
"null",
"string"
],
"default": null
}
]
}
带有新字段的 V2:“三”
{
"name": "test",
"namespace": "foo.bar",
"type": "record",
"fields": [
{
"name": "one",
"type": [
"null",
"string"
],
"default": null
},
{
"name": "two",
"type": [
"null",
"string"
],
"default": null
},
{
"name": "three",
"type": [
"null",
"string"
],
"default": null
}
]
}
场景:writer 使用 V1 写入,Reader 使用 V2 解码 avro 记录。我的期望是看到字段 3 填充了默认值为 null。但我在 spark 工作中遇到了以下异常。
我在这里遗漏了什么吗?我的理解是avro支持向后兼容。
Exception in thread "main" java.io.EOFException
at org.apache.avro.io.BinaryDecoder.ensureBounds(BinaryDecoder.java:473)
at org.apache.avro.io.BinaryDecoder.readInt(BinaryDecoder.java:128)
at org.apache.avro.io.BinaryDecoder.readIndex(BinaryDecoder.java:423)
at org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:290)
at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
at org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:267)
at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:179)
at org.apache.avro.specific.SpecificDatumReader.readField(SpecificDatumReader.java:116)
at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:222)
at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:145)
【问题讨论】:
-
您如何阅读您的 avro 文件?您可以在您的问题中添加
Reader代码吗?谢谢!
标签: scala apache-spark avro spark-avro