Pet*_*ter 5 java apache-spark spark-streaming
我有一个Java应用程序,我作为Spark Streaming作业编写,需要一些文本资源,我已经包含在资源目录中的jar中(使用默认的Maven目录结构).使用单元测试我访问这些文件没有问题但是当我使用spark-submit运行我的程序时,我得到一个FileNotFoundException.使用spark-submit运行时,如何访问JAR中类路径上的文件?
我目前用来访问我的文件的代码大致如下:
InputStream input;
try {
URL url = this.getClass().getClassLoader().getResource("my file");
if (url == null) {
throw new IOException("file does not exist");
}
String path = url.getPath();
input = new FileInputStream(path);
} catch(IOException e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我通过以不同的(并且明显不那么愚蠢的)方式访问资源目录来解决这个问题:
input = MyClass.class.getResourceAsStream("/my file");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1736 次 |
| 最近记录: |