Py4JJavaError:调用z:org.apache.spark.api.python.PythonRDD.runJob

时间:2019-03-05 05:26:14

标签: python-3.x java-8 pyspark

我在运行Python 3.6.5的Jupyter笔记本和运行3.7.2的Python Shell中遇到此错误。我的操作系统是Windows10。我在这两种环境中都安装了pippark pyspark。两者都使用Spark版本2.4.0,而我的Java JDK是Oracle JDK版本8,jdk1.8.0_201。这是我在两种情况下都运行的代码:

>>> from pyspark import SparkConf, SparkContext
>>> conf = SparkConf().setAppName("app")
>>> sc = SparkContext(conf=conf)
>>> import os

>>> os.chdir("C:/Users/theca/Desktop/school_folders/Big Data")
>>> data = sc.textFile("post_codes.txt")
>>> data.take(1)

我使用的是JRE版本8,我验证了JAVA_HOME:

C:\Python\Python37\Scripts>echo %JAVA_HOME%

C:\ProgramData\Oracle\Java\javapath\java.exe

我改用JDK认为可以解决此问题:

C:\Program Files\Java\jdk1.8.0_201>setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0_201"

SUCCESS: Specified value was saved.

C:\Program Files\Java\jdk1.8.0_201>setx PATH "%PATH%;%JAVA_HOME%\bin";

WARNING: The data being saved is truncated to 1024 characters.

我退出了cmd,然后返回,验证了我的Java主页:

C:\WINDOWS\system32>echo %JAVA_HOME% C:\Program Files\Java\jdk1.8.0_201

我在这里尝试过解决方案: PySpark exception: Java gateway process exited before sending its port number

在这里: Pyspark: SparkContext definition in Spyder throws Java gateway error

以及本板上的其他一些答案。我想知道是否需要使用早期版本的spark?  这是整个错误消息:

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    data.take(1)
  File "C:\Python\Python37\lib\site-packages\pyspark\rdd.py", line 1327, in take
    totalParts = self.getNumPartitions()
  File "C:\Python\Python37\lib\site-packages\pyspark\rdd.py", line 391, in getNumPartitions
    return self._jrdd.partitions().size()
  File "C:\Python\Python37\lib\site-packages\py4j\java_gateway.py", line 1257, in __call__
    answer, self.gateway_client, self.target_id, self.name)
  File "C:\Python\Python37\lib\site-packages\py4j\protocol.py", line 328, in get_return_value
    format(target_id, ".", name), value)
py4j.protocol.Py4JJavaError: An error occurred while calling o20.partitions.
: org.apache.hadoop.mapred.InvalidInputException: Input path does not exist: file:/C:/Python/Python37/post_codes.txt

at org.apache.hadoop.mapred.FileInputFormat.singleThreadedListStatus(FileInputFormat.java:287)

at org.apache.hadoop.mapred.FileInputFormat.listStatus(FileInputFormat.java:229)

at org.apache.hadoop.mapred.FileInputFormat.getSplits(FileInputFormat.java:315)

at org.apache.spark.rdd.HadoopRDD.getPartitions(HadoopRDD.scala:204)

at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:253)

at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:251)

at scala.Option.getOrElse(Option.scala:121)

at org.apache.spark.rdd.RDD.partitions(RDD.scala:251)

at org.apache.spark.rdd.MapPartitionsRDD.getPartitions(MapPartitionsRDD.scala:49)

at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:253)

at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:251)

at scala.Option.getOrElse(Option.scala:121)

at org.apache.spark.rdd.RDD.partitions(RDD.scala:251)

at org.apache.spark.api.java.JavaRDDLike$class.partitions(JavaRDDLike.scala:61)

at org.apache.spark.api.java.AbstractJavaRDDLike.partitions(JavaRDDLike.scala:45)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

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:238)

at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

data = sc.textFile("file:///path to the file/")

这应该有效。