conf = sp">
当前位置:   article > 正文

Python写的Spark示例,报错与解决方法_file "e:\dev\pythontest\my_utils\py_spark.py", lin

file "e:\dev\pythontest\my_utils\py_spark.py", line 4, in conf = sp

 spark运行环境参考:https://blog.csdn.net/max_cola/article/details/78902597

对应的环境变量:

  1. #java
  2. export JAVA_HOME=/usr/local/jdk1.8.0_181
  3. export PATH=$JAVA_HOME/bin:$PATH
  4. #python
  5. export PYTHON_HOME=/usr/local/python3
  6. export PATH=$PYTHON_HOME/bin:$PATH
  7. #spark
  8. export SPARK_HOME=/usr/local/spark export PATH=$SPARK_HOME/bin:$PATH
  9. #add spark to python
  10. export PYTHONPATH=/usr/local/spark/python
  11. #add pyspark to jupyter
  12. export PYSPARK_PYTHON=/usr/local/python3/bin/python3 # 因为我们装了两个版本的python,所以要指定pyspark_python,>否则pyspark执行程序会报错。
  13. export PYSPARK_DRIVER_PYTHON=jupyter
  14. export PYSPARK_DRIVER_PYTHON_OPTS='notebook --allow-root'

使用 python写的Spark示例:

  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. from pyspark import *
  4. import os
  5. if __name__ == '__main__':
  6. sc = SparkContext("local[4]")
  7. sc.setLogLevel("WARN")
  8. rdd = sc.parallelize("hello Pyspark world".split(" "))
  9. counts = rdd \
  10. .flatMap(lambda line: line) \
  11. .map(lambda word: (word, 1)) \
  12. .reduceByKey(lambda a, b: a + b) \
  13. .foreach(print)
  14. sc.stop

出现如下错误

  1. Traceback (most recent call last):
  2. File "test1.py", line 3, in <module>
  3. from pyspark import *
  4. File "/usr/local/spark/python/pyspark/__init__.py", line 46, in <module>
  5. from pyspark.context import SparkContext
  6. File "/usr/local/spark/python/pyspark/context.py", line 29, in <module>
  7. from py4j.protocol import Py4JError
  8. ImportError: No module named py4j.protocol

解决方法:

  1. #进入python的目录
  2. /usr/local/python3/lib/python3.6/site-packages
  3. #拷贝日志包过来
  4. cp /usr/local/spark/python/lib/py4j-0.10.7-src.zip ./
  5. #解压
  6. unzip py4j-0.10.7-src.zip 


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/496288
推荐阅读
相关标签
  

闽ICP备14008679号