hdfs文件系统FileSystem.rename异常

org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.lang.ClassCastException: org.apache.hadoop.hdfs.server.namenode.INodeFileUnderConstruction cannot be cast to org.apache.hadoop.hdfs.server.namenode.INodeDirectory
at org.apache.hadoop.hdfs.server.namenode.FSDirectory.addChild(FSDirectory.java:1132)
at org.apache.hadoop.hdfs.server.namenode.FSDirectory.addChildNoQuotaCheck(FSDirectory.java:1152)
at org.apache.hadoop.hdfs.server.namenode.FSDirectory.unprotectedRenameTo(FSDirectory.java:457)
at org.apache.hadoop.hdfs.server.namenode.FSDirectory.renameTo(FSDirectory.java:375)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.renameToInternal(FSNamesystem.java:1999)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.renameTo(FSNamesystem.java:1969)
at org.apache.hadoop.hdfs.server.namenode.NameNode.rename(NameNode.java:847)
at sun.reflect.GeneratedMethodAccessor117.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:557)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1439)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1435)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1278)

at org.apache.hadoop.ipc.Server$Handler.run(Server.java:1433)

今天运行 一个FileSystem.rename的时候报错了。后来发现应该是文件的父目录不存在,创建好就ok了。具体原理还有待查看源码

关键代码

/**
	 * 删除目录或者文件到指定文件
	 * @param filePath
	 * @return
	 * @throws IOException
	 */
	public static boolean deleteFileToTrash(FileSystem fs,String filePath) throws IOException{
		
		if(!checkedUserAuthority(filePath)){
			//操作其他的用户下的文件属于非法操作
			return false;
		}
		Path dstPath=getDstPath(filePath);
		Path parentPath=dstPath.getParent();
		String fileName=dstPath.getName();
		//处理逻辑 如果不存在就创建,如果存在并且是非目录
		if (!fs.exists(parentPath)) {
			fs.mkdirs(parentPath);
		}else if (!fs.getFileStatus(parentPath).isDir()) {
			parentPath=handlerRepeatFileDirSameName(0, fs, parentPath);
			fs.mkdirs(parentPath);
		}
		dstPath=new Path(parentPath,fileName);
		if (fs.exists(dstPath)) {
			dstPath=handlerRepeatFile(0,fs,dstPath);
		}
		return fs.rename(new Path(filePath),dstPath);
	}


  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HDFS的编程接口(API)包括Java API和C API两种。其中,Java API是Hadoop官方提供的API,C API是Hadoop官方提供的C语言接口。 对于文件增、删、改、查相关的接口,Java API提供了以下接口: 1. 创建文件FileSystem.create(Path path, boolean overwrite) 2. 删除文件FileSystem.delete(Path path, boolean recursive) 3. 重命名文件FileSystem.rename(Path src, Path dst) 4. 判断文件是否存在:FileSystem.exists(Path path) 5. 获取文件信息:FileSystem.getFileStatus(Path path) 6. 获取文件列表:FileSystem.listFiles(Path path, boolean recursive) C API提供了以下接口: 1. 创建文件hdfsCreateFile(hdfsFS fs, const char* path, int flags, short replication, tSize blockSize) 2. 删除文件hdfsDelete(hdfsFS fs, const char* path, int recursive) 3. 重命名文件hdfsRename(hdfsFS fs, const char* oldPath, const char* newPath) 4. 判断文件是否存在:hdfsExists(hdfsFS fs, const char* path) 5. 获取文件信息:hdfsGetPathInfo(hdfsFS fs, const char* path) 6. 获取文件列表:hdfsListDirectory(hdfsFS fs, const char* path) 应用举例: 1. 创建文件FileSystem.create(new Path("/test.txt"), true) 2. 删除文件FileSystem.delete(new Path("/test.txt"), false) 3. 重命名文件FileSystem.rename(new Path("/test.txt"), new Path("/new_test.txt")) 4. 判断文件是否存在:FileSystem.exists(new Path("/test.txt")) 5. 获取文件信息:FileSystem.getFileStatus(new Path("/test.txt")) 6. 获取文件列表:FileSystem.listFiles(new Path("/"), true) 注意:以上代码仅供参考,实际使用时需要根据具体情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值