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

本文提供了一个Java类,用于实现HDFS的基本操作,包括目录及文件的创建、重命名、删除,以及文件的上传、下载等功能,并展示了如何使用这些方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转载自:http://www.toyou.plus/web/wzjs/55.html,更多信息请到本文网站查看!
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<IOVFile> listFile(String path) throws IOException {
        FileSystem coreSys=getCorSys();
        Path p = new Path(path);
        FileStatus[] files = coreSys.listStatus(p);
        ImmutableList.Builder<IOVFile> builder = ImmutableList.builder();
        FileStatus[] var5 = files;
        int var6 = files.length;

        for(int var7 = 0; var7 < var6; ++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(); } }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值