官术网_书友最值得收藏!

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 file log4j.properties.template and copy all its content into log4j.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
主站蜘蛛池模板: 旺苍县| 和硕县| 盐城市| 泗水县| 怀集县| 讷河市| 桐城市| 和政县| 柏乡县| 昭觉县| 西丰县| 山阴县| 青海省| 黔江区| 芜湖市| 定结县| 万安县| 五常市| 托里县| 大埔区| 金昌市| 通辽市| 阜南县| 呈贡县| 沙坪坝区| 洛阳市| 普格县| 鄂尔多斯市| 临夏市| 靖安县| 南木林县| 平南县| 长春市| 石渠县| 钟祥市| 南宁市| 盐源县| 类乌齐县| 山丹县| 邻水| 日喀则市|