从Apache Spark Streaming上下文访问JAR中资源目录中的文件

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)

谢谢.

请注意,这不是从jar(已建议)中读取资源文件的重复,因为此代码在本地运行时有效.它仅在Spark群集中运行时失败.

Pet*_*ter 3

我通过以不同的(并且明显不那么愚蠢的)方式访问资源目录来解决这个问题:

input = MyClass.class.getResourceAsStream("/my file");
Run Code Online (Sandbox Code Playgroud)