简体   繁体   English

Multiples Hadoop FileSystem实例

[英]Multiples Hadoop FileSystem instances

I have a class (I removes try/catch for readability): 我有一个类(为了便于阅读,我删除了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));
    }

}

In my program, I a first HadoopFileSysem object, I don't close it . 在我的程序中,我是第一个HadoopFileSysem对象, 我不关闭它

Then I create a second HadoopFileSysem object, and I close it. 然后我创建了第二个HadoopFileSysem对象,然后关闭它。

Finally, when I want to use a function on m_fileSystem in my first object, I have the error : java.io.IOException: Filesystem closed 最后,当我想在我的第一个对象中使用m_fileSystem上的函数时,我有错误: java.io.IOException: Filesystem closed

But I did'nt close it ! 但我没有关闭它!

Here's a little code to illustrate my problem : 这里有一些代码来说明我的问题:

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();

Why ? 为什么?

m_fileSystem = FileSystem.get(l_configuration ); is a static call even if you have two different objects created. 即使您创建了两个不同的对象,也是一个静态调用。 You need to find a way to not make this call static for two different objects. 您需要找到一种方法, 不要让这个调用为两个不同的对象静态。

Try this out to do away with the problem, 试试这个来解决这个问题,

conf.setBoolean("fs.hdfs.impl.disable.cache", true);

You are creating an object based on a configuration which is hard-coded. 您正在基于硬编码的配置创建对象。 This basically means that you're creating 2 identical objects. 这基本上意味着您要创建2个相同的对象。 Because these objects are identical, the JVM will reference to the same object. 因为这些对象是相同的,所以JVM将引用同一个对象。 So h1 and h2 are referencing the same object. 所以h1和h2引用同一个对象。

The reason for this is because you're getting an existing instance of an object based on a configuration file. 原因是因为您正在根据配置文件获取对象的现有实例。 If the configuration is different for h1 and h2, it will not be the same instance anymore. 如果h1和h2的配置不同,则它将不再是同一个实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM