-
Notifications
You must be signed in to change notification settings - Fork 998
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kafka debezium json supports automatic discovery of primary keys
- Loading branch information
1 parent
15f8c9a
commit 1d483f3
Showing
14 changed files
with
404 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...ain/java/org/apache/paimon/flink/action/cdc/kafka/KafkaKeyValueDeserializationSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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.kafka; | ||
|
||
import org.apache.paimon.flink.action.cdc.format.DataFormat; | ||
import org.apache.paimon.flink.action.cdc.format.debezium.DebeziumSchemaUtils; | ||
import org.apache.paimon.utils.Pair; | ||
|
||
import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
import org.apache.flink.connector.kafka.source.reader.deserializer.KafkaRecordDeserializationSchema; | ||
import org.apache.flink.util.Collector; | ||
import org.apache.flink.util.Preconditions; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
|
||
/** Deserialization for kafka key and value. */ | ||
public class KafkaKeyValueDeserializationSchema | ||
implements KafkaRecordDeserializationSchema<String> { | ||
|
||
private String charset; | ||
private String format; | ||
|
||
public KafkaKeyValueDeserializationSchema(String format) { | ||
this(StandardCharsets.UTF_8.name(), format); | ||
} | ||
|
||
public KafkaKeyValueDeserializationSchema(String charset, String format) { | ||
this.charset = Preconditions.checkNotNull(charset); | ||
this.format = format; | ||
} | ||
|
||
@Override | ||
public void deserialize(ConsumerRecord<byte[], byte[]> record, Collector<String> collector) | ||
throws IOException { | ||
DataFormat dataFormat = DataFormat.fromConfigString(format); | ||
String value = new String(record.value(), Charset.forName(charset)); | ||
switch (dataFormat) { | ||
case DEBEZIUM_JSON: | ||
if (Objects.isNull(record.key())) { | ||
collector.collect(value); | ||
} else { | ||
String key = new String(record.key(), Charset.forName(charset)); | ||
Pair<String, String> keyValue = Pair.of(key, value); | ||
String result = DebeziumSchemaUtils.extractPrimaryKeys(keyValue); | ||
collector.collect(result); | ||
} | ||
break; | ||
default: | ||
collector.collect(value); | ||
break; | ||
} | ||
} | ||
|
||
@Override | ||
public TypeInformation<String> getProducedType() { | ||
return TypeInformation.of(String.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.