PyCharm 远程连接linux中Python 运行pyspark

PySpark in PyCharm on a remote server

1、确保remote端Python、spark安装正确

2、remote端安装、设置

vi /etc/profile
添加一行:PYTHONPATH=SPARKHOME/python/:

SPARK_HOME/python/lib/py4j-0.8.2.1-src.zip
source /etc/profile

 # 安装pip 和 py4j

下载pip-7.1.2.tar
tar -xvf pip-7.1.2.tar
cd pip-7.1.2
python setup.py install
pip install py4j

 # 避免ssh时tty检测

cd /etc
chmod 640 sudoers
vi /etc/sudoers
#Default requiretty

3、本地Pycharm设置

File > Settings > Project Interpreter:

PyCharm 远程连接linux中Python 运行pyspark_第1张图片

Project Interpreter > Add remote(前提:remote端python安装成功):

PyCharm 远程连接linux中Python 运行pyspark_第2张图片

注意,这里的Python路径为python interpreter path,如果python安装在其它路径,要把路径改过来

Run > Edit Configuration (前提:虚拟机中共享本地目录成功):

PyCharm 远程连接linux中Python 运行pyspark_第3张图片

此处我配置映射是在Tools中进行的

Tools > Dployment > Configuration

PyCharm 远程连接linux中Python 运行pyspark_第4张图片

PyCharm 远程连接linux中Python 运行pyspark_第5张图片

PyCharm 远程连接linux中Python 运行pyspark_第6张图片

4、测试

复制代码
import os
import sys
os.environ['SPARK_HOME'] = '/root/spark-1.4.0-bin-hadoop2.6'
sys.path.append("/root/spark-1.4.0-bin-hadoop2.6/python")

try:
    from pyspark import SparkContext
    from pyspark import SparkConf

    print ("Successfully imported Spark Modules")

except ImportError as e:
    print ("Can not import Spark Modules", e)
    sys.exit(1)
复制代码
Result:

ssh://[email protected]:22/usr/bin/python -u /home/hadoop/TestFile/pysparkProgram/Mainprogram.py
Successfully imported Spark Modules

Process finished with exit code 0

或者:

复制代码
import sys
sys.path.append("/root/programs/spark-1.4.0-bin-hadoop2.6/python")

try:
    import numpy as np
    import scipy.sparse as sps
    from pyspark.mllib.linalg import Vectors

    dv1 = np.array([1.0, 0.0, 3.0])
    dv2 = [1.0, 0.0, 3.0]
    sv1 = Vectors.sparse(3, [0, 2], [1.0, 3.0])
    sv2 = sps.csc_matrix((np.array([1.0, 3.0]), np.array([0, 2]), np.array([0, 2])), shape=(3, 1))

    print(sv2)

except ImportError as e:
    print("Can not import Spark Modules", e)
    sys.exit(1)
复制代码
复制代码
Result

ssh://[email protected]:22/usr/bin/python -u /home/hadoop/TestFile/pysparkProgram/Mainprogram.py
  (0, 0)    1.0
  (2, 0)    3.0

Process finished with exit code 0
复制代码

参考:
https://edumine.wordpress.com/2015/08/14/pyspark-in-pycharm/
http://renien.github.io/blog/accessing-pyspark-pycharm/
http://www.tuicool.com/articles/MJnYJb

 参照:

http://blog.csdn.net/u011196209/article/details/9934721

你可能感兴趣的:(大数据语言-Python语言,大数据计算-Spark)