This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new c712515aa4 [flink][cdc] JSON text is corrupted on non-UTF-8 JVMs
(#11046)
c712515aa4 is described below
commit c712515aa4d91470c83fe8d04014399021617246
Author: Ran Tao <[email protected]>
AuthorDate: Tue Nov 10 17:12:53 2026 +0800
[flink][cdc] JSON text is corrupted on non-UTF-8 JVMs (#11046)
---
.../KafkaDebeziumJsonDeserializationSchema.java | 3 +-
.../CdcDebeziumDeserializationSchema.java | 5 +-
.../CdcJsonDeserializationSchema.java | 3 +-
.../CdcDebeziumDeserializationSchemaTest.java | 134 +++++++++++++++++++++
4 files changed, 142 insertions(+), 3 deletions(-)
diff --git
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/kafka/KafkaDebeziumJsonDeserializationSchema.java
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/kafka/KafkaDebeziumJsonDeserializationSchema.java
index c708dbca2d..e1a4ff8c8c 100644
---
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/kafka/KafkaDebeziumJsonDeserializationSchema.java
+++
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/kafka/KafkaDebeziumJsonDeserializationSchema.java
@@ -33,6 +33,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.Map;
import static org.apache.flink.api.java.typeutils.TypeExtractor.getForClass;
@@ -83,7 +84,7 @@ public class KafkaDebeziumJsonDeserializationSchema
Map<String, Object> kafkaMetadata =
KafkaActionUtils.extractKafkaMetadata(message);
out.collect(new CdcSourceRecord(message.topic(), keyNode,
valueNode, kafkaMetadata));
} catch (Exception e) {
- LOG.error("Invalid Json:\n{}", new String(message.value()));
+ LOG.error("Invalid Json:\n{}", new String(message.value(),
StandardCharsets.UTF_8));
throw e;
}
}
diff --git
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/serialization/CdcDebeziumDeserializationSchema.java
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/serialization/CdcDebeziumDeserializationSchema.java
index e202e4eeca..6aa94cf474 100644
---
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/serialization/CdcDebeziumDeserializationSchema.java
+++
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/serialization/CdcDebeziumDeserializationSchema.java
@@ -29,6 +29,7 @@ import org.apache.kafka.connect.source.SourceRecord;
import org.apache.kafka.connect.storage.ConverterConfig;
import org.apache.kafka.connect.storage.ConverterType;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@@ -75,7 +76,9 @@ public class CdcDebeziumDeserializationSchema
}
byte[] bytes =
jsonConverter.fromConnectData(record.topic(),
record.valueSchema(), record.value());
- out.collect(new CdcSourceRecord(record.topic(), null, new
String(bytes)));
+ out.collect(
+ new CdcSourceRecord(
+ record.topic(), null, new String(bytes,
StandardCharsets.UTF_8)));
}
/** Initialize {@link JsonConverter} with given configs. */
diff --git
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/serialization/CdcJsonDeserializationSchema.java
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/serialization/CdcJsonDeserializationSchema.java
index be39ef695d..53b3268043 100644
---
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/serialization/CdcJsonDeserializationSchema.java
+++
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/serialization/CdcJsonDeserializationSchema.java
@@ -31,6 +31,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import static org.apache.flink.api.java.typeutils.TypeExtractor.getForClass;
@@ -62,7 +63,7 @@ public class CdcJsonDeserializationSchema implements
DeserializationSchema<CdcSo
try {
return new CdcSourceRecord(objectMapper.readValue(message,
JsonNode.class));
} catch (Exception e) {
- LOG.error("Invalid Json:\n{}", new String(message));
+ LOG.error("Invalid Json:\n{}", new String(message,
StandardCharsets.UTF_8));
throw e;
}
}
diff --git
a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/serialization/CdcDebeziumDeserializationSchemaTest.java
b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/serialization/CdcDebeziumDeserializationSchemaTest.java
new file mode 100644
index 0000000000..2c8ef7facb
--- /dev/null
+++
b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/serialization/CdcDebeziumDeserializationSchemaTest.java
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.flink.action.cdc.serialization;
+
+import org.apache.paimon.flink.action.cdc.CdcSourceRecord;
+
+import
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.apache.flink.api.common.functions.util.ListCollector;
+import org.apache.kafka.connect.data.Schema;
+import org.apache.kafka.connect.data.SchemaBuilder;
+import org.apache.kafka.connect.data.Struct;
+import org.apache.kafka.connect.source.SourceRecord;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringJoiner;
+import java.util.concurrent.TimeUnit;
+import java.util.jar.Attributes;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Tests for {@link CdcDebeziumDeserializationSchema}. */
+class CdcDebeziumDeserializationSchemaTest {
+
+ @TempDir private Path tempDir;
+
+ @ParameterizedTest
+ @ValueSource(strings = {"UTF-8", "US-ASCII"})
+ void testPreserveJsonText(String charset) throws Exception {
+ Path output = tempDir.resolve("output.log");
+ // The default charset is cached, so configure it when starting a
fresh JVM.
+ Process process =
+ new ProcessBuilder(
+ new File(System.getProperty("java.home"),
"bin/java")
+ .getAbsolutePath(),
+ "-Dfile.encoding=" + charset,
+ "-cp",
+ createClasspathJar().toString(),
+ CharsetCheck.class.getName(),
+ charset)
+ .redirectErrorStream(true)
+ .redirectOutput(output.toFile())
+ .start();
+ try {
+ assertThat(process.waitFor(60, TimeUnit.SECONDS))
+ .as("Charset check process completed")
+ .isTrue();
+ assertThat(process.exitValue())
+ .withFailMessage(
+ "Charset check (%s) failed:%n%s",
+ charset, new String(Files.readAllBytes(output),
StandardCharsets.UTF_8))
+ .isZero();
+ } finally {
+ process.destroyForcibly();
+ }
+ }
+
+ private Path createClasspathJar() throws IOException {
+ String classpath =
+ System.getProperty(
+ "surefire.test.class.path",
System.getProperty("java.class.path"));
+ StringJoiner entries = new StringJoiner(" ");
+ for (String entry : classpath.split(File.pathSeparator)) {
+ entries.add(new File(entry).toURI().toASCIIString());
+ }
+
+ Manifest manifest = new Manifest();
+ manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
"1.0");
+ manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH,
entries.toString());
+ Path classpathJar = tempDir.resolve("classpath.jar");
+ // Keep the child JVM command line below the Windows length limit.
+ try (JarOutputStream ignored =
+ new JarOutputStream(Files.newOutputStream(classpathJar),
manifest)) {
+ // The manifest references the original classpath; no class
entries are needed.
+ }
+ return classpathJar;
+ }
+
+ /** Entry point for the JVM with an explicitly configured default charset.
*/
+ public static class CharsetCheck {
+
+ public static void main(String[] args) throws Exception {
+ // Preserve assertion diagnostics without changing the default
charset under test.
+ System.setErr(new PrintStream(System.err, true,
StandardCharsets.UTF_8.name()));
+
assertThat(Charset.defaultCharset()).isEqualTo(Charset.forName(args[0]));
+ String text = "ASCII δΈζπ\"\\\n";
+ Schema schema = SchemaBuilder.struct().field("text",
Schema.STRING_SCHEMA).build();
+ SourceRecord input =
+ new SourceRecord(
+ null, null, "topic", schema, new
Struct(schema).put("text", text));
+ for (boolean includeSchema : new boolean[] {false, true}) {
+ List<CdcSourceRecord> results = new ArrayList<>();
+ new CdcDebeziumDeserializationSchema(includeSchema)
+ .deserialize(input, new ListCollector<>(results));
+ assertThat(results).hasSize(1);
+ JsonNode actual = new ObjectMapper().readTree((String)
results.get(0).getValue());
+ if (includeSchema) {
+ actual = actual.get("payload");
+ }
+ assertThat(actual.get("text").asText()).isEqualTo(text);
+ }
+ }
+ }
+}