public classHdfsService {
DataAccSourceService dass;privateFileSystem getCorSys() {
FileSystem coreSys= null;
Configuration conf= newConfiguration();try{return FileSystem.get(URI.create("hdfs://192.168.1.66:8020"), conf,"username");
}catch(Exception e) {
e.printStackTrace();
}returncoreSys;
}//创建目录
publicboolean createDir(String path){
FileSystem coreSys=getCorSys();try{return coreSys.mkdirs(newPath(path));
}catch(IOException e) {return false;
}finally{try{
coreSys.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}//修改目录文件
publicboolean renameDir(String oldPath,String newPath){
FileSystem coreSys=getCorSys();try{return coreSys.rename(new Path(oldPath),newPath(newPath));
}catch(IOException e) {return false;
}finally{try{
coreSys.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}//删除目录文件
publicboolean delDir(String path){
FileSystem coreSys=getCorSys();try{return coreSys.delete(new Path(path),true);
}catch(IOException e) {return false;
}finally{try{
coreSys.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}//目录和文件信息
public ListlistFile(String path) throws IOException {
FileSystem coreSys=getCorSys();
Path p= newPath(path);
FileStatus[] files=coreSys.listStatus(p);
ImmutableList.Builder builder =ImmutableList.builder();
FileStatus[] var5=files;int var6 =files.length;for(int var7 = 0; var7 < var6; ++var7) {
FileStatus file=var5[var7];
builder.add(newIOVFile(file));
}returnbuilder.build();
}//文件上传
publicboolean uoloadFile(String srcPath,String desPath){
FileSystem coreSys=getCorSys();try{if(coreSys.isDirectory(newPath(desPath))){
coreSys.copyFromLocalFile(new Path(srcPath),newPath(desPath));return true;
}else{throw new IOException("desPath is not exist");
}
}catch(IOException e) {return false;
}finally{try{
coreSys.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}//文件下载
publicboolean downloadFile(String srcPath,String desPath){
FileSystem coreSys=getCorSys();try{
coreSys.copyToLocalFile(new Path(srcPath),newPath(desPath));return true;
}catch(IOException e) {return false;
}finally{try{
coreSys.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}//获取文件信息
publicIOVFile getFileInfo(String path) {
FileSystem coreSys=getCorSys();try{return new IOVFile(coreSys.getFileStatus(newPath(path)));
}catch(IOException e) {
e.printStackTrace();
}finally{try{
coreSys.close();
}catch(IOException e) {
e.printStackTrace();
}
}return null;
}
IOVFile文件如下贴出public classIOVFile {public static String SEPARATOR = "/";privateFileStatus fileStatus;privatePath path;privateIOVFile(String path) {this.path = newPath(path);
}publicIOVFile(FileStatus fileStatus) {this.fileStatus =fileStatus;this.path =fileStatus.getPath();
}publicFileStatus fileStatus() {return this.fileStatus;
}public voidsetFileStatus(FileStatus fileStatus) {this.fileStatus =fileStatus;
}publicString getPath() {return this.path.toUri().getPath();
}publicString getName() {return this.path.getName();
}public longgetLength() {return this.fileStatus.getLen();
}publicString getParent() {return this.path.getParent().toUri().getPath();
}publicboolean isDirectory() {return this.fileStatus.isDirectory();
}public longgetModificationTime() {return this.fileStatus.getModificationTime();
}
}
本文转载自:http://www.toyou.plus/web/wzjs/55.html,更多信息请到原文网站查看!