You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by gi...@apache.org on 2026/09/05 13:44:58 UTC
(seatunnel) branch gh-readonly-queue/dev/pr-12088-76c22220d7e6611495f174678bfbcc8191bb8727 created (now 0badccfbe2)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a change to branch gh-readonly-queue/dev/pr-12088-76c22220d7e6611495f174678bfbcc8191bb8727
in repository https://gitbox.apache.org/repos/asf/seatunnel.git
at 0badccfbe2 [Feature][Connector-Kafka] Support Confluent-framed Avro payloads (#12088)
This branch includes the following new commits:
new 0badccfbe2 [Feature][Connector-Kafka] Support Confluent-framed Avro payloads (#12088)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(seatunnel) 01/01: [Feature][Connector-Kafka] Support Confluent-framed Avro payloads (#12088)
Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-12088-76c22220d7e6611495f174678bfbcc8191bb8727
in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit 0badccfbe28033f1f770609f2e5389211b8a8917
Author: Yohanes <Y0...@outlook.com>
AuthorDate: Sat Sep 5 13:44:36 2026 +0000
[Feature][Connector-Kafka] Support Confluent-framed Avro payloads (#12088)
Co-authored-by: Yohanes <Cr...@users.noreply.github.com>
---
docs/en/connectors/formats/avro.md | 3 +-
docs/en/connectors/source/Kafka.md | 4 +-
docs/zh/connectors/formats/avro.md | 3 +-
docs/zh/connectors/source/Kafka.md | 4 +-
.../seatunnel/kafka/config/KafkaSourceOptions.java | 5 ++-
.../seatunnel/kafka/source/KafkaSourceConfig.java | 17 +++++---
.../format/avro/AvroDeserializationSchema.java | 35 +++++++++++++---
.../format/avro/AvroSerializationSchemaTest.java | 46 ++++++++++++++++++++++
8 files changed, 96 insertions(+), 21 deletions(-)
diff --git a/docs/en/connectors/formats/avro.md b/docs/en/connectors/formats/avro.md
index f8222a1e21..e80c45f7ad 100644
--- a/docs/en/connectors/formats/avro.md
+++ b/docs/en/connectors/formats/avro.md
@@ -4,7 +4,7 @@ Avro is very popular in streaming data pipeline. Now seatunnel supports Avro for
:::note Confluent Schema Registry is not supported
-This format decodes plain Avro binary directly using the `schema` you configure; it does not talk to a schema registry. Messages produced by a Confluent Schema Registry-aware serializer carry a 5-byte wire-format header (a magic byte followed by a 4-byte schema ID) before the Avro payload, and this format does not strip that header, so registry-produced messages cannot be read as-is. To consume Confluent-style Avro from Kafka, either write a custom deserialization schema that strips the [...]
+This format decodes plain Avro binary directly using the `schema` you configure; it does not talk to a schema registry. For Confluent Schema Registry messages consumed through Kafka, set `strip_schema_registry_header = true` and provide `avro_schema`. The connector detects the header by its leading magic byte and strips the five-byte wire header (magic byte `0` plus four-byte schema ID) without contacting Schema Registry. The option is opt-in and raw Avro behavior is unchanged when disabled.
:::
@@ -114,4 +114,3 @@ sink {
}
}
```
-
diff --git a/docs/en/connectors/source/Kafka.md b/docs/en/connectors/source/Kafka.md
index bfe4e8fd79..dd5b05e908 100644
--- a/docs/en/connectors/source/Kafka.md
+++ b/docs/en/connectors/source/Kafka.md
@@ -61,7 +61,7 @@ They can be downloaded via install-plugin.sh or from the Maven central repositor
| common-options | | No | - | Source plugin common parameters, please refer to [Source Common Options](../common-options/source-common-options.md) for details [...]
| protobuf_message_name | String | No | - | Effective when the format is set to protobuf, specifies the Message name [...]
| protobuf_schema | String | No | - | Effective when the format is set to protobuf, specifies the Schema definition [...]
-| strip_schema_registry_header | Boolean | No | false | Effective when the format is set to protobuf. Whether to strip the Confluent Schema Registry wire format header (magic byte, schema id and message indexes) before protobuf deserialization. This option is useful when consuming Protobuf messages that were encoded using Confluent Schema Registry. When enabled, the connector will try to de [...]
+| strip_schema_registry_header | Boolean | No | false | Effective when the format is set to protobuf or avro. For protobuf, strips the Confluent Schema Registry header before deserialization. For avro, strips the fixed five-byte header (magic byte and schema ID); `avro_schema` is required when enabled, and no Schema Registry lookup is performed. |
| reader_cache_queue_size | Integer | No | 2 | The capacity of the fetcher-to-reader element queue. Each element is one `consumer.poll()` batch, not a single message. See [reader_cache_queue_size](#reader_cache_queue_size) for details. |
| is_native | Boolean | No | false | Supports retaining the source information of the record. [...]
| kafka_headers_fields | Array | No | - | Specify which Kafka message header keys to extract as row fields. Each header value is read as a STRING type and appended to the output row after the regular schema fields. Cannot be used with NATIVE format. [...]
@@ -711,7 +711,7 @@ Note: the `key` field in NATIVE format is base64-encoded bytes.
Kafka Source supports: `json`, `text`, `canal_json`, `debezium_json`, `ogg_json`, `avro`, `protobuf`, and `NATIVE`. Use `NATIVE` when you need access to Kafka-level metadata (headers, key, partition, timestamp) as part of the record.
-`format = avro` expects raw Avro-encoded messages. Unlike `protobuf` (see [Protobuf with Schema Registry wire format](#protobuf-with-schema-registry-wire-format)), there is no `strip_schema_registry_header`-equivalent option for `avro`: if a topic was produced by a Confluent `KafkaAvroSerializer` and its messages carry the Confluent Schema Registry wire-format header (magic byte + schema id), `format = avro` does not strip that header before deserializing, so reading will fail or produce [...]
+`format = avro` expects raw Avro-encoded messages by default. For messages produced by a Confluent `KafkaAvroSerializer`, set `strip_schema_registry_header = true` and provide `avro_schema`. SeaTunnel detects the header by its leading magic byte and strips the fixed five-byte wire header (magic byte `0` plus four-byte schema ID) before decoding, without contacting Schema Registry. The option is opt-in, so raw Avro behavior is unchanged when it is `false`.
### How do I configure SASL/Kerberos authentication?
diff --git a/docs/zh/connectors/formats/avro.md b/docs/zh/connectors/formats/avro.md
index dc3cf512f5..42a35ecd7a 100644
--- a/docs/zh/connectors/formats/avro.md
+++ b/docs/zh/connectors/formats/avro.md
@@ -4,7 +4,7 @@ Avro 在流式数据处理管道中非常流行。现在seatunnel在kafka连接
:::note 不支持 Confluent Schema Registry
-该格式直接使用你配置的 `schema` 解码原始 Avro 二进制数据,不会与 schema registry 交互。经过 Confluent Schema Registry 序列化的消息,会在 Avro 数据前带有 5 字节的线上格式头(1 字节 magic byte + 4 字节 schema ID),该格式不会剥离这部分头信息,因此无法直接读取 registry 生成的消息。如果需要消费 Confluent 风格的 Avro 数据,可以自行实现一个在解码前剥离头信息的反序列化 schema,或者改用不添加 registry 头的生产者写入普通 Avro 数据。Protobuf 格式提供了类似的 [`strip_schema_registry_header`](../source/Kafka.md) 选项;Avro 目前还没有对应的选项。
+该格式直接使用你配置的 `schema` 解码原始 Avro 二进制数据,不会与 Schema Registry 交互。消费 Confluent Schema Registry 消息时,请设置 `strip_schema_registry_header = true` 并提供 `avro_schema`。连接器通过开头的 magic byte 检测并剥离 5 字节线上格式头(1 字节 magic byte `0` + 4 字节 schema ID),不会查询 Schema Registry。该选项默认关闭,关闭时原始 Avro 行为保持不变。
:::
@@ -114,4 +114,3 @@ sink {
}
}
```
-
diff --git a/docs/zh/connectors/source/Kafka.md b/docs/zh/connectors/source/Kafka.md
index bda1300186..4436ada0ea 100644
--- a/docs/zh/connectors/source/Kafka.md
+++ b/docs/zh/connectors/source/Kafka.md
@@ -61,7 +61,7 @@ import ChangeLog from '../changelog/connector-kafka.md';
| common-options | | 否 | - | 源插件的常见参数,详情请参考 [Source Common Options](../common-options/source-common-options.md)。 |
| protobuf_message_name | String | 否 | - | 当格式设置为 protobuf 时有效,指定消息名称。 |
| protobuf_schema | String | 否 | - | 当格式设置为 protobuf 时有效,指定 Schema 定义。 |
-| strip_schema_registry_header | Boolean | 否 | false | 当格式设置为 protobuf 时有效。是否在 Protobuf 反序列化之前去除 Confluent Schema Registry 线格式头部(magic byte、schema id 和 message indexes)。当消费使用 Confluent Schema Registry 编码的 Protobuf 消息时,此选项非常有用。启用后,连接器将尝试在解析 Protobuf 消息之前检测并删除 Schema Registry 头部。如果未检测到头部,它将回退到标准的 Protobuf 反序列化。 [...]
+| strip_schema_registry_header | Boolean | 否 | false | 当格式设置为 protobuf 或 avro 时有效。protobuf 会在反序列化前去除 Confluent Schema Registry 头;avro 会去除固定的 5 字节头(magic byte 和 schema ID)。avro 启用此选项时必须同时配置 `avro_schema`,且不会查询 Schema Registry。 |
| reader_cache_queue_size | Integer | 否 | 2 | Fetcher 与 Reader 线程之间缓冲队列的容量。每个元素是一次 `consumer.poll()` 的整批结果,而非单条消息。详见 [reader_cache_queue_size](#reader_cache_queue_size)。 |
| is_native | Boolean | 否 | false | 支持保留record的源信息。 |
| kafka_headers_fields | Array | 否 | - | 指定要从 Kafka 消息 header 中提取并映射为行字段的 header key 列表。每个 header 值以 STRING 类型追加到输出行的末尾(位于正常 schema 字段之后)。不支持 NATIVE 格式。 |
@@ -704,7 +704,7 @@ transform {
支持:`json`、`text`、`canal_json`、`debezium_json`、`ogg_json`、`avro`、`protobuf` 和 `NATIVE`。当需要将 Kafka 元数据(headers、key、partition、timestamp)作为记录字段使用时,选择 `NATIVE` 格式。
-`format = avro` 仅支持原始(未经 Schema Registry 封装)的 Avro 消息。与 `protobuf`(参见 [Protobuf with Schema Registry wire format](#protobuf-with-schema-registry-wire-format))不同,`avro` 没有对应 `strip_schema_registry_header` 的选项:如果 topic 是由 Confluent `KafkaAvroSerializer` 写入、消息中带有 Confluent Schema Registry 线格式头部(magic byte + schema id),`format = avro` 不会在反序列化前去除该头部,因此会读取失败或得到损坏的数据。`avro_schema` 仅用于为普通(非 Schema Registry)Avro 消息提供 writer schema。
+`format = avro` 默认读取原始 Avro 二进制消息。对于由 Confluent `KafkaAvroSerializer` 写入的消息,请设置 `strip_schema_registry_header = true` 并提供 `avro_schema`。连接器通过开头的 magic byte 检测并剥离固定的 5 字节线上格式头(magic byte `0` 加 4 字节 schema ID)后再解码,不会查询 Schema Registry。该选项默认关闭,关闭时原始 Avro 行为保持不变。
### 如何配置 SASL/Kerberos 认证?
diff --git a/seatunnel-connectors-v2/connector-kafka/src/main/java/org/apache/seatunnel/connectors/seatunnel/kafka/config/KafkaSourceOptions.java b/seatunnel-connectors-v2/connector-kafka/src/main/java/org/apache/seatunnel/connectors/seatunnel/kafka/config/KafkaSourceOptions.java
index 0766e71b42..50e6ef5ab5 100644
--- a/seatunnel-connectors-v2/connector-kafka/src/main/java/org/apache/seatunnel/connectors/seatunnel/kafka/config/KafkaSourceOptions.java
+++ b/seatunnel-connectors-v2/connector-kafka/src/main/java/org/apache/seatunnel/connectors/seatunnel/kafka/config/KafkaSourceOptions.java
@@ -136,8 +136,9 @@ public class KafkaSourceOptions extends KafkaBaseOptions {
.defaultValue(false)
.withDescription(
"Whether to strip the Confluent Schema Registry wire format header "
- + "(magic byte, schema id and message indexes) before "
- + "protobuf deserialization.");
+ + "before protobuf deserialization, or the five-byte "
+ + "magic-byte/schema-id header before Avro deserialization. "
+ + "For Avro, avro_schema is required when enabled.");
public static final Option<String> AVRO_SCHEMA =
Options.key("avro_schema")
diff --git a/seatunnel-connectors-v2/connector-kafka/src/main/java/org/apache/seatunnel/connectors/seatunnel/kafka/source/KafkaSourceConfig.java b/seatunnel-connectors-v2/connector-kafka/src/main/java/org/apache/seatunnel/connectors/seatunnel/kafka/source/KafkaSourceConfig.java
index 63270eb58a..3964f8a599 100644
--- a/seatunnel-connectors-v2/connector-kafka/src/main/java/org/apache/seatunnel/connectors/seatunnel/kafka/source/KafkaSourceConfig.java
+++ b/seatunnel-connectors-v2/connector-kafka/src/main/java/org/apache/seatunnel/connectors/seatunnel/kafka/source/KafkaSourceConfig.java
@@ -424,13 +424,18 @@ public class KafkaSourceConfig implements Serializable {
break;
case AVRO:
Optional<String> avroSchema = readonlyConfig.getOptional(AVRO_SCHEMA);
+ boolean stripAvroSchemaRegistryHeader =
+ readonlyConfig.get(STRIP_SCHEMA_REGISTRY_HEADER);
+ if (stripAvroSchemaRegistryHeader && !avroSchema.isPresent()) {
+ throw new SeaTunnelJsonFormatException(
+ CommonErrorCodeDeprecated.ILLEGAL_ARGUMENT,
+ "avro_schema must be configured when strip_schema_registry_header is enabled for avro format");
+ }
schema =
- avroSchema
- .map(
- writerSchema ->
- new AvroDeserializationSchema(
- catalogTable, writerSchema))
- .orElseGet(() -> new AvroDeserializationSchema(catalogTable));
+ new AvroDeserializationSchema(
+ catalogTable,
+ avroSchema.orElse(null),
+ stripAvroSchemaRegistryHeader);
break;
case PROTOBUF:
boolean stripSchemaRegistryHeader =
diff --git a/seatunnel-formats/seatunnel-format-avro/src/main/java/org/apache/seatunnel/format/avro/AvroDeserializationSchema.java b/seatunnel-formats/seatunnel-format-avro/src/main/java/org/apache/seatunnel/format/avro/AvroDeserializationSchema.java
index c928bbb6be..36e3c42992 100644
--- a/seatunnel-formats/seatunnel-format-avro/src/main/java/org/apache/seatunnel/format/avro/AvroDeserializationSchema.java
+++ b/seatunnel-formats/seatunnel-format-avro/src/main/java/org/apache/seatunnel/format/avro/AvroDeserializationSchema.java
@@ -38,22 +38,47 @@ public class AvroDeserializationSchema implements DeserializationSchema<SeaTunne
private final SeaTunnelRowType rowType;
private final AvroToRowConverter converter;
private final CatalogTable catalogTable;
+ private final boolean stripSchemaRegistryHeader;
public AvroDeserializationSchema(CatalogTable catalogTable) {
- this.catalogTable = catalogTable;
- this.rowType = catalogTable.getSeaTunnelRowType();
- this.converter = new AvroToRowConverter(rowType);
+ this(catalogTable, null, false);
}
public AvroDeserializationSchema(CatalogTable catalogTable, String writerSchema) {
+ this(catalogTable, writerSchema, false);
+ }
+
+ public AvroDeserializationSchema(
+ CatalogTable catalogTable, String writerSchema, boolean stripSchemaRegistryHeader) {
this.catalogTable = catalogTable;
this.rowType = catalogTable.getSeaTunnelRowType();
- this.converter = new AvroToRowConverter(rowType, writerSchema);
+ this.converter =
+ writerSchema == null
+ ? new AvroToRowConverter(rowType)
+ : new AvroToRowConverter(rowType, writerSchema);
+ this.stripSchemaRegistryHeader = stripSchemaRegistryHeader;
}
@Override
public SeaTunnelRow deserialize(byte[] message) throws IOException {
- BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(message, null);
+ if (stripSchemaRegistryHeader && hasSchemaRegistryHeader(message)) {
+ try {
+ return deserializePayload(message, 5, message.length - 5);
+ } catch (IOException | RuntimeException framedFailure) {
+ // A raw Avro payload can legally begin with zero. Preserve compatibility by
+ // retrying the complete message when the framed interpretation is not decodable.
+ }
+ }
+ return deserializePayload(message, 0, message.length);
+ }
+
+ private static boolean hasSchemaRegistryHeader(byte[] message) {
+ return message != null && message.length >= 5 && message[0] == 0;
+ }
+
+ private SeaTunnelRow deserializePayload(byte[] message, int offset, int length)
+ throws IOException {
+ BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(message, offset, length, null);
GenericRecord record = this.converter.getReader().read(null, decoder);
SeaTunnelRow seaTunnelRow = converter.converter(record, rowType);
Optional<TablePath> tablePath =
diff --git a/seatunnel-formats/seatunnel-format-avro/src/test/java/org/apache/seatunnel/format/avro/AvroSerializationSchemaTest.java b/seatunnel-formats/seatunnel-format-avro/src/test/java/org/apache/seatunnel/format/avro/AvroSerializationSchemaTest.java
index 6e01aa1a59..ec4635ef1f 100644
--- a/seatunnel-formats/seatunnel-format-avro/src/test/java/org/apache/seatunnel/format/avro/AvroSerializationSchemaTest.java
+++ b/seatunnel-formats/seatunnel-format-avro/src/test/java/org/apache/seatunnel/format/avro/AvroSerializationSchemaTest.java
@@ -323,4 +323,50 @@ class AvroSerializationSchemaTest {
Assertions.assertEquals(42, ((SeaTunnelRow) result.getField(0)).getField(0));
}
+
+ @Test
+ public void testDeserializeConfluentSchemaRegistryHeader() throws IOException {
+ SeaTunnelRowType rowType =
+ new SeaTunnelRowType(
+ new String[] {"payload"},
+ new SeaTunnelDataType<?>[] {BasicType.STRING_TYPE});
+ CatalogTable catalogTable = CatalogTableUtil.getCatalogTable("", "", "", "test", rowType);
+ String writerSchemaText =
+ "{\"type\":\"record\",\"name\":\"Event\",\"fields\":[{\"name\":\"payload\",\"type\":\"string\"}]}";
+ Schema writerSchema = new Schema.Parser().parse(writerSchemaText);
+ GenericRecord record =
+ new GenericRecordBuilder(writerSchema).set("payload", "seatunnel").build();
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(out, null);
+ new GenericDatumWriter<GenericRecord>(writerSchema).write(record, encoder);
+ encoder.flush();
+ byte[] framed = new byte[out.size() + 5];
+ framed[0] = 0;
+ framed[1] = 0;
+ framed[2] = 0;
+ framed[3] = 0;
+ framed[4] = 1;
+ System.arraycopy(out.toByteArray(), 0, framed, 5, out.size());
+
+ AvroDeserializationSchema schema =
+ new AvroDeserializationSchema(catalogTable, writerSchemaText, true);
+ Assertions.assertEquals("seatunnel", schema.deserialize(framed).getField(0));
+ }
+
+ @Test
+ public void testDeserializeConfluentSchemaRegistryHeaderRejectsInvalidHeader() {
+ SeaTunnelRowType rowType =
+ new SeaTunnelRowType(
+ new String[] {"payload"},
+ new SeaTunnelDataType<?>[] {BasicType.STRING_TYPE});
+ CatalogTable catalogTable = CatalogTableUtil.getCatalogTable("", "", "", "test", rowType);
+ AvroDeserializationSchema schema =
+ new AvroDeserializationSchema(
+ catalogTable,
+ "{\"type\":\"record\",\"name\":\"Event\",\"fields\":[{\"name\":\"payload\",\"type\":\"string\"}]}",
+ true);
+ Assertions.assertThrows(
+ RuntimeException.class, () -> schema.deserialize(new byte[] {1, 0, 0, 0, 1}));
+ }
}