py4j.protocol.Py4JJavaError occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe
Asked Answered
T

10

17

I installed apache-spark and pyspark on my machine (Ubuntu), and in Pycharm, I also updated the environment variables (e.g. spark_home, pyspark_python). I'm trying to do:

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())

But, I receive some weird warnings:

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)

How can I solve this problem?

Tot answered 27/4, 2018 at 14:32 Comment(0)
T
16

Actually, I found a tricky solution. To solve the following problem:

Be sure that you installed Py4j, correctly. It's better to install it by using an official release. To do,

  1. download the latest official release from from https://pypi.org/project/py4j/.

  2. untar/unzip the file and navigate to the newly created directory, e.g., cd py4j-0.x.

  3. run

    sudo python(3) setup.py install

Then downgrade your Java to version 8 (previously, I used version 10.). To do, first remove the current version of Java using:

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

and then Install Java 8 using:

sudo apt install openjdk-8-jre-headless 

Now the code works for me properly.

Tot answered 30/4, 2018 at 9:53 Comment(1)
I was having the same problem. I had installed the default-jre library for java which gave me a 10.0.0 version of java. I removed this and installed the openjdf-8-jre-headless library and got a 1.8.0 version for java. Now everything is working!Illimani
M
4

I also confirm that the solution works on Ubuntu 18.04 LTS.

I had a java 10 installed and tried to run the Python examples from: http://spark.apache.org/docs/2.3.1/, i.e. things as simple as:

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

It did not work!

After applying the suggested fix:

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

the example eventually worked; I mean if you consider that the right answer is:

Pi is roughly 3.142000

Thanks for the solution,
Bagvian

Mydriasis answered 31/10, 2018 at 11:12 Comment(0)
G
1

I had two versions of java before, java8 and java9. When I deleted Java9, the problem has been solved.

Getz answered 26/10, 2018 at 13:39 Comment(0)
R
1

Step 1:

Downgrade or upgrade your java version to 8, if you have already installed one. (see how to alternate among java versions)

Step 2:

Add the following to ~/.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

and run source ~/.bashrc to load it, or just start a new terminal.

An alternative approach would be to copy /path/to/spark-2.x.x-bin-hadoop2.7/conf/spark-env.sh.template to /path/to/spark-2.x.x-bin-hadoop2.7/conf/spark-env.sh. Then add the following to spark-env.sh

export JAVA_HOME='/usr/lib/jvm/java-8-openjdk-amd64'
export PYSPARK_PYTHON=python3

Then add the following to ~/.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

and run source ~/.bashrc.

Resinate answered 25/8, 2019 at 13:54 Comment(0)
S
1

I need to maintain both OpenJDK 11 and JDK 8 for different purposes, so downgrading is not an option. For Spark Programs, I leverage by exporting (overriding) JAVA_HOME path pointing to JDK8 as below.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/
Shushubert answered 2/11, 2019 at 16:42 Comment(0)
A
0

direnv + adoptopenjdk8 (brew tap homebrew/cask-versions + brew cask install adoptopenjdk8) worked great for me in this situation (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
Antecedence answered 15/1, 2020 at 7:45 Comment(0)
S
0

If you are using anaconda, try: conda install -c cyclus java-jdk

Stromboli answered 28/1, 2020 at 2:3 Comment(0)
A
0

I had same problem. I had java-11, so I deleted Java-11 and installed java-8, the problem has been solved.

Apiary answered 23/1, 2021 at 8:59 Comment(0)
B
0

The main here of getting the error is due to the incorrect/incomplete path in the environment variable. You need to add path for java, spark, pyspark_python, hadoop(containing the bin folder).Most probably this solution can be resolved by adding right paths. https://youtu.be/WQErwxRTiW0 ---- this video helped me in resolving my issue(video describes all the installation and correct paths)

Brenneman answered 13/4, 2021 at 20:47 Comment(0)
H
0

I have the same issue.
PySpark 2.x.x supports Java 8. And PySpark 3.x.x supports Java 8 and Java 11.
So, check your PySpark and Java version.
If you are using PySpark 2.x.x then you need to install/upgrade/downgrade Java 8 and point your JAVA_HOME to java 8 jdk path.

Halfback answered 6/9, 2021 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.