markmckeown commented on code in PR #4923: URL: https://github.com/apache/polaris/pull/4923#discussion_r3497551683
########## extensions/events/kafka/src/main/java/org/apache/polaris/extensions/events/kafka/KafkaEventListener.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.polaris.extensions.events.kafka; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.smallrye.common.annotation.Identifier; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.UUID; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.rest.requests.RenameTableRequest; +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.kafka.common.serialization.StringSerializer; +import org.apache.kafka.common.serialization.UUIDSerializer; +import org.apache.polaris.service.events.EventAttributes; +import org.apache.polaris.service.events.PolarisEvent; +import org.apache.polaris.service.events.listeners.PolarisEventListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Event Listener that pushes Polaris events to Kafka. */ +@ApplicationScoped +@Identifier("kafka") +public class KafkaEventListener implements PolarisEventListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(KafkaEventListener.class); + private KafkaProducer<UUID, String> producer; + private final String topic; + private final boolean synchronousMode; + private final ObjectMapper objectMapper; + private final Map<String, String> kafkaProperties; + + @Inject + public KafkaEventListener( + KafkaEventListenerConfiguration configuration, ObjectMapper objectMapper) { + this.synchronousMode = configuration.synchronousMode(); + this.topic = configuration.topic(); + this.objectMapper = objectMapper; + this.kafkaProperties = new HashMap<>(configuration.properties()); + } + + @PostConstruct + void start() { + LOGGER.debug("Starting KafkaEventListener."); + Properties props = new Properties(); + props.putAll(this.kafkaProperties); + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, UUIDSerializer.class.getName()); + props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); + this.producer = new KafkaProducer<>(props); + } + + @PreDestroy + void shutdown() { + if (producer != null) { + producer.close(); + producer = null; + } + } + + @Override + public void onEvent(PolarisEvent event) { + HashMap<String, Object> eventProperties = new HashMap<>(); + eventProperties.put("event_type", event.type().name()); Review Comment: I will have a look. Would adding more information to AwsCloudWatchEventListener cause any problems? ########## extensions/events/kafka/src/main/java/org/apache/polaris/extensions/events/kafka/KafkaEventListenerConfiguration.java: ########## @@ -0,0 +1,65 @@ +/* + * 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.polaris.extensions.events.kafka; + +import io.quarkus.runtime.annotations.StaticInitSafe; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithDefault; +import io.smallrye.config.WithName; +import jakarta.enterprise.context.ApplicationScoped; +import java.util.Map; + +/** Configuration interface for the Kafka Event Listener. */ +@StaticInitSafe +@ConfigMapping(prefix = "polaris.event-listener.kafka") +@ApplicationScoped +public interface KafkaEventListenerConfiguration { + + /** + * The Kafka topic to send Polaris events to. + * + * <p>Configuration property: {@code polaris.event-listener.kafka.topic} + */ + @WithName("topic") + @WithDefault("polaris-catalog-events") + String topic(); + + /** + * Returns the synchronous mode setting for sending events to Kafka. + * + * <p>When set to "true", log events are sent to Kafka synchronously, which may impact application + * performance but ensures immediate delivery. When set to "false" (default), log events are sent + * asynchronously for better performance. + * + * <p>Configuration property: {@code polaris.event-listener.kafka.synchronous-mode} + * + * @return a boolean value indicating the synchronous mode setting + */ + @WithName("synchronous-mode") + @WithDefault("false") + boolean synchronousMode(); + + /** + * Kafka properties to pass to the Kafka producer, for example bootstap.servers. This can be used Review Comment: Thanks -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
