hadoop上传文件java_java实现在hadoop中hdfs文件(上传、下载、查看)操作

码农公社  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();

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值