- Apache Spark Graph Processing
- Rindra Ramamonjison
- 627字
- 2021-07-16 20:03:53
Experimenting with the Spark shell
The best way to learn Spark is through the Spark shell. There are two different shells for Scala and Python. But since the GraphX library is the most complete in Scala at the time this book was written, we are going to use the spark-shell
, that is, the Scala shell. Let's launch the Spark shell inside the $SPARKHOME/bin
from the command line:
$SPARKHOME/bin/spark-shell
If you set the current directory (cd
) to $SPARKHOME
, you can simply launch the shell with:
cd $SPARKHOME ./bin/spark-shell
Note
If you happen to get an error saying something like: Failed to find Spark assembly in spark-1.4.1/assembly/target/scala-2.10. You need to build Spark before running this program
, then it means that you have downloaded the Spark source code instead of a prebuilt version of Spark. In that case, go back to the project website and choose a prebuilt version of Spark.
If you were successful in launching the Spark shell, you should see the welcome message like this:
Welcome to ____ __ / __/__ ___ _____/ /__ _\ \/ _ \/ _ '/ __/ '_/ /___/ .__/\_,_/_/ /_/\_\ version 1.4.1 /_/ Using Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java)
For a sanity check, you can type in some Scala expressions or declarations and have them evaluated. Let's type some commands into the shell now:
scala> sc res1: org.apache.spark.SparkContext = org.apache.spark.SparkContext@52e52233 scala> val myRDD = sc.parallelize(List(1,2,3,4,5)) myRDD: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at <console>:12 scala> sc.textFile("README.md").filter(line => line contains "Spark").count() res2: Long = 21
Here is what you can tell about the preceding code. First, we displayed the Spark context defined by the variable sc
, which is automatically created when you launch the Spark shell. The Spark context is the point of entry to the Spark API. Second, we created an RDD named myRDD
that was obtained by calling the parallelize
function for a list of five numbers. Finally, we loaded the README.md
file into an RDD, filtered the lines that contain the word "Spark"
, and finally invoked an action on the filtered RDD to count the number of those lines.
Note
It is expected that you are already familiar with the basic RDD transformations and actions, such as map, reduce, and filter. If that is not the case, I recommend that you learn them first, perhaps by reading the programming guide at https://spark.apache.org/docs/latest/programming-guide.html or an introductory book such as Fast Data Processing with Spark by Packt Publishing and Learning Spark by O'Reilly Media.
Don't panic if you did not fully grasp the mechanisms behind RDDs. The following refresher, however, helps you to remember the important points. RDD is the core data abstraction in Spark to represent a distributed collection of large datasets that can be partitioned and processed in parallel across a cluster of machines. The Spark API provides a uniform set of operations to transform and reduce the data within an RDD. On top of these abstractions and operations, the GraphX library also offers a flexible API that enables us to create graphs and operate on them easily.
Perhaps, when you ran the preceding commands in the Spark shell, you were overwhelmed by the long list of logging statements that start with INFO
. There is a way to reduce the amount of information that Spark outputs in the shell.
Tip
You can reduce the level of verbosity of the Spark shell as follows:
- First, go to the
$SCALAHOME/conf
folder - Then, create a new file called
log4j.properties
- Inside the
conf
folder, open the template filelog4j.properties.template
and copy all its content intolog4j.properties
- Find and replace the line
log4j.rootCategory=INFO, console
with either one of these two lines:log4j.rootCategory=WARN, console
log4j.rootCategory=ERROR, console
- Finally, restart the Spark shell and you should now see fewer logging messages in the shell outputs
- 大學計算機基礎(第二版)
- Learn Blockchain Programming with JavaScript
- GAE編程指南
- Redis Applied Design Patterns
- R的極客理想:工具篇
- Hands-On Functional Programming with TypeScript
- Node.js:來一打 C++ 擴展
- Getting Started with React Native
- C語言程序設計習題與實驗指導
- JavaScript程序設計:基礎·PHP·XML
- Visual Basic程序設計(第三版)
- INSTANT Apache Hive Essentials How-to
- Mastering PowerCLI
- 高質量程序設計指南:C++/C語言
- 產品架構評估原理與方法