org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException

在Hadoop-1.0.4和Hadoop-2.2的使用append时,需求:追加写入文件,如果文件不存在,需求先创建。

异常:

Exception in thread "main" org.apache.hadoop.ipc.RemoteException: org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException: failed to create file /huangq/dailyRolling/mommy-dailyRolling for DFSClient_-1456545217 on client 10.1.85.243 because current leaseholder is trying to recreate file.
  at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.recoverLeaseInternal(FSNamesystem.java:1374)
  at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.startFileInternal(FSNamesystem.java:1246)
  at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFile(FSNamesystem.java:1426)
  at org.apache.hadoop.hdfs.server.namenode.NameNode.append(NameNode.java:643)
  at sun.reflect.GeneratedMethodAccessor25.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)

代码版本1:(出现上述报错的代码)

1 FileSystem fs = FileSystem.get(conf);
2 Path dstPath = new Path(dst);
3 if (!fs.exists(dstPath)) {
4     fs.create(dstPath);
5 } 
6 FSDataOutputStream fsout = fs.append(dstPath);
7 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fsout));

异常的原因:FSDataOutputStream create(Path f) 产生了一个输出流,创建完后需要关闭。

解决:创建完文件之后,关闭流FSDataOutputStream。

复制代码
FileSystem fs = FileSystem.get(conf);
Path dstPath = new Path(dst);
if (!fs.exists(dstPath)) {
    fs.create(dstPath).close();
} 
FSDataOutputStream fsout = fs.append(dstPath);
 
 
参考:http://www.cnblogs.com/byrhuangqiang/p/3926663.html
 

                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值