twitterStream example

Let us look at another example of how we can process tweets from Twitter using Spark Streaming:

  1. First, open a terminal and change the directory to spark-2.1.1-bin-hadoop2.7.
  2. Create a folder streamouts under the spark-2.1.1-bin-hadoop2.7 folder where you have spark installed. When the application runs, streamouts folder will have collected tweets to text files.
  3. Download the following jars into the directory:
  4. Launch spark-shell with the jars needed for Twitter integration specified:
      ./bin/spark-shell --jars twitter4j-stream-4.0.6.jar,
twitter4j-core-4.0.6.jar,
spark-streaming-twitter_2.11-2.1.0.jar
  1. Now, we can write a sample code. Shown in the following is the code to test Twitter event processing:
        import org.apache.spark._
import org.apache.spark.streaming._
import org.apache.spark.streaming.Twitter._
import twitter4j.auth.OAuthAuthorization
import twitter4j.conf.ConfigurationBuilder

//you can replace the next 4 settings with your own Twitter
account settings.
System.setProperty("twitter4j.oauth.consumerKey",
"8wVysSpBc0LGzbwKMRh8hldSm")
System.setProperty("twitter4j.oauth.consumerSecret",
"FpV5MUDWliR6sInqIYIdkKMQEKaAUHdGJkEb4MVhDkh7dXtXPZ")
System.setProperty("twitter4j.oauth.accessToken",
"817207925756358656-yR0JR92VBdA2rBbgJaF7PYREbiV8VZq")
System.setProperty("twitter4j.oauth.accessTokenSecret",
"JsiVkUItwWCGyOLQEtnRpEhbXyZS9jNSzcMtycn68aBaS")

val ssc = new StreamingContext(sc, Seconds(10))

val twitterStream = TwitterUtils.createStream(ssc, None)

twitterStream.saveAsTextFiles("streamouts/tweets", "txt")
ssc.start()

//wait for 30 seconds

ss.stop(false)

You will see the streamouts folder contains several tweets output in text files. You can now open the directory streamouts and check that the files contain tweets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.183.152