Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KafkaTopic to kafka to gcs template #1602

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
import com.google.cloud.teleport.v2.kafka.options.KafkaReadOptions;
import com.google.cloud.teleport.v2.kafka.transforms.KafkaTransform;
import com.google.cloud.teleport.v2.kafka.utils.KafkaCommonUtils;
import com.google.cloud.teleport.v2.kafka.utils.KafkaTopicUtils;
import com.google.cloud.teleport.v2.transforms.WriteToGCSAvro;
import com.google.cloud.teleport.v2.transforms.WriteToGCSParquet;
import com.google.cloud.teleport.v2.transforms.WriteToGCSText;
import com.google.cloud.teleport.v2.transforms.WriteTransform;
import com.google.cloud.teleport.v2.utils.SecretManagerUtils;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -57,7 +56,8 @@
contactInformation = "https://cloud.google.com/support",
hidden = true,
streaming = true,
requirements = {"The output Google Cloud Storage directory must exist."})
requirements = {"The output Google Cloud Storage directory must exist."},
skipOptions = {"readBootstrapServers", "kafkaReadTopics"})
public class KafkaToGcsFlex {
/**
* The {@link KafkaToGcsOptions} interface provides the custom execution options passed by the
Expand All @@ -71,6 +71,17 @@
WriteToGCSParquet.WriteToGCSParquetOptions,
WriteToGCSAvro.WriteToGCSAvroOptions {

@TemplateParameter.KafkaTopic(
order = 1,
name = "readBootstrapServerAndTopic",
groupName = "Source",
optional = false,
description = "Source Kafka Topic",
helpText = "Kafka Topic to read the input from.")
String getReadBootstrapServerAndTopic();

void setReadBootstrapServerAndTopic(String value);

@TemplateParameter.Enum(
order = 3,
groupName = "MessageFormat",
Expand Down Expand Up @@ -200,10 +211,17 @@
// Create the Pipeline
Pipeline pipeline = Pipeline.create(options);

PCollection<KafkaRecord<byte[], byte[]>> kafkaRecord;

List<String> topics =
new ArrayList<>(Arrays.asList(options.getKafkaReadTopics().split(topicsSplitDelimiter)));
List<String> topicsList;
String bootstrapServers;
if (options.getReadBootstrapServerAndTopic() != null) {
List<String> bootstrapServerAndTopicList =
KafkaTopicUtils.getBootstrapServerAndTopic(options.getReadBootstrapServerAndTopic());
topicsList = List.of(bootstrapServerAndTopicList.get(1));
bootstrapServers = bootstrapServerAndTopicList.get(0);
} else {
throw new IllegalArgumentException(

Check warning on line 222 in v2/kafka-to-gcs/src/main/java/com/google/cloud/teleport/v2/templates/KafkaToGcsFlex.java

View check run for this annotation

Codecov / codecov/patch

v2/kafka-to-gcs/src/main/java/com/google/cloud/teleport/v2/templates/KafkaToGcsFlex.java#L217-L222

Added lines #L217 - L222 were not covered by tests
"Please provide a valid bootstrap server which matches `[,:a-zA-Z0-9._-]+` and a topic which matches `[,a-zA-Z0-9._-]+`");
}

options.setStreaming(true);

Expand All @@ -219,11 +237,12 @@
// Configure offset value, group id and finalizing offset to consumer group.
kafkaConfig.putAll(KafkaCommonUtils.configureKafkaOffsetCommit(options));

PCollection<KafkaRecord<byte[], byte[]>> kafkaRecord;
// Step 1: Read from Kafka as bytes.
KafkaIO.Read<byte[], byte[]> kafkaTransform =
KafkaTransform.readBytesFromKafka(
options.getReadBootstrapServers(),
topics,
topicsList,
kafkaConfig,
null,
options.getEnableCommitOffsets());
Expand Down
Loading