Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎。Spark是UC Berkeley AMP lab (加州大学伯克利分校的AMP实验室)所开源的类Hadoop MapReduce的通用并行框架,Spark拥有Hadoop MapReduce所具有的优点;但不同MapReduce的是Job中间输出结果可以保存在内存中,从而不再需要读写HDFS,因此Spark能更好 适用于数据挖掘与机器学习等需要迭代的MapReduce的算法。
Spark提供了一个更快、更通用的数据处理平台。和Hadoop相比,Spark可以让你的程序在内存中运行时速度提升100倍,或者在磁盘上运行时速度提升10倍。去年,在100 TB Daytona GraySort比赛中,Spark战胜了Hadoop,它只使用了十分之一的机器,但运行速度提升了3倍。Spark也已经成为针对 PB 级别数据排序的最快的开源引擎。
Spark支持Scala、Java、Python、R等接口,本文均使用Python环境进行学习。
下载
如下图所示,选择最新版本的Spark,Pre-built版本下载之后可以直接运行,不需要我们再次编译。在不使用集群环境时,我们可以不安装Hadoop环境直接进行运行。选择完成后就可以点击选项4中的链接进行下载。
Spark下载
安装
1.下载的Spark直接解压就完成了安装,Windows 用户如果把Spark 安装到带有空格的路径下,可能会遇到一些问题。所以需要把Spark 安装到不带空格的路径下,比如C:\spark 这样的目录中。
2.Spark由于是使用Scala语言编写,因此需要安装对应版本的JDK包才能正常使用。JDK同样安装到不带空格的路径下。
3.Spark启动时需要根据HADOOP_HOME找到winutils.exe,因此需要下载对应版本的环境。
1.下载winutils的windows版本
由于我们的包是2.7版本,因此下载2.7版本的环境。
2.配置环境变量
增加用户变量HADOOP_HOME,指向文件解压的目录,然后在系统变量path里增加%HADOOP_HOME%\bin 即可。
4.将spark-python文件夹下的pyspark文件夹拷贝到python对应的文件夹中。或者执行以下命令安装:
pip install PySpark
Linux环境的配置类似即可。
测试
在conf 目录下复制log4j.properties.template为log4j.properties ,这个文件用来管理日志设置。接下来找到下面这一行:
log4j.rootCategory=INFO, console
然后通过下面的设定降低日志级别,只显示警告及更严重的信息:
log4j.rootCategory=WARN, console
在spark-bin文件夹下执行pyspark命令就可以进入pyspark-shell环境。也可以为Spark设置环境变量,同以上Hadoop环境的操作。
spark
在shell中执行下列代码测试,计算文件行数:
lines = sc.textFile("E:\Documents\Desktop\s.txt")
lines.count()
count
测试独立应用连接spark,将下列代码保存在demo.py,执行spark-submit demo.py,命令。
# coding:utf-8
from pyspark import SparkConf, SparkContext
if __name__ == '__main__':
# 连接集群local,本应用名称为Demo
conf = SparkConf().setMaster('local').setAppName('Demo')
sc = SparkContext(conf=conf)
# 统计文件中包含mape的行数,并打印第一行
lines = sc.textFile("E:\Documents\Desktop\s.txt")
plines = lines.filter(lambda lines: 'mape' in lines)
print(plines.count())
print(plines.first())
sc.stop
在Spark2.0中只要创建一个SparkSession就够了,SparkConf、SparkContext和SQLContext都已经被封装在SparkSession当中,因此代码也可以写成如下:
from pyspark.sql import SparkSession
# 连接集群local,本应用名称为Demo
sc = SparkSession.builder.master("local").appName(Demo").config("spark.some.config.option", "some-value").getOrCreate()
向spark提交的运行结果如图所示:
submit.jpg
异常处理
在windows下进行安装调试Spark时出现了多种异常状况,就我遇到的异常状况进行一下总结。
Hadoop winutils不存在异常
最开始没有配置winutils环境导致的异常,异常关键提示为:
Failed to locate the winutils binary in the hadoop binary path
F:\spark-2.2.0\bin>pyspark
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
17/10/25 16:13:59 ERROR Shell: Failed to locate the winutils binary in the hadoop binary path
java.io.IOException: Could not locate executable null\bin\winutils.e