JAVA HDFS 文件上传、下载、删除、更名、移动/剪切、获取文件列表、复制文件/文件夹、查找、创建文件夹、获取文件夹大小

// 1文件上传
/**
     * 文件上传
     *
     * @param IDc       用户ID
     * @param localPath 本地文件完整路径
     * @param cloudPath hdfs文件保存完整路径
     * @return 返回json格式字符串,包含operation和log两个属性
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    public String Upload(String IDc, String localPath, String cloudPath) throws IOException, InterruptedException, URISyntaxException {

        // 1获取fs对象
        Configuration conf = new Configuration();
//        conf.set("dfs.replication", "2");
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");

        Path findf = new Path(cloudPath);
        //FileSystem.exists指定HDFS文件是否存在
        boolean isExists = fs.exists(findf);
//        System. out.println(isExists?"文件存在" :"文件不存在" );
        if (isExists) {
            fs.close();
            return "{\"operation\":\"The file/folder you are uploading already exists\",\"log\":{}}";
        }

        // 2 执行上传API
        fs.copyFromLocalFile(new Path(localPath), new Path(cloudPath));//前本后云

        // 3关闭资源
        fs.close();
        return "{\"operation\":\"Upload\",\"log\":{\"IDc\":\"" + IDc + "\",\"ticket\":\"null\","
                + "\"file\":\"" + cloudPath + "\",\"operation\":\"Upload\","
                + "\"time\":\"" + System.currentTimeMillis() + "\"}}";
    }

    // 2文件下载

    /**
     * 文件下载子操作
     *
     * @param cloudPath hdfs文件下载完整路径
     * @param localPath 文件下载到本地完整路径
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    private void down(String cloudPath, String localPath) throws IOException, InterruptedException, URISyntaxException {

        // 1获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");

        // 2执行下载操作
//		fs.copyToLocalFile(new Path("/banhua.txt"), new Path("D:/banhua.txt"));
        fs.copyToLocalFile(false, new Path(cloudPath), new Path(localPath), true);

        // 3关闭资源
        fs.close();
    }


    // 3文件删除

    /**
     * 文件删除
     *
     * @param IDc       用户ID
     * @param cloudPath hdfs文件完整路径
     * @return 返回json格式字符串,包含operation和log两个属性
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    public String Delete(String IDc, String cloudPath) throws IOException, InterruptedException, URISyntaxException {

//		 1获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");

        //判断文件是否存在
        if (!fs.exists(new Path(cloudPath))) {
            fs.close();
            return "{\"operation\":\"The file/folder you are deleting does not exist\",\"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值