cameronlee314 commented on a change in pull request #71: SAMZA-2437: Sample for producing to Azure Blob Storage URL: https://github.com/apache/samza-hello-samza/pull/71#discussion_r371586933
########## File path: src/main/java/samza/examples/azure/AzureBlobApplication.java ########## @@ -0,0 +1,136 @@ +/* + * 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 samza.examples.azure; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import java.util.List; +import java.util.Map; +import org.apache.samza.application.StreamApplication; +import org.apache.samza.application.descriptors.StreamApplicationDescriptor; +import org.apache.samza.operators.MessageStream; +import org.apache.samza.operators.OutputStream; +import org.apache.samza.serializers.JsonSerdeV2; +import org.apache.samza.serializers.NoOpSerde; +import org.apache.samza.system.descriptors.GenericOutputDescriptor; +import org.apache.samza.system.descriptors.GenericSystemDescriptor; +import org.apache.samza.system.kafka.descriptors.KafkaInputDescriptor; +import org.apache.samza.system.kafka.descriptors.KafkaSystemDescriptor; +import samza.examples.azure.data.PageViewAvroRecord; +import samza.examples.cookbook.data.PageView; + +/** + * In this example, we demonstrate sending blobs to Azure Blob Storage. + * This Samza job reads from Kafka topic "page-view-azure-blob-input" and produces blobs to Azure-Container "oss-testcontainer" in your Azure Storage account. + * + * Currently, Samza supports sending Avro files are blobs. + * Hence the incoming messages into the Samza job have to be converted to an Avro record. + * For this job, we use input message as {@link samza.examples.cookbook.data.PageView} and + * covert it to an Avro record defined as {@link samza.examples.azure.data.PageViewAvroRecord}. + * + * To run the below example: + * + * <ol> + * <li> + * Replace your-azure-storage-account-name and your-azure-storage-account-key with details of your Azure Storage Account. + * </li> + * <li> + * Ensure that the topic "page-view-azure-blob-input" is created <br/> + * ./deploy/kafka/bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic page-view-azure-blob-input --partitions 1 --replication-factor 1 + * </li> + * <li> + * Run the application using the run-app.sh script <br/> + * ./deploy/samza/bin/run-app.sh --config-factory=org.apache.samza.config.factories.PropertiesConfigFactory --config-path=file://$PWD/deploy/samza/config/azure-blob-application.properties + * </li> + * <li> + * Produce some messages to the "page-view-azure-blob-input" topic <br/> + * ./deploy/kafka/bin/kafka-console-producer.sh --topic page-view-azure-blob-input --broker-list localhost:9092 <br/> + * {"userId": "user1", "country": "india", "pageId":"google.com"} <br/> + * {"userId": "user2", "country": "france", "pageId":"facebook.com"} <br/> + * {"userId": "user3", "country": "china", "pageId":"yahoo.com"} <br/> + * {"userId": "user4", "country": "italy", "pageId":"linkedin.com"} <br/> + * {"userId": "user5", "country": "germany", "pageId":"amazon.com"} <br/> + * {"userId": "user6", "country": "denmark", "pageId":"apple.com"} <br/> + * </li> + * <li> + * Seeing Output: + * <ol> + * <li> + * See blobs in your Azure portal at https://<azure-storage-account-name>.blob.core.windows.net/oss-testcontainer/PageViewEventStream/<time-stamp>.avro + * </li> + * <li> + * system-name "oss-testcontainer" in configs and code below maps to Azure-Container in Azure Storage account. + * </li> + * <li> + * <time-stamp> is of the format yyyy/MM/dd/HH/mm-ss-randomString.avro. Hence navigate through the virtual folders on the portal to see your blobs. + * </li> + * <li> + * Due to network calls, allow a few minutes for blobs to appear on the portal. + * </li> + * <li> + * Config "maxMessagesPerBlob=2" ensures that a blob is created per 2 input messages. Adjust input or config accordingly. + * </li> + * </ol> + * </li> + * </ol> + */ +public class AzureBlobApplication implements StreamApplication { + private static final List<String> KAFKA_CONSUMER_ZK_CONNECT = ImmutableList.of("localhost:2181"); + private static final List<String> KAFKA_PRODUCER_BOOTSTRAP_SERVERS = ImmutableList.of("localhost:9092"); + private static final Map<String, String> KAFKA_DEFAULT_STREAM_CONFIGS = ImmutableMap.of("replication.factor", "1"); + private static final String INPUT_PAGEVIEW_STREAM_ID = "page-view-azure-blob-input"; + private static final String OUTPUT_SYSTEM = "oss-testcontainer"; + private static final String OUTPUT_STREAM = "PageViewEventStream"; + + @Override + public void describe(StreamApplicationDescriptor appDescriptor) { + // Define a system descriptor for Kafka + KafkaSystemDescriptor kafkaSystemDescriptor = + new KafkaSystemDescriptor("kafka").withConsumerZkConnect(KAFKA_CONSUMER_ZK_CONNECT) + .withProducerBootstrapServers(KAFKA_PRODUCER_BOOTSTRAP_SERVERS) + .withDefaultStreamConfigs(KAFKA_DEFAULT_STREAM_CONFIGS); + + KafkaInputDescriptor<PageView> pageViewInputDescriptor = + kafkaSystemDescriptor.getInputDescriptor(INPUT_PAGEVIEW_STREAM_ID, new JsonSerdeV2<>(PageView.class)); + + // Define a system descriptor for Azure Blob Storage + GenericSystemDescriptor azureBlobSystemDescriptor = + new GenericSystemDescriptor(OUTPUT_SYSTEM, "org.apache.samza.system.azureblob.AzureBlobSystemFactory"); Review comment: It might be nice to have a system descriptor for the azure blob system, but probably not necessary now. The benefit is that you could move some of the properties into this file directly. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services