1. 从官网下载Spark安装包,解压到自己的安装目录下; http://spark.apache.org/downloads.html
2. 进入系统命令行界面,进入安装目录下,如"/安装目录/spark-1.6.0-bin-hadoop-2.6.0 ",输入命令"./bin/pyspark"验证pyspark能否运行,再输入命令“./bin/spark-shell”,查看scala环境能否运行。成功的话会显示Spark图形,并能输入python或scala命令行。
3. 下载pycharm,点击完成安装。新建一个工程,打开Edit configuration,找到Environment variables,点击后面的编辑框,在变量栏目中添加PYTHONPATH,值为spark目录/python,SPARK_HOMR,值为spark安装目录,点ok退出。
4. 如果是用python的话要下载py4j包,用在命令行输入“easy_install py4j”命令就行。然后进入spark安装目录中的python文件夹下,打开lib文件夹,把里面的py4j压缩包复制到上一级python文件夹下,解压。
5. 在pycharm中写好demo,点击运行即可。demo示例如下:
"""SimpleApp.py"""from pyspark import SparkContext logFile = "/Spark/spark-1.6.0-bin-hadoop2.6/README.md" # Should be some file on your systemsc = SparkContext("local", "Simple App")logData = sc.textFile(logFile).cache() numAs = logData.filter(lambda s: ‘a‘ in s).count()numBs = logData.filter(lambda s: ‘b‘ in s).count() print("Lines with a: %i, lines with b: %i" % (numAs, numBs))
时间: 2024-11-02 07:08:54