java下载hdfs文件_hadoop中hdfs文件(上传、下载、查看)操作

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,更多信息请到原文网站查看!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值