gpt4 book ai didi

scala - 从 spark-submit 运行应用程序时从 jar 读取输入文件

转载 作者:行者123 更新时间:2023-12-02 03:03:55 25 4
gpt4 key购买 nike

我有一个自定义分隔的输入文件,它被传递给 newAPIHadoopFile 以转换为 RDD[String]。该文件位于项目资源目录下。从 Eclipse IDE 运行时,以下代码运行良好。

  val path = this.getClass()
.getClassLoader()
.getResource(fileName)
.toURI().toString()
val conf = new org.apache.hadoop.conf.Configuration()
conf.set("textinputformat.record.delimiter", recordDelimiter)

return sc.newAPIHadoopFile(
path,
classOf[org.apache.hadoop.mapreduce.lib.input.TextInputFormat],
classOf[org.apache.hadoop.io.LongWritable],
classOf[org.apache.hadoop.io.Text],
conf)
.map(_._2.toString)

但是,当我按如下方式在 spark-submit(使用 uber jar)上运行它时

   spark-submit /Users/anon/Documents/myUber.jar

我收到以下错误。

 Exception in thread "main" java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: jar:file:/Users/anon/Documents/myUber.jar!/myhome-data.json

有什么意见吗?

最佳答案

如果文件是用于需要路径而不是输入流的 sc.newAPIHadoopFile,我建议使用 spark-submit 的 --files 选项

--files FILES Comma-separated list of files to be placed in the working directory of each executor. File paths of these files in executors can be accessed via SparkFiles.get(fileName).

参见 SparkFiles.get方法:

Get the absolute path of a file added through SparkContext.addFile().

这样,您应该按如下方式使用 spark-submit:

spark-submit --files fileNameHere /Users/anon/Documents/myUber.jar

一般情况下,如果文件在 jar 文件中,您应该使用 InputStream 访问该文件(而不是直接作为 File)。

代码可能如下所示:

val content = scala.io.Source.fromInputStream(
classOf[yourObject].getClassLoader.getResourceAsStream(yourFileNameHere)

参见 Scala 的 Source对象和 Java 的 ClassLoader.getResourceAsStream方法。

关于scala - 从 spark-submit 运行应用程序时从 jar 读取输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44004414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com