DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Streaming With Apache Spark Custom Receiver

Streaming With Apache Spark Custom Receiver

This article covers writing a custom Apache Spark 2.0 Streaming Receiver in Scala with detailed code for general Spark 2.0 style streaming.

Rahul Kumar user avatar by
Rahul Kumar
·
Oct. 07, 16 · Tutorial
Like (4)
Save
Tweet
Share
4.99K Views

Join the DZone community and get the full member experience.

Join For Free

Hello inquisitor. In a previous post, we covered the predefined Stream receiver of Spark. In this blog, we are going to discuss the 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 an entry point, if there are not any such use case .

First, you should add the following dependency to build.sbt :

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

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

class CustomReceiver extends Receiver[String](StorageLevel.MEMORY_AND_DISK_2) {
  override def onStart(): Unit = ???
  override def onStop(): Unit = ???
}

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

override def onStart(): Unit = {
val externalData = retrieveExternalData();
store(externalData)
}

Now what we ned is to configure StreamingContext: 

val conf = new SparkConf()
.setAppName("wohooo")
.setMaster("local[2]")
val streamingContext = new StreamingContext(conf, Seconds.apply(2)

For the final step, we have to tell Spark StreamingContext about CustomReceiver:

val lines = streamingContext.receiverStream(new CustomReceiver)

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

val words: DStream[String] = lines.flatMap(_.split(","))

Now we are concerned about the transformation of spark. Computation may be of following types:

  • 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 very curious about how the Computation will be performed on stream. And i got that we can perform Windowed Computation as:

Spark Streaming

val windowedWords = words.reduceByWindow((a: String, b: String) => (a + b), Seconds(10), Seconds(4))

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

streamingContext.start()
streamingContext.awaitTermination()

You can find complete code here.

Receiver (information theory) Apache Spark

Published at DZone with permission of Rahul Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Playwright vs. Cypress: The King Is Dead, Long Live the King?
  • When AI Strengthens Good Old Chatbots: A Brief History of Conversational AI
  • Three SQL Keywords in QuestDB for Finding Missing Data
  • Using QuestDB to Collect Infrastructure Metrics

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: