使用其他文件

时间:2015-05-05 08:24:44

标签: apache-spark hdfs yarn

我正在编写一个简单的spark应用程序,它使用一些输入RDD,通过管道将它发送到外部脚本,并将该脚本的输出写入文件。驱动程序代码如下所示:

val input = args(0)
val scriptPath = args(1)
val output = args(2)
val sc = getSparkContext
if (args.length == 4) {
  //Here I pass an additional argument which contains an absolute path to a script on my local machine, only for local testing
  sc.addFile(args(3))
}

sc.textFile(input).pipe(Seq("python2", SparkFiles.get(scriptPath))).saveAsTextFile(output)

当我在我的本地机器上运行时,它工作正常。但是当我通过

将它提交给YARN集群时
spark-submit --master yarn --deploy-mode cluster --files /absolute/path/to/local/test.py --class somepackage.PythonLauncher path/to/driver.jar path/to/input/part-* test.py path/to/output` 

失败,但有例外。

Lost task 1.0 in stage 0.0 (TID 1, rwds2.1dmp.ru): java.lang.Exception: Subprocess exited with status 2

我尝试过管道命令的不同变体。例如,.pipe("cat")工作正常,行为与预期一致,但.pipe(Seq("cat", scriptPath))也失败,错误代码为1,因此似乎spark无法找出群集节点上脚本的路径。 / p>

有什么建议吗?

3 个答案:

答案 0 :(得分:6)

我自己不使用python,但我发现一些线索可能对您有用(在Spark-1.3 SparkSubmitArguments的源代码中)

  • --py-files PY_FILES 逗号分隔的.zip,.egg或.py文件列表,放在PYTHONPATH for Python应用程序中。

  • --files FILES 以逗号分隔的文件列表,放在每个执行程序的工作目录中。

  • --archives ARCHIVES 以逗号分隔的档案列表,将其提取到每个执行人的工作目录中。

而且,你对spark-submit的论证应遵循这种风格:

Usage: spark-submit [options] <app jar | python file> [app arguments]

答案 1 :(得分:1)

您可能想尝试使用local://$SPARK_YARN_STAGING_DIR env var。

例如,以下内容应该有效:

spark-submit \
    --master yarn \
    --deploy-mode cluster \
    --files /absolute/path/to/local/test.py \
    --class somepackage.PythonLauncher \
    local://$SPARK_YARN_STAGING_DIR/test.py

答案 2 :(得分:1)

要了解原因,您必须熟悉spark的三种运行模式的区别,例如。独立的,纱线客户端,纱线群集。

与独立和yarn-client一样,驱动程序在您本地计算机的当前位置运行,而worker程序在其他地方运行(独立可能是$ SPARK_HOME下的另一个临时目录,yarn-client可能是集群中的随机节点),因此您可以使用驱动程序中指定的本地路径访问本地文件,而工作程序中不能指定本地路径。

但是,当您以yarn-cluster模式运行时,驱动程序和辅助程序都在随机群集节点上运行,本地文件是相对于其工作计算机和目录的,因此,需要抛出未找到文件的异常在提交时使用--files或--archive归档这些文件,或者在提交前自己将它们归档在.egg或.jar中,或者在驱动程序中使用addFile api,例如this