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
Newsletter
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

  • Introducing senro: A Pipeline Engine You Program in Go
  • A Walk-Through of the DZone Article Editor
  • The Context Window Trap: Why More Context Doesn’t Mean Better AI
  • Agentic Test Creation: From Plain-Language Requirements to End-to-End Test Cases

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jun. 17, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
18.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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