import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.FileSystem;
import java.io.*;
public class MoveFile {
public static boolean mv(Configuration conf, String remoteFilePath,
String remoteToFilePath) {
try (FileSystem fs = FileSystem.get(conf)) {
Path srcPath = new Path(remoteFilePath);
Path dstPath = new Path(remoteToFilePath);
return fs.rename(srcPath, dstPath);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 主函数
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://master:9000");
String remoteFilePath = "/user/hadoop/myLocalFile.txt"; // 源文件HDFS路径
String remoteToFilePath = "/user/hadoop/dir"; // 目的HDFS路径
try {
if (MoveFile.mv(conf, remoteFilePath, remoteToFilePath)) {
System.out.println("将文件 " + remoteFilePath + " 移动到 "
+ remoteToFilePath);
} else {
System.out.println("操作失败(源文件不存在或移动失败)");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}