Multiples Hadoop FileSystem实例

Apa*_*hee 5 java hadoop

我有一个类(为了便于阅读,我删除了try/catch):

public class HadoopFileSystem {

    private FileSystem m_fileSystem = null;

    public HadoopFileSystem() {
        Configuration l_configuration = new Configuration();
        l_configuration .set("fs.default.name", "hdfs://localhost:9100");
        l_configuration .set("mapred.job.tracker", "localhost:9101");

        m_fileSystem = FileSystem.get(l_configuration );

    }

    public void close() {
        m_fileSystem.close();
    }

    public void createFile(String a_pathDFS) {
        m_fileSystem.create(new Path(a_pathDFS));
    }

}
Run Code Online (Sandbox Code Playgroud)

在我的程序中,我是第一个HadoopFileSysem对象,我不关闭它.

然后我创建了第二个HadoopFileSysem对象,然后关闭它.

最后,当我想m_fileSystem在我的第一个对象中使用函数时,我有错误:java.io.IOException: Filesystem closed

但我没有关闭它!

这里有一些代码来说明我的问题:

HadoopFileSystem h1 = new HadoopFileSystem();
HadoopFileSystem h2 = new HadoopFileSystem();

if(h1 == h2)
    System.out.println("=="); // No print
if(h1.equals(h2))
    System.out.println("equals"); // No print

h2.close();
h1.createFile("test.test"); // ERROR : java.io.IOException: Filesystem closed
h1.close();
Run Code Online (Sandbox Code Playgroud)

为什么?

SSa*_*ker 10

m_fileSystem = FileSystem.get(l_configuration );即使您创建了两个不同的对象,也是一个静态调用.您需要找到一种方法,不要让这个调用为两个不同的对象静态.

试试这个来解决问题,

conf.setBoolean("fs.hdfs.impl.disable.cache", true);
Run Code Online (Sandbox Code Playgroud)