获取指定路径所有文件
public static void listStatus() throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
FileStatus stats[]=hdfs.listStatus(new Path("/"));
for(int i = 0; i < stats.length; ++i)
System.out.println(stats[i].getPath().toString());
hdfs.close();
}
重命名
public static void rename() throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
Path frpaht=new Path("/aaa");
Path topath=new Path("/aaaaaaa");
boolean isRename=hdfs.rename(frpaht, topath);
String result=isRename?"修改成功!":"修改失败!";
System.out.println(result);
}
获取文件日期
public static void GetTime() throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
FileStatus fileStatus=hdfs.getFileStatus(new Path("/yarn-daemons.txt"));
long modiTime=fileStatus.getModificationTime();
System.out.println(modiTime);
}
创建文件夹
public static void mkdir () throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
boolean bool2=hdfs.mkdirs(new Path("/user/new"));
if (bool2){
System.out.println("创建成功!!");
}else{
System.out.println("创建失败!!");
}
}
删除文件
public static void deletefile() throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
boolean isDeleted=hdfs.delete(new Path("/user/new"),true);
System.out.println("Delete?"+isDeleted);
}
创建数据
public static void AddFile() throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
byte[] buff="hello hadoop world!\r\n hadoop ".getBytes();
FSDataOutputStream outputStream=hdfs.create(new Path("/tmp/file.txt"));
outputStream.write(buff,0,buff.length);
outputStream.close();
}
上传数据
public static void put() throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
Path src =new Path("C:/123.py");
Path dst =new Path("/");
hdfs.copyFromLocalFile(src, dst);
}
检查目录是否存在
public static void check() throws Exception {
Configuration conf=new Configuration();
FileSystem hdfs=FileSystem.get(new URI("hdfs://192.168.100.201:8020"),conf);
Path findf=new Path("/abc");
boolean isExists=hdfs.exists(findf);
System.out.println("Exist?"+isExists);
}