io.kestra.plugin.pulsar.AbstractReader Maven / Gradle / Ivy
package io.kestra.plugin.pulsar;
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.models.executions.metrics.Counter;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.runners.RunContext;
import io.kestra.core.serializers.FileSerde;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.apache.pulsar.client.api.Message;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
public abstract class AbstractReader extends AbstractPulsarConnection implements ReadInterface, RunnableTask {
private Object topic;
@Builder.Default
private SerdeType deserializer = SerdeType.STRING;
@Builder.Default
private Duration pollDuration = Duration.ofSeconds(2);
private Integer maxRecords;
private Duration maxDuration;
public Output read(RunContext runContext, Supplier>> supplier) throws Exception {
File tempFile = runContext.tempFile(".ion").toFile();
Map count = new HashMap<>();
AtomicInteger total = new AtomicInteger();
ZonedDateTime started = ZonedDateTime.now();
ZonedDateTime lastPool = ZonedDateTime.now();
try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(tempFile))) {
do {
for (Message message : supplier.get()) {
// data to write
Map