获取文件系统句柄
public static FileSystem getFileSystem() throws Exception{
Configuration conf = new Configuration();
URI uri = new URI("hdfs://cdh2:9000"); //active,不能是standby
FileSystem hdfs = FileSystem.get(uri, conf);
return hdfs ;
}
创建文件目录
public static void mkdir() throws Exception{
FileSystem hdfs = getFileSystem();
hdfs.mkdirs(new Path("hdfs://cdh2:9000/test2"));
//active,不能是standby
hdfs.close();
}
上传文件
public static void copyToHDFS() throws Exception{
FileSystem hdfs = getFileSystem();
Path srcPath = new Path("D:\\data\\wc.txt");
Path descPath = new Path("hdfs://cdh2:9000/test2/");
hdfs.copyFromLocalFile(srcPath, descPath);
hdfs.close();
}
下载文件
public static void getFileFromHDFS() throws Exception{
FileSystem hdfs = getFileSystem();
Path srcPath = new Path("hdfs://cdh2:9000/test2/wc.txt");
Path descPath = new Path("D:\\data\\target");
hdfs.copyToLocalFile(srcPath, descPath);
hdfs.close();
}
目录浏览
public static void listAllFile() throws Exception{
FileSystem hdfs = getFileSystem();
FileStatus[] liststatus = hdfs.listStatus(new Path("hdfs://cdh2:9000/"));
for(FileStatus fs : liststatus){
System.out.println(fs);
}
Path[] path =FileUtil.stat2Paths(liststatus);
for(Path p : path){
System.out.println(p);
}
hdfs.close();
}
删除目录
public static void deleteDir() throws Exception{
FileSystem hdfs = getFileSystem();
hdfs.delete(new Path("hdfs://cdh2:9000/test2"), true);
hdfs.close();
}