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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Trending

  • A Hands-On ABAP RESTful Programming Model Guide
  • A System Cannot Protect What It Does Not Understand
  • Multi-Scale Feature Learning in CNN and U-Net Architectures
  • How to Format Articles for DZone
  1. DZone
  2. Coding
  3. Languages
  4. Twitter Live Streaming With Spark Streaming (Using Scala)

Twitter Live Streaming With Spark Streaming (Using Scala)

In this post, we go through a quick step-by-step demonstration of how to use Spark streaming techniques with a Twitter application. Let's get to it!

By 
Joydeep Das user avatar
Joydeep Das
·
Jun. 17, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
18.6K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

In this article, we are trying to demonstrate how to use Spark streaming with Twitter. It’s a demo only article and does not discuss anything in regards to Spark streaming techniques.

Create Your Twitter App

To stream Twitter data, you must create a Twitter app first.

You need 4 key values from the “Key and Access Token” in your Twitter app.

  1. Consumer Key (API Key)

  2. Consumer Secret (API Secret)

  3. Access Token              

  4. Access Token Secret

  5. Optional - Filter Key - if we need to filter any specified subject in Twitter.

Scala IDE Build of Eclipse SDK Change the pom.xml Dependency

We need to add the dependency to our pom.xml file in the Scala IDE build environment of our Eclipse SDK.

<dependency>
  <groupId>org.apache.spark</groupId>
  <artifactId>spark-streaming-twitter_2.10</artifactId>
  <version>1.0.0</version>
</dependency>

Adding twitter4j-core-3.0.6.jar

We need to add a reference to the “twitter4j-core-3.0.6.jar” file in the Scala IDE build of the Eclipse SDK. We can download the “twitter4j-core-3.0.6.jar” jar file from a Maven repository.

Image title

Image title

Create Scala Code

Image title

Scala Code Details

package com.sqlknowledgebank.spark.sparkstreaming

import org.apache.spark.SparkConf
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.Seconds
import twitter4j.conf.ConfigurationBuilder
import twitter4j.auth.OAuthAuthorization
import twitter4j.Status
import org.apache.spark.streaming.twitter.TwitterUtils


object twitterstreaming {
  def main(args: Array[String]) {
    if (args.length < 4) {
      System.err.println("Usage: TwitterData <ConsumerKey><ConsumerSecret><accessToken><accessTokenSecret>" +
        "[<filters>]")

      System.exit(1)
    }

    val appName = "TwitterData"
    val conf = new SparkConf()
    conf.setAppName(appName).setMaster("local[2]")
    val ssc = new StreamingContext(conf, Seconds(5))

    val Array(consumerKey, consumerSecret, accessToken, accessTokenSecret) = args.take(4)

    val filters = args.takeRight(args.length - 4)
    val cb = new ConfigurationBuilder

    cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)
      .setOAuthConsumerSecret(consumerSecret)
      .setOAuthAccessToken(accessToken)
      .setOAuthAccessTokenSecret(accessTokenSecret)

    val auth = new OAuthAuthorization(cb.build)
    val tweets = TwitterUtils.createStream(ssc, Some(auth), filters)


    tweets .saveAsTextFiles("tweets", "json")
    ssc.start()
    ssc.awaitTermination()
  }
}

Passing Command Line Parameters to Run

Image title

Hope this was informative!

twitter Scala (programming language)

Published at DZone with permission of Joydeep Das. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook