ilya-kozyrev commented on a change in pull request #13112:
URL: https://github.com/apache/beam/pull/13112#discussion_r528722776



##########
File path: 
examples/templates/java/kafka-to-pubsub/src/main/java/org/apache/beam/templates/KafkaToPubsub.java
##########
@@ -0,0 +1,319 @@
+/*
+ * 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.templates;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.PipelineResult;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubIO;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.transforms.Values;
+import org.apache.beam.templates.avro.TaxiRide;
+import org.apache.beam.templates.options.KafkaToPubsubOptions;
+import org.apache.beam.templates.transforms.FormatTransform;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+import org.apache.kafka.common.config.SaslConfigs;
+import org.apache.kafka.common.config.SslConfigs;
+import org.apache.kafka.common.security.auth.SecurityProtocol;
+import org.apache.kafka.common.security.scram.ScramMechanism;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The {@link KafkaToPubsub} pipeline is a streaming pipeline which ingests 
data in JSON format from
+ * Kafka, and outputs the resulting records to PubSub. Input topics, output 
topic, Bootstrap servers
+ * are specified by the user as template parameters. <br>
+ * Kafka may be configured with SASL/SCRAM security mechanism, in this case a 
Vault secret storage
+ * with credentials should be provided. URL to credentials and Vault token are 
specified by the user
+ * as template parameters.
+ *
+ * <p><b>Pipeline Requirements</b>
+ *
+ * <ul>
+ *   <li>Kafka Bootstrap Server(s).
+ *   <li>Kafka Topic(s) exists.
+ *   <li>The PubSub output topic exists.
+ *   <li>(Optional) An existing HashiCorp Vault secret storage
+ * </ul>
+ *
+ * <p><b>Example Usage</b>
+ *
+ * <pre>
+ * # Set the pipeline vars
+ * PROJECT=id-of-my-project
+ * BUCKET_NAME=my-bucket
+ *
+ * # Set containerization vars
+ * IMAGE_NAME=my-image-name
+ * TARGET_GCR_IMAGE=gcr.io/${PROJECT}/${IMAGE_NAME}
+ * BASE_CONTAINER_IMAGE=my-base-container-image
+ * TEMPLATE_PATH="gs://${BUCKET_NAME}/templates/kafka-pubsub.json"
+ *
+ * # Create bucket in the cloud storage
+ * gsutil mb gs://${BUCKET_NAME}
+ *
+ * # Go to the beam folder
+ * cd /path/to/beam
+ *
+ * <b>FLEX TEMPLATE</b>
+ * # Assemble uber-jar
+ * ./gradlew -p templates/kafka-to-pubsub clean shadowJar
+ *
+ * # Go to the template folder
+ * cd /path/to/beam/templates/kafka-to-pubsub
+ *
+ * # Build the flex template
+ * gcloud dataflow flex-template build ${TEMPLATE_PATH} \
+ *       --image-gcr-path "${TARGET_GCR_IMAGE}" \
+ *       --sdk-language "JAVA" \
+ *       --flex-template-base-image ${BASE_CONTAINER_IMAGE} \
+ *       --metadata-file "src/main/resources/kafka_to_pubsub_metadata.json" \
+ *       --jar 
"build/libs/beam-templates-kafka-to-pubsub-2.25.0-SNAPSHOT-all.jar" \
+ *       --env 
FLEX_TEMPLATE_JAVA_MAIN_CLASS="org.apache.beam.templates.KafkaToPubsub"
+ *
+ * # Execute template:
+ *    API_ROOT_URL="https://dataflow.googleapis.com";
+ *    
TEMPLATES_LAUNCH_API="${API_ROOT_URL}/v1b3/projects/${PROJECT}/locations/${REGION}/flexTemplates:launch"
+ *    JOB_NAME="kafka-to-pubsub-`date +%Y%m%d-%H%M%S-%N`"
+ *
+ *    time curl -X POST -H "Content-Type: application/json" \
+ *            -H "Authorization: Bearer $(gcloud auth print-access-token)" \
+ *            -d '
+ *             {
+ *                 "launch_parameter": {
+ *                     "jobName": "'$JOB_NAME'",
+ *                     "containerSpecGcsPath": "'$TEMPLATE_PATH'",
+ *                     "parameters": {
+ *                         "bootstrapServers": "broker_1:9091, broker_2:9092",
+ *                         "inputTopics": "topic1, topic2",
+ *                         "outputTopic": 
"projects/'$PROJECT'/topics/your-topic-name",
+ *                         "secretStoreUrl": 
"http(s)://host:port/path/to/credentials",
+ *                         "vaultToken": "your-token"
+ *                     }
+ *                 }
+ *             }
+ *            '
+ *            "${TEMPLATES_LAUNCH_API}"
+ * </pre>
+ *
+ * <p><b>Example Avro usage</b>
+ *
+ * <pre>
+ * This template contains an example Class to deserialize AVRO from Kafka and 
serialize it to AVRO in Pub/Sub.
+ *
+ * To use this example in the specific case, follow the few steps:
+ * <ul>
+ * <li> Create your own class to describe AVRO schema. As an example use 
{@link TaxiRide}. Just define necessary fields.
+ * <li> Create your own Avro Deserializer class. As an example use {@link 
org.apache.beam.templates.avro.TaxiRidesKafkaAvroDeserializer}. Just rename it, 
and put your own Schema class as the necessary types.
+ * <li> Modify the {@link FormatTransform}. Put your Schema class and 
Deserializer to the related parameter.
+ * <li> Modify write step in the {@link KafkaToPubsub} by put your Schema 
class to "writeAvrosToPubSub" step.
+ * </ul>

Review comment:
       I think README and javadoc have a bit different use-cases. When we work 
in IDE or text editors javadoc is a good place to look at class or module 
documentation, but when we work with GitHub, it provides us awesome approaches 
to render MD files and we can just open the template folder and look at the 
rendered readme. 




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to