chamikaramj commented on code in PR #24799: URL: https://github.com/apache/beam/pull/24799#discussion_r1092648906
########## examples/java/src/main/java/org/apache/beam/examples/io/examplekafkaread/ExampleKafkaReadIO.java: ########## @@ -0,0 +1,134 @@ +/* + * 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.beam.examples.io.examplekafkaread; + +import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument; + +import com.google.auto.value.AutoValue; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.beam.sdk.annotations.Experimental; +import org.apache.beam.sdk.annotations.Experimental.Kind; +import org.apache.beam.sdk.transforms.Impulse; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.PBegin; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap; +import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.dataflow.qual.Pure; + +/** + * An example unbounded SDF IO, using Kafka as an underlying source. This IO deliberately has a + * minimal set of Kafka features, and exists as an example of how to write an unbounded SDF source. + * + * <p>This IO should not be used in pipelines, and has no guarantees for quality, correctness, or + * performance. + * + * <p>This IO was generated as a pared down version of KafkaIO, and should act to consume + * KV<byte[],byte[]> pairs from Kafka In practice, this IO would be used thusly: pipeline + * .apply(ExampleKafkaReadIO.read() .withBootstrapServers("broker_1:9092,broker_2:9092") + * .withTopics(ImmutableList.of("my_topic"))) .apply(Some Other Transforms); + */ +public class ExampleKafkaReadIO { + + public static Read read() { + return new AutoValue_ExampleKafkaReadIO_Read.Builder() + .setTopics(new ArrayList<>()) + .setConsumerConfig(ExampleKafkaReadIOUtils.DEFAULT_CONSUMER_PROPERTIES) + .build(); + } + + @AutoValue + @AutoValue.CopyAnnotations + public abstract static class Read extends PTransform<PBegin, PCollection<KV<byte[], byte[]>>> { + + @Pure + abstract Map<String, Object> getConsumerConfig(); + + @Pure + abstract List<String> getTopics(); + + abstract Read.Builder toBuilder(); + + @Experimental(Kind.PORTABILITY) + @AutoValue.Builder + abstract static class Builder { + abstract Builder setConsumerConfig(Map<String, Object> config); + + abstract Builder setTopics(List<String> topics); + + abstract Read build(); + } + + /** + * This expand method is characteristic of many top level expands for IOs. It validates required + * parameters and configurations, and then it applies a series of DoFns to actually execute the + * read or write. It is typical to have multiple DoFns here, as frequently an IO needs to Review Comment: How about: s\\ "It is typical to have multiple DoFns here , as frequently an IO needs to determine what to read from the configuration before it actually reads" \\"It's typical for I/O connector transforms to be composite transforms since I/O connectors usually requires multiple computation units that have to be separated out for correctness or efficiency" -- 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]
