使用Hadoop Api操作hdfs时处理租约问题及异常处理

hadoop的FileSystem追加时异常

1.org.apache.hadoop.hdfs.protocol.RecoveryInProgressException

2.org.apache.hadoop.hdfs.protocol.RecoveryInProgressException

配置

        Configuration conf = new Configuration();
//        conf.setJobName(System.currentTimeMillis() + "");
        conf.setBoolean("dfs.support.append", true);
        conf.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");    //修改属性参数
        conf.setInt("dfs.replication", 1);
        conf.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");

 网上各种说法,基本都在上面这个配置,然并卵,如果你是同一个服务在本地测完,然后发到线上,很容易出现上面的异常,因为租约没有失效!

解决方法:

  /**
     * 创建文件,并且可以将制定内容写到文件中,如果为null则创建一个空的文件
     *
     * @param hdfsPath
     * @param content
     */
    public synchronized boolean updateFile(String hdfsPath, String content) throws Exception {
        Path path = null;
        try {
            logger.info(hdfsPath + "---content size:" + content.length());
            path = new Path(hdfsPath);
//            fs = FileSystem.get(URI.create(hdfsPath), conf);
            if (fs.exists(path)) {
                out = fs.append(path);
                out.write(content.getBytes());
            } else {
                // 创建一个空文件
                if (content.equals("") || content == null) {
                    out = fs.create(path);
                    System.out.println("创建了一个空文件!");
                } else {
                    // 创建一个文件,然后将内容写入进去
                    out = fs.create(path);
                    out.write(content.getBytes());
                    System.out.println("文件创建成功,内容已写入!");
                }
            }
            return true;
        } catch (Exception e) {
            if (path != null) {
                ((DistributedFileSystem) fs).recoverLease(path);
            }
            close();
            throw e;
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }


    public void close() throws IOException {
        appendContent = null;
    }

你没有看错,就是在追加抛异常的时候,将FileSystem强制转换成DistributedFileSystem然后将路径recoverLease,如果你想重新跑这次追加就在调一次就行了,我是直接让他抛异常,人工重启任务就不会出现异常了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值