如果我通过在控制台中输入/usr/bin/pyspark
来启动pyspark,下面的示例代码将运行而没有任何错误。但是,如果我在ippython中使用它,要么通过调用
$IPYTHON_OPTS="notebook" /usr/bin/pyspark # notebook
或by
$IPYTHON=1 /usr/bin/pyspark
则抛出异常。
这是代码:
from pyspark import SparkContext,SparkConf
from pyspark import SQLContext
from pyspark.sql.types import *
# sc is a SparkContex object created when pyspark is invoked
sqc = SQLContext(sc)
下面是错误信息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-f0bbbc9cdb50> in <module>()
3 from pyspark.sql.types import *
4 # sc is a SparkContex object created when pyspark is invoked
----> 5 sqc = SQLContext(sc)
/opt/cloudera/parcels/CDH-5.4.2-1.cdh5.4.2.p0.2/lib/spark/python/pyspark/sql/context.py in __init__(self, sparkContext, sqlContext)
91 """
92 self._sc = sparkContext
---> 93 self._jsc = self._sc._jsc
94 self._jvm = self._sc._jvm
95 self._scala_SQLContext = sqlContext
AttributeError: 'module' object has no attribute '_jsc'
是什么导致了这个错误,我该如何修复它?
如果我在Linux上使用Anaconda python发行版,那么问题就存在了:
~$ ipython --version
4.0.0
~$ python --version
Python 2.7.10 :: Anaconda 2.3.0 (64-bit)
但是,如果我禁用anaconda发行版并使用系统自带的Python,一切都会正常运行
$ ipython --version
4.0.0
$ python --version
Python 2.7.3
$ cat /etc/issue
Debian GNU/Linux 7 n l
问题出在Anaconda上,但我还是不知道问题出在哪里
不确定具体的错误,因为它应该对香草和蟒蛇火花有相同的问题,但是,您可以检查几件事:
确保驱动程序和工作程序都安装了相同的python版本。不同的版本可能会导致序列化问题。
一般不推荐使用IPYTHON_OPTS。我定义了以下环境变量:
# tells pyspark to use notebook
export PYSPARK_DRIVER_PYTHON_OPS="notebook"
# tells pyspark to use the jupyter executable instead of python. In your case you might want this to be ipython instead
export PYSPARK_DRIVER_PYTHON=/opt/anaconda2/bin/jupyter
# tells pyspark where the python executable is on the executors. It MUST be the same version of python (preferably with the same packages if you are using them in a UDF or similar
export PYSPARK_PYTHON=/opt/anaconda2/bin/python
当然,我看到你没有在命令行中添加一个master,所以如果你没有改变你的spark默认值(即没有worker),这将在本地运行spark。