yuxiqian commented on code in PR #3805: URL: https://github.com/apache/flink-cdc/pull/3805#discussion_r1895492708
########## flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-kafka/src/test/java/org/apache/flink/cdc/connectors/kafka/sink/KafkaDataSinkITCase.java: ########## @@ -560,6 +559,67 @@ void testTopicAndHeaderOption() throws Exception { checkProducerLeak(); } + @Test + void testSinkTableMapping() throws Exception { + final StreamExecutionEnvironment env = new LocalStreamEnvironment(); + env.enableCheckpointing(1000L); + env.setRestartStrategy(RestartStrategies.noRestart()); + final DataStream<Event> source = env.fromData(createSourceEvents(), new EventTypeInfo()); + Map<String, String> config = new HashMap<>(); + config.put( + KafkaDataSinkOptions.SINK_TABLE_ID_TO_TOPIC_MAPPING.key(), + "default_namespace.default_schema_copy.\\.*:test_topic_mapping_copy;default_namespace.default_schema.\\.*:test_topic_mapping"); + Properties properties = getKafkaClientConfiguration(); + properties.forEach( + (key, value) -> + config.put( + KafkaDataSinkOptions.PROPERTIES_PREFIX + key.toString(), + value.toString())); + source.sinkTo( + ((FlinkSinkProvider) + (new KafkaDataSinkFactory() + .createDataSink( + new FactoryHelper.DefaultContext( + Configuration.fromMap(config), + Configuration.fromMap(new HashMap<>()), + this.getClass().getClassLoader())) + .getEventSinkProvider())) + .getSink()); + env.execute(); + + final List<ConsumerRecord<byte[], byte[]>> collectedRecords = + drainAllRecordsFromTopic("test_topic_mapping", false, 0); + final long recordsCount = 5; + assertThat(recordsCount).isEqualTo(collectedRecords.size()); Review Comment: ```suggestion assertThat(collectedRecords).hasSize(5); ``` ########## flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-kafka/src/main/java/org/apache/flink/cdc/connectors/kafka/utils/KafkaSinkUtils.java: ########## @@ -0,0 +1,52 @@ +/* + * 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.flink.cdc.connectors.kafka.utils; + +import org.apache.flink.cdc.common.schema.Selectors; +import org.apache.flink.cdc.common.utils.Preconditions; +import org.apache.flink.cdc.connectors.kafka.sink.KafkaDataSinkOptions; + +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.apache.flink.cdc.connectors.kafka.sink.KafkaDataSinkOptions.DELIMITER_SELECTOR_TOPIC; +import static org.apache.flink.cdc.connectors.kafka.sink.KafkaDataSinkOptions.DELIMITER_TABLE_MAPPINGS; + +/** Util class for {@link org.apache.flink.cdc.connectors.kafka.sink.KafkaDataSink}. */ +public class KafkaSinkUtils { + + /** Parse the mapping text to a map from Selectors to Kafka Topic name. */ + public static Map<Selectors, String> parseSelectorsToTopicMap(String mappingRuleString) { + // Keep the order. + Map<Selectors, String> result = new LinkedHashMap<>(); + if (mappingRuleString == null || mappingRuleString.isEmpty()) { + return result; + } + for (String mapping : mappingRuleString.split(DELIMITER_TABLE_MAPPINGS)) { + String[] selectorsAndTopic = mapping.split(DELIMITER_SELECTOR_TOPIC); + Preconditions.checkArgument( + selectorsAndTopic.length == 2, + "Please check you configuration of " Review Comment: ```suggestion "Please check your configuration of " ``` I think printing out the questionable `mapping` string here will be more helpful if users write a malformed expression. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org