Build Twitter Scala API Library for Spark Streaming using sbt
If you need to import Twitter API library in Scala:
1
import org.apache.spark.streaming.twitter._
Copied!
You will need to build the jar file and place the jar file in the classpath. In our case, default classpath is $SPARK_HOME/jars
To start with, know your Spark and Scala version, which can be found by running $SPARK_HOME/bin/spark-shell
1
$SPARK_HOME/bin/spark-shell
2
Setting default log level to "WARN".
3
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
4
20/06/12 14:06:22 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
5
Spark context Web UI available at http://10.0.2.15:4040
6
Spark context available as 'sc' (master = local[*], app id = local-1591995983793).
7
Spark session available as 'spark'.
8
Welcome to
9
____ __
10
/ __/__ ___ _____/ /__
11
_\ \/ _ \/ _ `/ __/ '_/
12
/___/ .__/\_,_/_/ /_/\_\ version 2.2.1
13
/_/
14
​
15
Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_171)
16
Type in expressions to have them evaluated.
17
Type :help for more information.
18
​
Copied!
In this example, Spark version is 2.2.1, Scala version is 2.11.8
To build the "twitter jar" file, you need to manually create directory structure, let's call twitter for example, that contains subfolder project and src. Subfolder names are significant. The directory structure is like below:
1
(base) [[email protected] twitter]$ tree -a
2
.
3
β”œβ”€β”€ build.sbt
4
β”œβ”€β”€ project
5
β”‚Β Β  β”œβ”€β”€ assembly.sbt
6
β”‚Β Β  └── build.properties
7
└── src
8
└── main
9
└── scala
10
​
Copied!
Install tree command on Linux, here is CentOS, do below
1
sudo yum install tree -y
Copied!
test tree command to see if it works
1
tree -d
Copied!
There are 1 files from the root directory called build.sbt, you need to create:
vi build.sbt
1
// this file was written for spark 3.0.0-preview and scala 2.12.10
2
// George Jen
3
// Jen Tek LLC
4
​
5
version := "1"
6
name := "JentekLLC-spark-streaming-from-Twitter"
7
scalaVersion := "2.11.8"
8
​
9
libraryDependencies += "org.apache.spark" %% "spark-core" % "2.2.1" % "provided"
10
libraryDependencies += "org.apache.spark" %% "spark-streaming" % "2.2.1" % "provided"
11
libraryDependencies += "org.apache.spark" %% "spark-sql" % "2.2.1" % "provided"
12
libraryDependencies += "org.twitter4j" % "twitter4j-core" % "4.0.4"
13
libraryDependencies += "org.twitter4j" % "twitter4j-stream" % "4.0.4"
14
libraryDependencies += "org.apache.bahir" %% "spark-streaming-twitter" % "2.2.1"
15
​
16
assemblyMergeStrategy in assembly := {
17
case PathList("org","aopalliance", xs @ _*) => MergeStrategy.last
18
case PathList("javax", "inject", xs @ _*) => MergeStrategy.last
19
case PathList("javax", "servlet", xs @ _*) => MergeStrategy.last
20
case PathList("javax", "activation", xs @ _*) => MergeStrategy.last
21
case PathList("org", "apache", xs @ _*) => MergeStrategy.last
22
case PathList("com", "google", xs @ _*) => MergeStrategy.last
23
case PathList("com", "esotericsoftware", xs @ _*) => MergeStrategy.last
24
case PathList("com", "codahale", xs @ _*) => MergeStrategy.last
25
case PathList("com", "yammer", xs @ _*) => MergeStrategy.last
26
case "about.html" => MergeStrategy.rename
27
case "META-INF/ECLIPSEF.RSA" => MergeStrategy.last
28
case "META-INF/mailcap" => MergeStrategy.last
29
case "META-INF/mimetypes.default" => MergeStrategy.last
30
case "plugin.properties" => MergeStrategy.last
31
case "log4j.properties" => MergeStrategy.last
32
case x =>
33
val oldStrategy = (assemblyMergeStrategy in assembly).value
34
oldStrategy(x)
35
}
36
​
Copied!
You need to set your Scala version, in my example, 2.11.8. Your one might be different.
1
​
2
scalaVersion := "2.11.8"
Copied!
Then fill in your Spark version in these lines, mine is 2.2.1
1
libraryDependencies += "org.apache.spark" %% "spark-core" % "2.2.1" % "provided"
2
​
3
libraryDependencies += "org.apache.spark" %% "spark-streaming" % "2.2.1" % "provided"
4
​
5
libraryDependencies += "org.apache.spark" %% "spark-sql" % "2.2.1" % "provided"
6
​
7
libraryDependencies += "org.apache.bahir" %% "spark-streaming-twitter" % "2.2.1"
Copied!
Also change the line spark-streaming-twitter to your Spark version, mine is 2.2.1
1
libraryDependencies += "org.apache.bahir" %% "spark-streaming-twitter" % "2.2.1"
Copied!
Save and exit
Inside sub-folder project, there are 2 files: assembly.sbt and build.properties, you need to create both:
vi project/assembly.sbt
1
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
Copied!
Save and exit
vi project/build.properties
1
sbt.version=1.3.6
Copied!
Save and exit
You do not need place any source code file in src/main/scala folder in this excercise. We only need to download needed twitter jar files from Maven repository and assemble into one single combined jar file by sbt.
If you do not have sbt installed, you can get it at
​
Download the zip file, sbt-1.3.10.zip (note, the file name may be changing over the time). On linux, do below to download and install sbt:
1
mkdir ~/sbt
2
cd ~/sbt
3
wget https://piccolo.link/sbt-1.3.10.zip
4
unzip sbt-1.3.10.zip
5
sudo cp sbt/bin/* /usr/local/bin
Copied!
Now, you should be able to build Twitter jar file using sbt
1
cd ~/twitter
2
#Test below to see you have the right
3
#directory structure with right files
4
​
5
(base) [[email protected] twitter]$ tree -a
6
.
7
β”œβ”€β”€ build.sbt
8
β”œβ”€β”€ project
9
β”‚Β Β  β”œβ”€β”€ assembly.sbt
10
β”‚Β Β  └── build.properties
11
└── src
12
└── main
13
└── scala
14
​
15
#Now you run sbt at the root of the directory
16
#that has the build.sbt file
17
​
Copied!
You must run the sbt command below at the root level of the project folder, that has the file build.sbt
1
sbt assembly
Copied!
The first time, it will takes a while and download lots of files, eventually, it will show below:
1
(base) [[email protected] twitter]$ sbt assembly
2
[info] Loading settings for project twitter-build from assembly.sbt ...
3
[info] Loading project definition from /opt/hadoop/sbt/twitter/project
4
[info] Loading settings for project twitter from build.sbt ...
5
[info] Set current project to JentekLLC-spark-streaming-from-Twitter (in build file:/opt/hadoop/sbt/twitter/)
6
[info] Strategy 'discard' was applied to 12 files (Run the task at debug level to see details)
7
[info] Strategy 'last' was applied to a file (Run the task at debug level to see details)
8
[info] Strategy 'rename' was applied to 3 files (Run the task at debug level to see details)
9
[success] Total time: 8 s, completed Jun 12, 2020 3:47:03 PM
10
​
Copied!
It produces a new directory called target, with subdirectory called scala-<version>, simply cd into it
1
(base) [[email protected] twitter]$ ls
2
build.sbt project src target
3
(base) [[email protected] twitter]$ cd target
4
(base) [[email protected] target]$ ls
5
scala-2.11 streams
6
(base) [[email protected] target]$ cd scala*
7
(base) [[email protected] scala-2.11]$ pwd
8
/opt/hadoop/sbt/twitter/target/scala-2.11
9
​
Copied!
Now you should see twitter jar file produced
1
(base) [[email protected] scala-2.11]$ ls
2
JentekLLC-spark-streaming-from-Twitter-assembly-1.jar update
3
​
Copied!
Then copy the jar file into $SPARK_HOME/jars/, which is the default classpath of Java library
1
cp JentekLLC-spark-streaming-from-Twitter-assembly-1.jar $SPARK_HOME/jars/
Copied!
Then verify the file JentekLLC-spark-streaming-from-Twitter-assembly-1.jar in $SPARK_HOME/jars/
1
ls $SPARK_HOME/jars/Jentek*
2
/opt/spark/jars/JentekLLC-spark-streaming-from-Twitter-assembly-1.jar
3
​
Copied!
Since we are going to load Twitter Tweets into Hive table, set the permission of HDFS /tmp/hive by:
1
hdfs dfs -chmod -R 777 /tmp/hive
2
hdfs dfs -ls /tmp/
3
drwx------ - hadoop supergroup 0 2020-05-09 14:16 /tmp/hadoop-yarn
4
drwxrwxrwx - hadoop supergroup 0 2018-05-05 22:00 /tmp/hive
5
​
Copied!
Assuming you have already started up Hadoop, Hive, Spark, and Jupyter-Notebook server in the virtualenv spark by conda activate spark. If not, visit the relevant session of this eBook if needing direction. Then launch your web browser, connect to Jupyter notebook server, start a new notebook in Scala Spylon kernel and run below code
You need to replace with your own Twitter consumer key, consumer secret, access token, access token secret. Additionally, it is likely you might need to replace the IP address of the machine that runs HIVE.
Note:
The notebook runs fines with Jupyter-notebook (server) runs on Linux, but however, on Mac, when creating a table on HIVE, following error will result, despite hive support has been enabled.
1
val spark = SparkSession.builder().config("spark.master", "local[2]").appName("interfacing spark sql to hive metastore through thrift url below").config("hive.metastore.uris", "thrift://localhost:9083").enableHiveSupport().getOrCreate()
Copied!
But when running below create table statement
1
spark.sql("CREATE TABLE IF NOT EXISTS tweets (datetime STRING, text STRING) USING hive")
2
​
3
org.apache.spark.sql.AnalysisException: Hive support is required to CREATE Hive TABLE (AS SELECT);;
4
'CreateTable `tweets`, Ignore
5
at org.apache.spark.sql.execution.datasources.HiveOnlyCheck$.$anonfun$apply$4(rules.scala:446)
6
at org.apache.spark.sql.execution.datasources.HiveOnlyCheck$.$anonfun$apply$4$adapted(rules.scala:444)
7
at org.apache.spark.sql.catalyst.trees.TreeNode.foreach(TreeNode.scala:144)
8
at org.apache.spark.sql.execution.datasources.HiveOnlyCheck$.apply(rules.scala:444)
9
at org.apache.spark.sql.execution.datasources.HiveOnlyCheck$.apply(rules.scala:442)
10
at org.apache.spark.sql.catalyst.analysis.CheckAnalysis.$anonfun$checkAnalysis$38(CheckAnalysis.scala:574)
11
at org.apache.spark.sql.catalyst.analysis.CheckAnalysis.$anonfun$checkAnalysis$38$adapted(CheckAnalysis.scala:574)
Copied!
This is likely related to Jupyter notebook Spylon Kernel on MacOS only, because the same code runs fines on spark-shell on the same Mac without problem
1
scala> val spark = SparkSession.builder().config("spark.master", "local[2]").appName("interfacing spark sql to hive metastore through thrift url below").config("hive.metastore.uris", "thrift://localhost:9083").enableHiveSupport().getOrCreate()
2
spark: org.apache.spark.sql.SparkSession = [email protected]
3
​
4
scala> import spark.implicits._
5
import spark.implicits._
6
​
7
scala> spark.sql("CREATE TABLE IF NOT EXISTS tweets (datetime STRING, text STRING) USING hive")
8
res19: org.apache.spark.sql.DataFrame = []
9
​
Copied!
​
If you need direction, view my video presentation below in its entirety:
​
Last modified 1yr ago
Copy link