解决hdfs报错:filesystem closed

FileSystem fileSystem = FileSystem.get(uri, new Configuration);
final FSDataInputStream in = fileSystem.open(path);
IOUtils.copyBytes(in, out, 1024, true);

程序中使用此方法进行获取文件系统,然后使用此方法   IOUtils.copyBytes(in, out, 1024, true);   进行流copy。结果系统在运行环境中大量报错fileSystem closed。 根据以上代码分析在  IOUtils.copyBytes(in, out, 1024, true); 时说明fileSystem已经在别处被关闭了。因此断定FileSystem fileSystem = FileSystem.get(uri, new Configuration); 中的fileSystem为单例,其他线程关闭此fileSystem时便会在此处报错。  FileSystem.get  方法源码查看:

从图中可以看出。在获取fileSystem时,会判断是否绕过缓存,默认时不绕过,会从缓存中获取fileSystem。因此上面猜想正确。

解决办法:


    public FileSystem createNewFileSystem() throws URISyntaxException, IOException
    {
        URI uri = new URI(hdfsConf.getUrl());
        String scheme = uri.getScheme();
        String disableCacheName = String.format("fs.%s.impl.disable.cache", scheme);

        Configuration configuration = new Configuration();
        configuration.setBoolean(disableCacheName, true);
        return FileSystem.get(uri, configuration);
    }

猜你喜欢

转载自blog.csdn.net/sinat_34704593/article/details/88236196