Streaming with Apache Spark Custom Receiver

Reading Time: 2 minutes

Hello inqisitor. In previous blog we have seen about the predefined Stream receiver of Spark.

In this blog we are going to discuss about Custom receiver of spark so that we can source the data from any .

So if we want to use Custom Receiver than we should know first we are not going to use SparkSession as entry point , if there are not any such use case .

So at first you should add following dependency to build.sbt :

“org.apache.spark” %% “spark-streaming” % “2.0.0”

Now create a class CustomReceiver which should extends Receiver class  and override onStart() and onStop().

Here onStart() will contain the code to retrieve data from external source periodically and to store the data to stream using store().

Now what we ned is to configure StreamingContext : 

and at final step we have to tell Spark StreamingContext about CustomReceiver :

After all these things we can do transformation on streamed data.Transformation allows the data from the input DStream to be modified :

Now we are much concern about transformatio of spark. Computation may be of following type :

  • map(func)
  • flatMap(func)
  • filter(func)
  • repartition(numPartitions)
  • union(otherStream)
  • count()
  • reduce(func)
  • countByValue()
  • reduceByKey(func, [numTasks])
  • join(otherStream, [numTasks])
  • cogroup(otherStream, [numTasks])
  • transform(func)
  • updateStateByKey(func)

I was much curious about how the Computation will be performed on stream. And i got that we can perform Windowed Computation as :

Spark Streaming

Now you please fasten your seatbelts at this time. and here we go :

You can find complete code here



Written by 

Software Consultant At Knoldus

1 thought on “Streaming with Apache Spark Custom Receiver2 min read

Comments are closed.