Github user tdas commented on a diff in the pull request:
https://github.com/apache/spark/pull/1434#discussion_r15568742
--- Diff:
extras/spark-kinesis-asl/src/main/scala/org/apache/spark/examples/streaming/KinesisWordCount.scala
---
@@ -0,0 +1,345 @@
+/*
+ * 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.spark.examples.streaming
+
+import java.nio.ByteBuffer
+import org.apache.log4j.Level
+import org.apache.log4j.Logger
+import org.apache.spark.Logging
+import org.apache.spark.SparkConf
+import org.apache.spark.SparkContext._
+import org.apache.spark.streaming.Milliseconds
+import org.apache.spark.streaming.StreamingContext
+import org.apache.spark.streaming.StreamingContext.toPairDStreamFunctions
+import org.apache.spark.streaming.kinesis.KinesisStringRecordSerializer
+import org.apache.spark.streaming.kinesis.KinesisUtils
+import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
+import com.amazonaws.services.kinesis.AmazonKinesisClient
+import com.amazonaws.services.kinesis.model.PutRecordRequest
+import scala.util.Random
+import
com.amazonaws.services.kinesis.clientlibrary.lib.worker.InitialPositionInStream
+import org.apache.spark.storage.StorageLevel
+import org.apache.spark.streaming.dstream.ReceiverInputDStream
+import org.apache.spark.streaming.dstream.DStream
+
+/**
+ * Kinesis Spark Streaming WordCount example.
+ *
+ * See
http://spark.apache.org/docs/latest/streaming-programming-guide.html for more
details on the Kinesis Spark Streaming integration.
+ *
+ * This example spins up 1 Kinesis Worker (Spark Streaming Receivers) per
shard of the given stream.
+ * It then starts pulling from the tip of the given <stream-name> and
<endpoint-url> at the given <batch-interval>.
+ * Because we're pulling from the tip (InitialPositionInStream.LATEST),
only new stream data will be picked up after the KinesisReceiver starts.
+ * This could lead to missed records if data is added to the stream while
no KinesisReceivers are running.
+ * In production, you'll want to switch to
InitialPositionInStream.TRIM_HORIZON which will read up to 24 hours (Kinesis
limit) of previous stream data
+ * depending on the checkpoint frequency.
+ *
+ * InitialPositionInStream.TRIM_HORIZON may lead to duplicate processing
of records depending on the checkpoint frequency.
+ * Record processing should be idempotent when possible.
+ *
+ * This code uses the DefaultAWSCredentialsProviderChain and searches for
credentials in the following order of precedence:
+ * Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_KEY
+ * Java System Properties - aws.accessKeyId and aws.secretKey
+ * Credential profiles file - default location (~/.aws/credentials) shared
by all AWS SDKs
+ * Instance profile credentials - delivered through the Amazon EC2
metadata service
+ *
+ * Usage: KinesisWordCount <stream-name> <endpoint-url> <batch-interval>
+ * <stream-name> is the name of the Kinesis stream (ie. mySparkStream)
+ * <endpoint-url> is the endpoint of the Kinesis service (ie.
https://kinesis.us-east-1.amazonaws.com)
+ * <batch-interval> is the batch interval in millis (ie. 1000ms)
+ *
+ * Example:
+ * $ export AWS_ACCESS_KEY_ID=<your-access-key>
+ * $ export AWS_SECRET_KEY=<your-secret-key>
+ * $ bin/run-kinesis-example \
+ * org.apache.spark.examples.streaming.KinesisWordCount
mySparkStream https://kinesis.us-east-1.amazonaws.com 100
+ *
+ * There is a companion helper class below called KinesisWordCountProducer
which puts dummy data onto the Kinesis stream.
+ * Usage instructions for KinesisWordCountProducer are provided in that
class definition.
+ */
+object KinesisWordCount extends Logging {
+ val WordSeparator = " "
+
+ def main(args: Array[String]) {
+/**
+ * Check that all required args were passed in.
+ */
+ if (args.length < 3) {
+ System.err.println("Usage: KinesisWordCount <stream-name>
<endpoint-url> <batch-interval>")
+ System.exit(1)
+ }
+
+ /**
+ * (This was lifted from the StreamingExamples.scala in order to avoid
the dependency on the spark-examples artifact.)
+ * Set reasonable logging levels for streaming if the user has not
configured log4j.
+ */
+ val log4jInitialized =
Logger.getRootLogger.getAllAppenders.hasMoreElements
+ if (!log4jInitialized) {
+ /** We first log something to initialize Spark's default logging,
then we override the logging level. */
+ logInfo("Setting log level to [INFO] for streaming example." +
+ " To override add a custom log4j.properties to the classpath.")
+
+ Logger.getRootLogger().setLevel(Level.INFO)
+
Logger.getLogger("org.apache.spark.examples.streaming").setLevel(Level.DEBUG);
+ }
+
+ /** Populate the appropriate variables from the given args */
+ val Array(stream, endpoint, batchIntervalMillisStr) = args
+ val batchIntervalMillis = batchIntervalMillisStr.toInt
+
+ /** Create a Kinesis client in order to determine the number of shards
for the given stream */
+ val KinesisClient = new AmazonKinesisClient(new
DefaultAWSCredentialsProviderChain());
+
+ /** Determine the number of shards from the stream */
+ val numShards =
KinesisClient.describeStream(stream).getStreamDescription().getShards().size()
+
+ /** In this example, we're going to create 1 Kinesis
Worker/Receiver/DStreams for each stream shard */
+ val numStreams = numShards
+
+ /** Must add 1 more thread than the number of receivers or the output
won't show properly from the driver */
+ val numSparkThreads = numStreams + 1
+
+ /** Set the app name */
+ val app = "KinesisWordCount"
+
+ /** Setup the Spark config. */
+ val sparkConfig = new
SparkConf().setAppName(app).setMaster(s"local[$numSparkThreads]")
+
+ /**
+ * Set the batch interval.
+ * Records will be pulled from the Kinesis stream and stored as a
single DStream within Spark every batch interval.
+ */
+ val batchInterval = Milliseconds(batchIntervalMillis)
+
+ /**
+ * It's recommended that you perform a Spark checkpoint between 5 and
10 times the batch interval.
+ * While this is the Spark checkpoint interval, we're going to use it
for the Kinesis checkpoint interval, as well.
+ */
+ val checkpointInterval = batchInterval * 5
+
+ /** Setup the StreamingContext */
+ val ssc = new StreamingContext(sparkConfig, batchInterval)
+
+ /** Setup the checkpoint directory used by Spark Streaming */
+ ssc.checkpoint("/tmp/checkpoint");
+
+ /** Create the same number of Kinesis Receivers/DStreams as stream
shards, then union them all */
+ var allStreams: DStream[Array[Byte]] = KinesisUtils.createStream(ssc,
app, stream, endpoint, checkpointInterval.milliseconds,
+ InitialPositionInStream.LATEST, StorageLevel.MEMORY_AND_DISK_2)
+ /** Set the checkpoint interval */
+ allStreams.checkpoint(checkpointInterval)
+ for (i <- 1 until numStreams) {
+ /** Create a new Receiver/DStream for each stream shard */
+ val dStream = KinesisUtils.createStream(ssc, app, stream, endpoint,
checkpointInterval.milliseconds,
+ InitialPositionInStream.LATEST, StorageLevel.MEMORY_AND_DISK_2)
+ /** Set the Spark checkpoint interval */
+ dStream.checkpoint(checkpointInterval)
+
+ /** Union with the existing streams */
+ allStreams = allStreams.union(dStream)
+ }
+
+ /** This implementation uses the String-based KinesisRecordSerializer
impl */
+ val recordSerializer = new KinesisStringRecordSerializer()
+
+ /**
+ * Sort and print the given dstream.
+ * This is an Output Operation that will materialize the underlying
DStream.
+ * Everything up to this point is a lazy Transformation Operation.
+ *
+ * @param description of the dstream for logging purposes
+ * @param dstream to sort and print
+ */
+ def sortAndPrint(description: String, dstream: DStream[(String,Int)])
= {
+ dstream.foreachRDD((batch, endOfWindowTime) => {
--- End diff --
This is great example with a lot of functionality but for a newbie trying
to learn Spark Streaming + Kinesis, this example looks a little scary because
of its size. All we want to demonstrate in this example is how to use
KinesisUtils, not that you can use window operations and stuff. There are other
examples for that. So I would prefer this example being very short and to the
point, like KafkaWordCount. Can you please change it to something like that?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---