2017-10-15 24 views
0

我有一个小星火计划使用Scala的,我希望它打包成一个可执行的脂肪罐子,在文件中设置的配置:src/main/resource/localconfig.properties,所以我在src/main/scala/com.let.App新的org.apache.hadoop.fs.Path(String pathString)一个实例:如何从jar中的属性构造HDFS的路径?

val Path = new Path("localconfig.properties") 

问题它可以在IDEA上运行,但在包装在jar中运行时出现故障,并以java -jar myapp.jar运行,提示:文件找不到。

我解压缩jar和属性文件在根文件夹中:myapp,我也试过Path("resources/localconfig.properties"),它也不起作用。

如何在可执行jar中为path设置正确的路径?

这是Windows环境,我读了它似乎与操作系统有关的Path()方法,但我仍然不知道如何使用构造函数。

+0

对象“fs.Path”与HDFS一起使用。在Windows环境下,HDFS和本地目录是相同的。但在Hadoop环境中,它们是不同的。猜测,配置文件可以读取为简单文件(而不是“fs.Path”)。或者配置文件可以放在HDFS上。 – pasha701

回答

0

引用Path(String pathString)

从字符串建立的路径。路径字符串是URI,但具有未转义的元素和一些额外的规范化。

这意味着pathString(例如,在你的例子localconfig.properties)应该是可用的文件系统,这是显然并非如此上。该文件位于jar文件中,因此您应该要求JVM为您提供该文件。

这就是您应该使用Hadoop HDFS的Path(URI aUri)和Java的ClassLoader.getResource方法的地方。

$ jar cvf /tmp/hello.jar hello.xml 

$ jar -tf /tmp/hello.jar 
META-INF/ 
META-INF/MANIFEST.MF 
hello.xml 

// start spark-shell with the jar on the CLASSPATH 
// that should be exactly your case where your jar is on CLASSPATH 
// with the file you want to use to construct Path inside 

$ ./bin/spark-shell --jars /tmp/hello.jar 

scala> this.getClass.getResource("/hello.xml") 
res0: java.net.URL = jar:file:/tmp/hello.jar!/hello.xml 

// Convert URL to URI that Path supports 
scala> this.getClass.getResource("/hello.xml").toURI 
res1: java.net.URI = jar:file:/tmp/hello.jar!/hello.xml 

scala> import org.apache.hadoop.fs.Path 
import org.apache.hadoop.fs.Path 

scala> new Path(this.getClass.getResource("/hello.xml").toURI) 
res3: org.apache.hadoop.fs.Path = jar: 

然而,我不知道是否构成Path这样的支持与否。