spark方面的书太少,而且中文版的几乎没有,且仅有的中文版都翻译的一塌糊涂,所以我这个英文很烂的人只能一边开着有道一边硬着头皮啃英文版的书,先入手一本《Learning Spark》,只是为了记录自己学习过程,有用的东西记下来方便以后查询。
第二章为下载spark和快速起步,download之后tar解压。spark提供两种交互式的shell:spark shell和PySpark shell,对应于两种语言scala和python,由于spark是scala写的,自己这两种语言都没学过,当然从scala开始。
spark解压后目录下运行:
bin/pyspark启动python shell
bin/spark-shell启动scala shell
启动过程会打印很多log,为了方便查看,把不必要的log去掉:
拷贝conf/log4j.properties.template为conf/log4j.properties,并找到 log4j.rootCategory=INFO, console改为log4j.rootCategory=WARN, console
scala shell的一个例子:
scala> val lines = sc.textFile("README.md")
//用文件README.md创建一个RDD lines(关于RDD后面章节会讲到,是spark比较重要的一个概念),这里就把README.md的每一行取出来作为一个数据集,在此数据集上做一些列的并行计算
scala> lines.count()
// 计算RDD lines的items,也就是README.md有几行,结果如下:
res0: Long = 127
scala> lines.first()
// first函数查看RDD的第一个item,对于此处就是文件README.md的第一行,结果如下:
res1: String = # Apache Spark
这就是一个简单的spark 处理过程,退出shell:Ctrl-D
上图是程序在集群上运行图,Diriver Program是spark 应用的一个重要部分,它包括你应用的main函数,并在集群上定义分布式数据集(RDD),在数据集上做一系列操作。通过SparkContext(上面程序的sc)对象来进行,连接到计算机集群上。通常部署在name node上,而并行的task分散到data node上来进行计算。得到sc之后就可以用它创建RDDs。要在lines上进行count操作,若README.md为很大的文件,且spark部署在hadoop yarn上,那么我们知道README.md会分成很多块分布到不同的data node上,Diriver Program来管理这些data node上的executor对自己的数据库分别进行计算。、
我们也可以在lines上进行如下等一些操作:
scala> val pythonLines = lines.filter(line => line.contains("Python"))//返回包含单词Python的RDD。(不懂的可自行去学习scala语言)
接下来是单机模式的wordcount程序,怎么编写及编译打包,提交运行,这里就不讲了,关注点在之后章节的集群模式。wordcount scala代码如下,很好理解:
// Create a Scala Spark Context.
val conf = new SparkConf().setAppName("wordCount")
val sc = new SparkContext(conf)
// Load our input data.
val input = sc.textFile(inputFile)
// Split it up into words.
val words = input.flatMap(line => line.split(" "))
// Transform into pairs and count.
val counts = words.map(word => (word, 1)).reduceByKey{case (x, y) => x + y}
// Save the word count back out to a text file, causing evaluation.
counts.saveAsTextFile(outputFile)
这一章看下来意识到自己的英文水平还是非常有待提高······能看懂,但翻译的一塌糊涂!语言组织能力太差!