Spark支持Java、Python、Scala、R语法,可以通过spark-shell(Scala)或pyspark(Python)进行交互式的操作,也可以用spark-submit提交用Java、Python、Scala编写的应用。
为了学习Spark的使用,本文不涉及原理,仅仅介绍Spark中Python的基本使用,为之后学习Spark的使用打下基础。
本文使用部分全部来自官方参考文档Quick Start。
1. 使用IPYTHON启动pyspark
- 安装IPYTHON,如下:
sudo apt install ipython3
sudo apt install ipython3-notebook
- 设置环境变量,Spark1.x和Spark2.x有区别,如下:
IPYTHON and IPYTHON_OPTS are removed in Spark 2.0+.
Remove these and set PYSPARK_DRIVER_PYTHON and PYSPARK_DRIVER_PYTHON_OPTS instead.
在spark-env.sh
中添加内容如下:
#ipython for pyspark
export PYSPARK_DRIVER_PYTHON=ipython
export PYSPARK_DRIVER_PYTHON_OPTS="notebook"
- 再次启动pyspark,如下:
![](https://i-blog.csdnimg.cn/blog_migrate/ca1e84822c8b127236e0cddd5aedd211.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9db50591e1ba2b812291b402685fbe92.png)
2. 使用pyspark
2.1 用DataSet代替RDD
文档中有几句需要注意的话,如下:
Note that, before Spark 2.0, the main programming interface of Spark was the Resilient Distributed Dataset (RDD). After Spark 2.0, RDDs are replaced by Dataset,