码农公社 210.net.cn 210是何含义?10月24日是程序员节,1024 =210、210既
210
之意。
废话不叙,上代码。
public class HdfsService {
DataAccSourceService dass;
private FileSystem getCorSys() {
FileSystem coreSys = null;
Configuration conf = new Configuration();
try {
return FileSystem.get(URI.create("hdfs://192.168.1.66:8020"), conf,"username");
} catch (Exception e) {
e.printStackTrace();
}
return coreSys;
}
//创建目录
public boolean createDir(String path){
FileSystem coreSys=getCorSys();
try {
return coreSys.mkdirs(new Path(path));
} catch (IOException e) {
return false;
}finally {
try {
coreSys.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//修改目录文件
public boolean renameDir(String oldPath,String newPath){
FileSystem coreSys=getCorSys();
try {
return coreSys.rename(new Path(oldPath),new Path(newPath));
} catch (IOException e) {
return false;
}finally {
try {
coreSys.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//删除目录文件
public boolean 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 List listFile(String path) throws IOException {
FileSystem coreSys=getCorSys();
Path p = new Path(path);
FileStatus[] files = coreSys.listStatus(p);
ImmutableList.Builder builder = ImmutableList.builder();
FileStatus[] var5 = files;
int var6 = files.length;
for(int var7 = 0; var7
FileStatus file = var5[var7];
builder.add(new IOVFile(file));
}
return builder.build();
}
//文件上传
public boolean uoloadFile(String srcPath,String desPath){
FileSystem coreSys=getCorSys();
try {
if(coreSys.isDirectory(new Path(desPath))){
coreSys.copyFromLocalFile(new Path(srcPath),new Path(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();
}
}
}
//文件下载
public boolean downloadFile(String srcPath,String desPath){
FileSystem coreSys=getCorSys();
try {
coreSys.copyToLocalFile(new Path(srcPath),new Path(desPath));
return true;
} catch (IOException e) {
return false;
}finally {
try {
coreSys.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//获取文件信息
public IOVFile getFileInfo(String path) {
FileSystem coreSys=getCorSys();
try {
return new IOVFile(coreSys.getFileStatus(new Path(path)));
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
coreSys.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
IOVFile文件如下贴出
public class IOVFile {
public static String SEPARATOR = "/";
private FileStatus fileStatus;
private Path path;
private IOVFile(String path) {
this.path = new Path(path);
}
public IOVFile(FileStatus fileStatus) {
this.fileStatus = fileStatus;
this.path = fileStatus.getPath();
}
public FileStatus fileStatus() {
return this.fileStatus;
}
public void setFileStatus(FileStatus fileStatus) {
this.fileStatus = fileStatus;
}
public String getPath() {
return this.path.toUri().getPath();
}
public String getName() {
return this.path.getName();
}
public long getLength() {
return this.fileStatus.getLen();
}
public String getParent() {
return this.path.getParent().toUri().getPath();
}
public boolean isDirectory() {
return this.fileStatus.isDirectory();
}
public long getModificationTime() {
return this.fileStatus.getModificationTime();
}
}