io.kestra.plugin.cassandra.Query Maven / Gradle / Ivy
package io.kestra.plugin.cassandra;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ColumnDefinition;
import com.datastax.oss.driver.api.core.cql.ColumnDefinitions;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.data.CqlDuration;
import com.datastax.oss.driver.api.core.data.GettableByIndex;
import com.datastax.oss.driver.api.core.data.TupleValue;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.executions.metrics.Counter;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.models.tasks.Task;
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 java.io.*;
import java.net.InetAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import javax.validation.constraints.NotNull;
import static io.kestra.core.utils.Rethrow.throwConsumer;
@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
@Schema(
title = "Query a Cassandra database with CQL."
)
@Plugin(
examples = {
@Example(
title = "Send a cql query to a Cassandra Database",
code = {
"session:",
" host: localhost",
" username: cassandra_user",
" password: cassandra_passwd",
"cql: SELECT * FROM CQL_KEYSPACE.CQL_TABLE",
}
),
}
)
public class Query extends Task implements RunnableTask, QueryInterface {
@Schema(
title = "The session connection properties"
)
@PluginProperty(dynamic = true)
@NotNull
protected CassandraDbSession session;
@Builder.Default
private boolean fetch = false;
@Builder.Default
private boolean store = false;
@Builder.Default
private boolean fetchOne = false;
protected String cql;
public Query.Output run(RunContext runContext) throws Exception {
try (CqlSession session = this.session.connect(runContext)) {
ResultSet rs = session.execute(runContext.render(cql));
ColumnDefinitions columnDefinitions = rs.getColumnDefinitions();
Output.OutputBuilder outputBuilder = Output.builder()
.bytes(rs.getExecutionInfo().getResponseSizeInBytes());
if (this.fetchOne) {
outputBuilder
.row(convertRow(rs.one(), columnDefinitions))
.size(1L);
} else if (this.store) {
File tempFile = runContext.tempFile(".ion").toFile();
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(tempFile));
AtomicLong count = new AtomicLong();
try (OutputStream outputStream = new FileOutputStream(tempFile)) {
rs.forEach(throwConsumer(row -> {
count.getAndIncrement();
FileSerde.write(outputStream, convertRow(row, columnDefinitions));
}));
}
fileWriter.flush();
fileWriter.close();
outputBuilder
.uri(runContext.putTempFile(tempFile))
.size(count.get());
} else if (this.fetch) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy