wangxianghu commented on a change in pull request #1886: URL: https://github.com/apache/hudi/pull/1886#discussion_r471314453
########## File path: hudi-utilities/src/main/java/org/apache/hudi/utilities/callback/kafka/HoodieWriteCommitKafkaCallback.java ########## @@ -0,0 +1,145 @@ +/* + * 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.hudi.utilities.callback.kafka; + +import org.apache.hudi.callback.HoodieWriteCommitCallback; +import org.apache.hudi.callback.common.HoodieWriteCommitCallbackMessage; +import org.apache.hudi.callback.util.HoodieWriteCommitCallbackUtil; +import org.apache.hudi.common.util.StringUtils; +import org.apache.hudi.common.util.ValidationUtils; +import org.apache.hudi.config.HoodieWriteConfig; + +import org.apache.kafka.clients.producer.Callback; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.clients.producer.RecordMetadata; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; + +import java.util.Properties; + +import static org.apache.hudi.config.HoodieWriteConfig.TABLE_NAME; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_ACKS; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_BOOTSTRAP_SERVERS; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_PARTITION; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_RETRIES; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_TOPIC; + +/** + * Kafka implementation of {@link HoodieWriteCommitCallback}. + */ +public class HoodieWriteCommitKafkaCallback implements HoodieWriteCommitCallback { + + private static final Logger LOG = LogManager.getLogger(HoodieWriteCommitKafkaCallback.class); + + private Properties props; + private String bootstrapServers; + private String topic; + + public HoodieWriteCommitKafkaCallback(HoodieWriteConfig config) { + this.props = config.getProps(); + this.bootstrapServers = props.getProperty(CALLBACK_KAFKA_BOOTSTRAP_SERVERS); + this.topic = props.getProperty(CALLBACK_KAFKA_TOPIC); + validateKafkaConfig(); + } + + @Override + public void call(HoodieWriteCommitCallbackMessage callbackMessage) { + String callbackMsg = HoodieWriteCommitCallbackUtil.convertToJsonString(callbackMessage); + try (KafkaProducer<String, String> producer = createProducer(props)) { + ProducerRecord<String, String> record = buildProducerRecord(props, callbackMsg); + producer.send(record); + LOG.info(String.format("Send callback message %s succeed", callbackMsg)); + } catch (Exception e) { + LOG.error("Send kafka callback msg failed : " + e); + } + } + + /** + * Method helps to create {@link KafkaProducer}. Here we set acks = all and retries = 3 by default to ensure no data + * loss. + * + * @param props Kafka configs + * @return A {@link KafkaProducer} + */ + public KafkaProducer<String, String> createProducer(Properties props) { + Properties kafkaProducerProps = new Properties(); + // bootstrap.servers + kafkaProducerProps.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); + // default "all" to ensure no message loss + kafkaProducerProps.setProperty(ProducerConfig.ACKS_CONFIG, props.getProperty(CALLBACK_KAFKA_ACKS)); + // retries 3 times by default + kafkaProducerProps.setProperty(ProducerConfig.RETRIES_CONFIG, props.getProperty(CALLBACK_KAFKA_RETRIES)); + kafkaProducerProps.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, + "org.apache.kafka.common.serialization.StringSerializer"); + kafkaProducerProps.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, + "org.apache.kafka.common.serialization.StringSerializer"); + + LOG.info("Callback kafka producer init with configs: " Review comment: done ########## File path: hudi-utilities/src/main/java/org/apache/hudi/utilities/callback/kafka/HoodieWriteCommitKafkaCallback.java ########## @@ -0,0 +1,145 @@ +/* + * 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.hudi.utilities.callback.kafka; + +import org.apache.hudi.callback.HoodieWriteCommitCallback; +import org.apache.hudi.callback.common.HoodieWriteCommitCallbackMessage; +import org.apache.hudi.callback.util.HoodieWriteCommitCallbackUtil; +import org.apache.hudi.common.util.StringUtils; +import org.apache.hudi.common.util.ValidationUtils; +import org.apache.hudi.config.HoodieWriteConfig; + +import org.apache.kafka.clients.producer.Callback; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.clients.producer.RecordMetadata; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; + +import java.util.Properties; + +import static org.apache.hudi.config.HoodieWriteConfig.TABLE_NAME; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_ACKS; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_BOOTSTRAP_SERVERS; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_PARTITION; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_RETRIES; +import static org.apache.hudi.utilities.callback.kafka.HoodieWriteCommitKafkaCallbackConfig.CALLBACK_KAFKA_TOPIC; + +/** + * Kafka implementation of {@link HoodieWriteCommitCallback}. + */ +public class HoodieWriteCommitKafkaCallback implements HoodieWriteCommitCallback { + + private static final Logger LOG = LogManager.getLogger(HoodieWriteCommitKafkaCallback.class); + + private Properties props; + private String bootstrapServers; + private String topic; + + public HoodieWriteCommitKafkaCallback(HoodieWriteConfig config) { + this.props = config.getProps(); + this.bootstrapServers = props.getProperty(CALLBACK_KAFKA_BOOTSTRAP_SERVERS); + this.topic = props.getProperty(CALLBACK_KAFKA_TOPIC); + validateKafkaConfig(); + } + + @Override + public void call(HoodieWriteCommitCallbackMessage callbackMessage) { + String callbackMsg = HoodieWriteCommitCallbackUtil.convertToJsonString(callbackMessage); + try (KafkaProducer<String, String> producer = createProducer(props)) { + ProducerRecord<String, String> record = buildProducerRecord(props, callbackMsg); + producer.send(record); + LOG.info(String.format("Send callback message %s succeed", callbackMsg)); + } catch (Exception e) { + LOG.error("Send kafka callback msg failed : " + e); + } + } + + /** + * Method helps to create {@link KafkaProducer}. Here we set acks = all and retries = 3 by default to ensure no data + * loss. + * + * @param props Kafka configs + * @return A {@link KafkaProducer} + */ + public KafkaProducer<String, String> createProducer(Properties props) { + Properties kafkaProducerProps = new Properties(); + // bootstrap.servers + kafkaProducerProps.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); + // default "all" to ensure no message loss + kafkaProducerProps.setProperty(ProducerConfig.ACKS_CONFIG, props.getProperty(CALLBACK_KAFKA_ACKS)); + // retries 3 times by default + kafkaProducerProps.setProperty(ProducerConfig.RETRIES_CONFIG, props.getProperty(CALLBACK_KAFKA_RETRIES)); + kafkaProducerProps.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, + "org.apache.kafka.common.serialization.StringSerializer"); + kafkaProducerProps.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, + "org.apache.kafka.common.serialization.StringSerializer"); + + LOG.info("Callback kafka producer init with configs: " + + HoodieWriteCommitCallbackUtil.convertToJsonString(kafkaProducerProps)); + return new KafkaProducer<String, String>(kafkaProducerProps); + } + + /** + * Method helps to create a {@link ProducerRecord}. To ensure the order of the callback messages, we should guarantee + * that the callback message of the same table will goes to the same partition. Therefore, if user does not specify + * the partition, we can use the table name as {@link ProducerRecord} key. + * + * @param props Kafka configs + * @param callbackMsg Callback message + * @return Callback {@link ProducerRecord} + */ + private ProducerRecord<String, String> buildProducerRecord(Properties props, String callbackMsg) { + String partition = props.getProperty(CALLBACK_KAFKA_PARTITION); + if (null != partition) { + return new ProducerRecord<String, String>(topic, + Integer.valueOf(partition), + props.getProperty(TABLE_NAME), + callbackMsg); + } else { + return new ProducerRecord<String, String>(topic, Review comment: done ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
