在使用append的操作时可能返回异常dfs.support.append未设置为true,只要才hdfs-site.xml中把该属性设置为true
<property> <name>dfs.support.append</name> <value>false</value> <description>Does HDFS allow appends to files? This is currently set to false because there are bugs in the "append code" and is not supported in any prodction cluster. </description> </property>而且0.20的早起版本也不支持append操作,官方文档的建议是如果追加数据就重新create一个文件。使用的时候应该谨慎一点。
当读取文件时若要返回文件的大小,用下列代码:
FileStatus status=hdfs.getFileStatus(p); return status.getLen(); 但是当对文件写入时,FileStatus的getLen函数返回的不是当前的大小,因为FSDataInputStream在写入数据时,数据会缓存,只有当写入的数据满64M(块大小)时或者关闭文件时,才会把数据写入到文件中去。连续写入数据时,为了实时获取当前文件大小,如果连续关闭打开文件势必会影响效率。可以这样解决: long result; if(outFsData!=null){//FSDataInputStream对象实例 result=writeInitSize+outFsData.size()//文件打开时的长度 } else{ FileStatus status=hdfs.getFileStatus(p); result=status.getLen();//这个getLen()返回的是int型,最大也就是4G,要注意 } return result;FSDataOutputStream该类的size函数就是返回的缓冲区写入的数据的大小。
参考:http://blog.csdn.net/qiuchenl/article/details/8617990
