运行 PySpark 命令时出错

Posted

技术标签:

【中文标题】运行 PySpark 命令时出错【英文标题】:Error while running PySpark command 【发布时间】:2015-12-31 04:21:00 【问题描述】:

我在 Hadoop 2.6.0 中安装了 Spark 1.4.1,并尝试运行以下 PySpark 命令来计算行数。它抛出以下错误。我是 Spark 的新手,无法找到错误。

谁能提供解决方案。

>>> distFile = sc.textFile("/home/hduser2/spark-1.4.1-bin-hadoop2.6/README.md")
15/12/31 09:31:50 INFO storage.MemoryStore: ensureFreeSpace(213560) called with curMem=695185, maxMem=278019440
15/12/31 09:31:50 INFO storage.MemoryStore: Block broadcast_10 stored as values in memory (estimated size 208.6 KB, free 264.3 MB)
15/12/31 09:31:50 INFO storage.MemoryStore: ensureFreeSpace(19929) called with curMem=908745, maxMem=278019440
15/12/31 09:31:50 INFO storage.MemoryStore: Block broadcast_10_piece0 stored as bytes in memory (estimated size 19.5 KB, free 264.3 MB)
15/12/31 09:31:50 INFO storage.BlockManagerInfo: Added broadcast_10_piece0 in memory on localhost:60765 (size: 19.5 KB, free: 265.1 MB)
15/12/31 09:31:50 INFO spark.SparkContext: Created broadcast 10 from textFile at NativeMethodAccessorImpl.java:-2


>>> distFile.count()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/spark/python/pyspark/rdd.py", line 984, in count
    return self.mapPartitions(lambda i: [sum(1 for _ in i)]).sum()
  File "/usr/local/spark/python/pyspark/rdd.py", line 975, in sum
    return self.mapPartitions(lambda x: [sum(x)]).fold(0, operator.add)
  File "/usr/local/spark/python/pyspark/rdd.py", line 852, in fold
    vals = self.mapPartitions(func).collect()
  File "/usr/local/spark/python/pyspark/rdd.py", line 757, in collect
    port = self.ctx._jvm.PythonRDD.collectAndServe(self._jrdd.rdd())
  File "/usr/local/spark/python/lib/py4j-0.8.2.1-src.zip/py4j/java_gateway.py", line 538, in __call__
  File "/usr/local/spark/python/lib/py4j-0.8.2.1-src.zip/py4j/protocol.py", line 300, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe.
: org.apache.hadoop.mapred.InvalidInputException: Input path does not exist: hdfs://localhost:9000/home/hduser2/spark-1.4.1-bin-hadoop2.6/README.md
    at org.apache.hadoop.mapred.FileInputFormat.singleThreadedListStatus(FileInputFormat.java:285)
    at org.apache.hadoop.mapred.FileInputFormat.listStatus(FileInputFormat.java:228)
    at org.apache.hadoop.mapred.FileInputFormat.getSplits(FileInputFormat.java:313)
    at org.apache.spark.rdd.HadoopRDD.getPartitions(HadoopRDD.scala:207)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:219)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:217)
    at scala.Option.getOrElse(Option.scala:120)
    at org.apache.spark.rdd.RDD.partitions(RDD.scala:217)
    at org.apache.spark.rdd.MapPartitionsRDD.getPartitions(MapPartitionsRDD.scala:32)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:219)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:217)
    at scala.Option.getOrElse(Option.scala:120)
    at org.apache.spark.rdd.RDD.partitions(RDD.scala:217)
    at org.apache.spark.api.python.PythonRDD.getPartitions(PythonRDD.scala:58)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:219)
    at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:217)
    at scala.Option.getOrElse(Option.scala:120)
    at org.apache.spark.rdd.RDD.partitions(RDD.scala:217)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:1781)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:885)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:147)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:108)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:286)
    at org.apache.spark.rdd.RDD.collect(RDD.scala:884)
    at org.apache.spark.api.python.PythonRDD$.collectAndServe(PythonRDD.scala:378)
    at org.apache.spark.api.python.PythonRDD.collectAndServe(PythonRDD.scala)
    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:497)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:231)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:379)
    at py4j.Gateway.invoke(Gateway.java:259)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:207)
    at java.lang.Thread.run(Thread.java:745)

【问题讨论】:

您的文件是位于本地文件系统还是 hdfs 上? 感谢@cricket_007 提醒文件位置。现在我得到了预期的输出。 【参考方案1】:

您说该文件在您的本地文件系统中,但错误表明它正在寻找 HDFS 上的文件。

Input path does not exist: hdfs://localhost:9000/home/hduser2/spark-1.4.1-bin-hadoop2.6/README.md.

Spark 延迟执行,这意味着它不会真正读取文件,直到它需要,例如打电话给count()。这就解释了为什么上一行没有错误。

您可以将文件移动到 HDFS 中的该路径,或者在本地模式下设置 SparkContext。

【讨论】:

是的 @cricket_007 我将文件复制到 hdfs。现在它正在成功运行。如何在本地模式下设置 SparkContext。

以上是关于运行 PySpark 命令时出错的主要内容,如果未能解决你的问题,请参考以下文章

运行pyspark时出错

查找“pyspark.worker”的模块规范时出错(ModuleNotFoundError:没有名为“pyspark”的模块)

在 pyspark mlib 中运行逻辑回归时出错

运行 pyspark kafka steam 时出错

当我在 pyspark EMR 5.x 中运行用 Java 编写的 hive UDF 时出错

Apache Spark:启动 PySpark 时出错