packagehadoopDemo;importjava.io.IOException;importjava.net.URI;importjava.net.URISyntaxException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.FileSystem;importorg.apache.hadoop.fs.LocatedFileStatus;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.fs.RemoteIterator;public classAa {public static void main(String[] args) throwsException{
Aa a=newAa();
a.mv("e:/c.txt", "/aaa");
a.getAllFile();
a.delete("/aaa");
a.delete("/b.txt");
}public void upload(String src,String dst) throwsIOException, InterruptedException, URISyntaxException{//拿到一个文件系统与客户端一个实例
FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),new Configuration(),"root");
filesys.copyFromLocalFile(new Path(src), newPath(dst));
filesys.close();
}public void download(String src,String dst) throwsIOException, InterruptedException, URISyntaxException{
FileSystem fileSystem= FileSystem.get(new URI("hdfs://linux1:9000"), new Configuration(), "root");
fileSystem.copyToLocalFile(new Path(src), newPath(dst));
fileSystem.close();
}public void getAllFile() throwsIOException, InterruptedException, URISyntaxException{
Configuration cfg=newConfiguration();
FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");
RemoteIterator listFiles=filesys.listFiles(new Path("/"), true);while(listFiles.hasNext()){
LocatedFileStatus next2=listFiles.next();
String name=next2.getPath().getName();
System.out.println(next2.getPath()+" "+name);
}
}public void mkdir(String dir) throwsIOException, InterruptedException, URISyntaxException{
Configuration cfg=newConfiguration();
FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");
filesys.mkdirs(newPath(dir));
filesys.close();
}public void mv(String src,String dst) throwsIOException, InterruptedException, URISyntaxException{
Configuration cfg=newConfiguration();//cfg.set("fs.defaultFs", "hdfs://linux1:9000");
FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");
filesys.moveFromLocalFile(new Path(src), newPath(dst));
filesys.close();
}public void delete(String path) throwsIOException, InterruptedException, URISyntaxException{
Configuration cfg=newConfiguration();
FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");
filesys.delete(new Path(path), true);
filesys.close();
}
}