
io.kestra.plugin.kafka.Trigger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugin-kafka Show documentation
Show all versions of plugin-kafka Show documentation
Leverage Apache Kafka messaging in Kestra data workflows.
package io.kestra.plugin.kafka;
import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.conditions.ConditionContext;
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.executions.ExecutionTrigger;
import io.kestra.core.models.flows.State;
import io.kestra.core.models.triggers.AbstractTrigger;
import io.kestra.core.models.triggers.PollingTriggerInterface;
import io.kestra.core.models.triggers.TriggerContext;
import io.kestra.core.models.triggers.TriggerOutput;
import io.kestra.core.runners.RunContext;
import io.kestra.core.utils.IdUtils;
import io.kestra.plugin.kafka.serdes.SerdeType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.slf4j.Logger;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
@Schema(
title = "Wait for messages on Kafka topics"
)
@Plugin(
examples = {
@Example(
code = {
"topic: test_kestra",
"properties:",
" bootstrap.servers: localhost:9092",
"serdeProperties:",
" schema.registry.url: http://localhost:8085",
"keyDeserializer: STRING",
"valueDeserializer: AVRO",
"valueAvroSchema: |",
" {\"type\":\"record\",\"name\":\"twitter_schema\",\"namespace\":\"io.kestra.examples\",\"fields\":[{\"name\":\"username\",\"type\":\"string\"},{\"name\":\"tweet\",\"type\":\"string\"}]}"
}
)
}
)
public class Trigger extends AbstractTrigger implements PollingTriggerInterface, TriggerOutput, KafkaConnectionInterface, ConsumeInterface {
@Builder.Default
private final Duration interval = Duration.ofSeconds(60);
private Map properties;
@Builder.Default
private Map serdeProperties = Collections.emptyMap();
private Object topic;
private String groupId;
private SerdeType keyDeserializer;
private SerdeType valueDeserializer;
private String since;
@Builder.Default
private Duration pollDuration = Duration.ofSeconds(2);
private Integer maxRecords;
private Duration maxDuration;
@Override
public Optional evaluate(ConditionContext conditionContext, TriggerContext context) throws Exception {
RunContext runContext = conditionContext.getRunContext();
Logger logger = runContext.logger();
Consume task = Consume.builder()
.id(this.id)
.type(Consume.class.getName())
.properties(this.properties)
.serdeProperties(this.serdeProperties)
.topic(this.topic)
.groupId(this.groupId)
.keyDeserializer(this.keyDeserializer)
.valueDeserializer(this.valueDeserializer)
.since(this.since)
.pollDuration(this.pollDuration)
.maxRecords(this.maxRecords)
.maxDuration(this.maxDuration)
.build();
Consume.Output run = task.run(runContext);
if (logger.isDebugEnabled()) {
logger.debug("Found '{}' messages from '{}'", run.getMessagesCount(), task.topics(runContext));
}
if (run.getMessagesCount() == 0) {
return Optional.empty();
}
String executionId = IdUtils.create();
ExecutionTrigger executionTrigger = ExecutionTrigger.of(
this,
run
);
Execution execution = Execution.builder()
.id(executionId)
.namespace(context.getNamespace())
.flowId(context.getFlowId())
.flowRevision(context.getFlowRevision())
.state(new State())
.trigger(executionTrigger)
.build();
return Optional.of(execution);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy