<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://info319.wiki.uib.no/index.php?action=history&amp;feed=atom&amp;title=Spark_Streaming_Twitter</id>
	<title>Spark Streaming Twitter - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://info319.wiki.uib.no/index.php?action=history&amp;feed=atom&amp;title=Spark_Streaming_Twitter"/>
	<link rel="alternate" type="text/html" href="http://info319.wiki.uib.no/index.php?title=Spark_Streaming_Twitter&amp;action=history"/>
	<updated>2026-04-25T05:52:27Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>http://info319.wiki.uib.no/index.php?title=Spark_Streaming_Twitter&amp;diff=474&amp;oldid=prev</id>
		<title>Vimala: Created page with &quot;==Register a Twitter app== Create a http://twitter.com account if you do not have one and log into it.   Go to https://apps.twitter.com/ . Make sure you are still logged in: y...&quot;</title>
		<link rel="alternate" type="text/html" href="http://info319.wiki.uib.no/index.php?title=Spark_Streaming_Twitter&amp;diff=474&amp;oldid=prev"/>
		<updated>2018-10-25T17:03:15Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Register a Twitter app== Create a http://twitter.com account if you do not have one and log into it.   Go to https://apps.twitter.com/ . Make sure you are still logged in: y...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Register a Twitter app==&lt;br /&gt;
Create a http://twitter.com account if you do not have one and log into it. &lt;br /&gt;
&lt;br /&gt;
Go to https://apps.twitter.com/ . Make sure you are still logged in: you should see a drop-down menu in the upper right-hand corner of the page.&lt;br /&gt;
&lt;br /&gt;
Click &amp;#039;&amp;#039;Create New App&amp;#039;&amp;#039; and fill in as many details as you can (you can change most of them later). Click &amp;#039;&amp;#039;Create your Twitter application&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Go to &amp;#039;&amp;#039;Keys and Access Tokens&amp;#039;&amp;#039;. Click &amp;#039;&amp;#039;Create my access token&amp;#039;&amp;#039;. You will need the following four key strings need later (keep them secret to protect your Twitter account): &lt;br /&gt;
* Consumer Key (API Key)&lt;br /&gt;
* Consumer Secret (API Secret)&lt;br /&gt;
* Access Token&lt;br /&gt;
* Access Token Secret&lt;br /&gt;
&lt;br /&gt;
==Stream Twitter messages into Spark== &lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;1) You can use the following links to access the tutorial.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
*[https://tenmann.pw/info319/ Accessing Spark streaming Folder]&lt;br /&gt;
*[[:File:SS.zip|Spark streaming Tutorial files]]&lt;br /&gt;
*[[:File:Guide.pdf|Installaion guide for Spark streaming]]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;2)Or you can follow the below steps:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
To test that login works, open a &amp;#039;&amp;#039;spark-shell&amp;#039;&amp;#039; (remember the --jars SPARK_JARS option if you defined such an environment variable earlier), and import these APIs:&lt;br /&gt;
    import org.apache.spark._&lt;br /&gt;
    import org.apache.spark.streaming._&lt;br /&gt;
    import org.apache.spark.streaming.twitter._&lt;br /&gt;
&lt;br /&gt;
Set logging level (to avoid warnings from running Spark standalone), and create a new &amp;#039;&amp;#039;spark streaming context (ssc)&amp;#039;&amp;#039;:&lt;br /&gt;
    sc.setLogLevel(&amp;quot;ERROR&amp;quot;) &lt;br /&gt;
    val ssc = new StreamingContext(sc, Seconds(5))  &lt;br /&gt;
&lt;br /&gt;
Set system properties for each of your keys and access tokens provided by Twitter earlier:&lt;br /&gt;
    System.setProperty(&amp;quot;twitter4j.oauth.consumerKey&amp;quot;, &amp;quot;...copy this string from the Twitter app page...&amp;quot;)  &lt;br /&gt;
    System.setProperty(&amp;quot;twitter4j.oauth.consumerSecret&amp;quot;, &amp;quot;...copy this string from the Twitter app page...&amp;quot;)  &lt;br /&gt;
    System.setProperty(&amp;quot;twitter4j.oauth.accessToken&amp;quot;, &amp;quot;...copy this string from the Twitter app page...&amp;quot;)  &lt;br /&gt;
    System.setProperty(&amp;quot;twitter4j.oauth.accessTokenSecret&amp;quot;, &amp;quot;...copy this string from the Twitter app page...&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
Create a Spark stream with messages from Twitter:&lt;br /&gt;
    val stream = TwitterUtils.createStream(ssc, None)&lt;br /&gt;
&lt;br /&gt;
Because Spark has lazy evaluation, nothing happens until you have defined some transformations on the stream and started it. The next two lines collect messages from Twitter, identified by their &amp;#039;&amp;#039;status&amp;#039;&amp;#039;, split each message text into words, pick out only those words that start with a &amp;#039;&amp;#039;#&amp;#039;&amp;#039;, and print them to the console:&lt;br /&gt;
    val hashTags = stream.flatMap(status =&amp;gt; status.getText.split(&amp;quot; &amp;quot;).filter(_.startsWith(&amp;quot;#&amp;quot;)))&lt;br /&gt;
    hashTags.foreachRDD(rdd =&amp;gt; rdd.foreach(str =&amp;gt; println(&amp;quot;:: &amp;quot; + str)))&lt;br /&gt;
&lt;br /&gt;
==Starting and stopping streams==&lt;br /&gt;
You are now ready to start the stream:&lt;br /&gt;
    ssc.start&lt;br /&gt;
&lt;br /&gt;
This should write out current hashtags to the console. After a while, stop the stream with:&lt;br /&gt;
    ssc.stop(false, true)&lt;br /&gt;
Here is the documentation page for [https://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.streaming.StreamingContext Spark&amp;#039;s StreamingContext and other classes.]&lt;br /&gt;
&lt;br /&gt;
When a streaming context has been stopped, it cannot be restarted, but it can be recreated as follows:&lt;br /&gt;
    val ssc = new StreamingContext(sc, Seconds(5))  &lt;br /&gt;
    val stream = TwitterUtils.createStream(ssc, None)&lt;br /&gt;
&lt;br /&gt;
You can create an initialisation file, for example &amp;#039;&amp;#039;init-twitter.scala&amp;#039;&amp;#039;, as follows:&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&lt;br /&gt;
import org.apache.spark._&lt;br /&gt;
import org.apache.spark.streaming._&lt;br /&gt;
import org.apache.spark.streaming.twitter._&lt;br /&gt;
&lt;br /&gt;
// set Twitter oauth properties&lt;br /&gt;
System.setProperty(&amp;quot;twitter4j.oauth.consumerKey&amp;quot;, &amp;quot;2AvbG84msbL34BEHslMsWZTUR&amp;quot;)  &lt;br /&gt;
System.setProperty(&amp;quot;twitter4j.oauth.consumerSecret&amp;quot;, &amp;quot;gzEENqmZoMl2hhHvDIdaWzIF9ShMSLO0o7gh8csZnfqKdK6Y9H&amp;quot;)  &lt;br /&gt;
System.setProperty(&amp;quot;twitter4j.oauth.accessToken&amp;quot;, &amp;quot;14113114-1we8sJQs1z54dWfjWbUwZtDtkQYf3kDOrXLUMBFkZ&amp;quot;)  &lt;br /&gt;
System.setProperty(&amp;quot;twitter4j.oauth.accessTokenSecret&amp;quot;, &amp;quot;6Sq95ezVVRNTuLw7grKzm4czA32VqmlM0QwvaLjWLNl5A&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
// create stream&lt;br /&gt;
sc.setLogLevel(&amp;quot;ERROR&amp;quot;) &lt;br /&gt;
val ssc = new StreamingContext(sc, Seconds(5))  &lt;br /&gt;
val stream = TwitterUtils.createStream(ssc, None)&lt;br /&gt;
&lt;br /&gt;
// define transformations&lt;br /&gt;
val hashTags = stream.flatMap(status =&amp;gt; status.getText.split(&amp;quot; &amp;quot;).filter(_.startsWith(&amp;quot;#&amp;quot;)))&lt;br /&gt;
hashTags.foreachRDD(rdd =&amp;gt; rdd.foreach(str =&amp;gt; println(&amp;quot;:: &amp;quot; + str)))&lt;br /&gt;
&lt;br /&gt;
// start&lt;br /&gt;
ssc.start&lt;br /&gt;
&lt;br /&gt;
// to stop, use: ssc.stop(false, true)&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start &amp;#039;&amp;#039;spark-shell&amp;#039;&amp;#039; with an initialisation file as follows:&lt;br /&gt;
    spark-shell -i init-twitter.scala&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Todo:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
# partition by hashtag, save message text, save bags of words&lt;br /&gt;
# explore basic sentiment analysis&lt;/div&gt;</summary>
		<author><name>Vimala</name></author>
	</entry>
</feed>