调用z:org.apache.spark.api.python.PythonRDD.collectAndServe时发生py4j.protocol.Py4JJavaError

时间:2018-04-27 14:32:36

标签: python-3.x apache-spark pyspark pycharm py4j

我在我的机器(Ubuntu)上安装了apache-spark和pyspark,在Pycharm中,我还更新了环境变量(例如spark_home,pyspark_python)。 我试图这样做:

import os, sys
os.environ['SPARK_HOME'] = ".../spark-2.3.0-bin-hadoop2.7"
sys.path.append(".../spark-2.3.0-bin-hadoop2.7/bin/pyspark/")
sys.path.append(".../spark-2.3.0-bin-hadoop2.7/python/lib/py4j-0.10.6-src.zip")
from pyspark import SparkContext
from pyspark import SparkConf
sc = SparkContext('local[2]')
words = sc.parallelize(["scala", "java", "hadoop", "spark", "akka"])
print(words.count())

但是,我收到了一些奇怪的警告:

py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe.
: java.lang.IllegalArgumentException
at org.apache.xbean.asm5.ClassReader.<init>(Unknown Source)
at org.apache.xbean.asm5.ClassReader.<init>(Unknown Source)
at org.apache.xbean.asm5.ClassReader.<init>(Unknown Source)
at org.apache.spark.util.ClosureCleaner$.getClassReader(ClosureCleaner.scala:46)
at org.apache.spark.util.FieldAccessFinder$$anon$3$$anonfun$visitMethodInsn$2.apply(ClosureCleaner.scala:449)
at org.apache.spark.util.FieldAccessFinder$$anon$3$$anonfun$visitMethodInsn$2.apply(ClosureCleaner.scala:432)
at scala.collection.TraversableLike$WithFilter$$anonfun$foreach$1.apply(TraversableLike.scala:733)
at scala.collection.mutable.HashMap$$anon$1$$anonfun$foreach$2.apply(HashMap.scala:103)
at scala.collection.mutable.HashMap$$anon$1$$anonfun$foreach$2.apply(HashMap.scala:103)
at scala.collection.mutable.HashTable$class.foreachEntry(HashTable.scala:230)
at scala.collection.mutable.HashMap.foreachEntry(HashMap.scala:40)
at scala.collection.mutable.HashMap$$anon$1.foreach(HashMap.scala:103)
at scala.collection.TraversableLike$WithFilter.foreach(TraversableLike.scala:732)
at org.apache.spark.util.FieldAccessFinder$$anon$3.visitMethodInsn(ClosureCleaner.scala:432)
at org.apache.xbean.asm5.ClassReader.a(Unknown Source)
at org.apache.xbean.asm5.ClassReader.b(Unknown Source)
at org.apache.xbean.asm5.ClassReader.accept(Unknown Source)
at org.apache.xbean.asm5.ClassReader.accept(Unknown Source)
at org.apache.spark.util.ClosureCleaner$$anonfun$org$apache$spark$util$ClosureCleaner$$clean$14.apply(ClosureCleaner.scala:262)
at org.apache.spark.util.ClosureCleaner$$anonfun$org$apache$spark$util$ClosureCleaner$$clean$14.apply(ClosureCleaner.scala:261)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner.scala:261)
at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:159)
at org.apache.spark.SparkContext.clean(SparkContext.scala:2292)
at org.apache.spark.SparkContext.runJob(SparkContext.scala:2066)
at org.apache.spark.SparkContext.runJob(SparkContext.scala:2092)
at org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:939)
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
at org.apache.spark.rdd.RDD.withScope(RDD.scala:363)
at org.apache.spark.rdd.RDD.collect(RDD.scala:938)
at org.apache.spark.api.python.PythonRDD$.collectAndServe(PythonRDD.scala:153)
at org.apache.spark.api.python.PythonRDD.collectAndServe(PythonRDD.scala)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:282)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:214)
at java.base/java.lang.Thread.run(Thread.java:844)

我该如何解决这个问题?

9 个答案:

答案 0 :(得分:5)

实际上,我发现了一个棘手的解决方案。解决以下问题:

确保您正确安装了Py4j。最好使用正式版本安装它。要做,

  1. https://pypi.org/project/py4j/下载最新的官方发布。

  2. 解压缩/解压缩文件并导航到新创建的目录,例如cd py4j-0.x。

  3. 运行

    sudo python(3)setup.py install

  4. 然后将Java降级到版本8(之前我使用的是版本10)。 要做的是,首先使用以下命令删除当前版本的Java:

    sudo apt-get purge openjdk-\* icedtea-\* icedtea6-\*
    

    然后使用:

    安装Java 8
    sudo apt install openjdk-8-jre-headless 
    

    现在代码适合我。

答案 1 :(得分:2)

我还确认该解决方案可在Ubuntu 18.04 LTS上使用。

我安装了Java 10,并尝试从以下位置运行Python示例: http://spark.apache.org/docs/2.3.1/,例如:

./bin/spark-submit examples/src/main/python/pi.py 10

它没有用!

应用建议的修复程序之后:

sudo apt-get purge openjdk-\* icedtea-\* icedtea6-\*
sudo apt autoremove
sudo apt install openjdk-8-jre-headless

这个例子最终成功了;我的意思是,如果您认为正确的答案是:

  

Pi大约为3.142000

感谢您的解决方案,
巴吉安

答案 2 :(得分:1)

我以前有两个Java版本,java8和java9。当我删除Java9时,问题已解决。

答案 3 :(得分:1)

步骤1:

如果已经安装Java版本,则将其降级或升级到8。 (see how to alternate among java versions

第2步:

将以下内容添加到datetime.utcfromtimestamp(timestamp)

~/.bashrc

并运行export JAVA_HOME='/usr/lib/jvm/java-8-openjdk-amd64' export PATH=$JAVA_HOME/bin:$PATH export SPARK_HOME='/path/to/spark-2.x.x-bin-hadoop2.7' export PATH=$SPARK_HOME/bin:$PATH 进行加载,或者只是启动一个新终端。

另一种方法是将source ~/.bashrc复制到/path/to/spark-2.x.x-bin-hadoop2.7/conf/spark-env.sh.template。然后将以下内容添加到/path/to/spark-2.x.x-bin-hadoop2.7/conf/spark-env.sh

spark-env.sh

然后将以下内容添加到export JAVA_HOME='/usr/lib/jvm/java-8-openjdk-amd64' export PYSPARK_PYTHON=python3

~/.bashrc

并运行export SPARK_HOME='/path/to/spark-2.x.x-bin-hadoop2.7' export PATH=$SPARK_HOME/bin:$PATH export SPARK_CONF_DIR=$SPARK_HOME/conf

答案 4 :(得分:0)

我需要出于不同目的同时维护OpenJDK 11和JDK 8,因此降级不是一种选择。对于Spark程序,我通过导出(覆盖)指向JDK8的$this->validator = Validation::createValidator(); $this->validator->validate($input, $this->constraint, $groups); 路径来利用,如下所示。

JAVA_HOME

答案 5 :(得分:0)

direnv +采用openjdk8(brew tap homebrew/cask-versions + brew cask install adoptopenjdk8)在这种情况下(macOS)对我非常有用

# ~/.direnvrc
use_java() {
    if [ "$#" -ne 1 ]; then
    echo "usage: use java VERSION" >&2
    return 1
  fi
  local v
  v="$1"
  if [ "$v" -le "8" ]; then
    v="1.$v"
  fi
  export JAVA_HOME="$(/usr/libexec/java_home -v "$v")"
  PATH_add $JAVA_HOME/bin
}
# .envrc in the project directory
use_java 8

答案 6 :(得分:0)

如果您正在使用anaconda,请尝试: conda install -c cyclus java-jdk

答案 7 :(得分:0)

我遇到了同样的问题。我有java-11,所以我删除了Java-11并安装了java-8,问题已经解决了。

答案 8 :(得分:0)

这里出现错误的主要原因是环境变量中的路径不正确/不完整。您需要为 java、spark、pyspark_python、hadoop(包含 bin 文件夹)添加路径。很可能可以通过添加正确的路径来解决此解决方案。 https://youtu.be/WQErwxRTiW0 ---- 这个视频帮助我解决了我的问题(视频描述了所有安装和正确路径)