解决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);
    }

 

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值