hadoop-hdfs-文件工具类(Java)

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class HdfsUtil {

    /**
     * ls
     */
    public void listFiles(String specialPath) {
        FileSystem fileSystem = null;
        try {
            fileSystem = this.getFS();
            ;

            FileStatus[] fstats = fileSystem.listStatus(new Path(specialPath));

            for (FileStatus fstat : fstats) {
                System.out.println(fstat.isDirectory() ? "directory" : "file");
                System.out.println("Permission:" + fstat.getPermission());
                System.out.println("Owner:" + fstat.getOwner());
                System.out.println("Group:" + fstat.getGroup());
                System.out.println("Size:" + fstat.getLen());
                System.out.println("Replication:" + fstat.getReplication());
                System.out.println("Block Size:" + fstat.getBlockSize());
                System.out.println("Name:" + fstat.getPath());

                System.out.println("#############################");
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("link err");
        } finally {
            if (fileSystem != null) {
                try {
                    fileSystem.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * cat
     *
     * @param hdfsFilePath
     */
    public void cat(String hdfsFilePath) {
        FileSystem fileSystem = null;
        try {
            fileSystem = this.getFS();

            FSDataInputStream fdis = fileSystem.open(new Path(hdfsFilePath));

            IOUtils.copyBytes(fdis, System.out, 1024);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(fileSystem);
        }

    }

    /**
     * 创建目录
     *
     * @param hdfsFilePath
     */
    public void mkdir(String hdfsFilePath) {

        FileSystem fileSystem = this.getFS();

        try {
            boolean success = fileSystem.mkdirs(new Path(hdfsFilePath));
            if (success) {
                System.out.println("Create directory or file successfully");
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            this.closeFS(fileSystem);
        }


    }

    /**
     * 删除文件或目录
     *
     * @param hdfsFilePath
     * @param recursive    递归
     */
    public void rm(String hdfsFilePath, boolean recursive) {
        FileSystem fileSystem = this.getFS();
        try {
            boolean success = fileSystem.delete(new Path(hdfsFilePath), recursive);
            if (success) {
                System.out.println("delete successfully");
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            this.closeFS(fileSystem);
        }
    }

    /**
     * 上传文件到HDFS
     *
     * @param localFilePath
     * @param hdfsFilePath
     */
    public void put(String localFilePath, String hdfsFilePath) {
        FileSystem fileSystem = this.getFS();
        try {
            FSDataOutputStream fdos = fileSystem.create(new Path(hdfsFilePath));
            FileInputStream fis = new FileInputStream(new File(localFilePath));
            IOUtils.copyBytes(fis, fdos, 1024);

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(fileSystem);
        }
    }

    public void read(String fileName) throws Exception {

        // get filesystem
        FileSystem fileSystem = this.getFS();

        Path readPath = new Path(fileName);

        // open file
        FSDataInputStream inStream = fileSystem.open(readPath);

        try {
            // read
            IOUtils.copyBytes(inStream, System.out, 4096, false);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // close Stream
            IOUtils.closeStream(inStream);
        }
    }

    /**
     * 下载文件到本地
     *
     * @param localFilePath
     * @param hdfsFilePath
     */
    public void get(String localFilePath, String hdfsFilePath) {
        FileSystem fileSystem = this.getFS();
        try {
            FSDataInputStream fsis = fileSystem.open(new Path(hdfsFilePath));
            FileOutputStream fos = new FileOutputStream(new File(localFilePath));
            IOUtils.copyBytes(fsis, fos, 1024);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(fileSystem);
        }
    }

    public void write(String localPath, String hdfspath) throws Exception {
        FileInputStream inStream = new FileInputStream(
                new File(localPath)
        );
        FileSystem fileSystem = this.getFS();

        Path writePath = new Path(hdfspath);

        // Output Stream
        FSDataOutputStream outStream = fileSystem.create(writePath);


        try {
            IOUtils.copyBytes(inStream, outStream, 4096, false);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(inStream);
            IOUtils.closeStream(outStream);
        }

    }


    /**
     * 获取FileSystem实例
     *
     * @return
     */
    private FileSystem getFS() {
        System.setProperty("hadoop.home.dir", "D:\\04coding\\projects-bigData\\Hadoop\\hadoop-2.5.0");
        System.setProperty("HADOOP_USER_NAME", "xiaoyuzhou");

        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://xyz01.aiso.com:8020/");
        conf.set("mapred.remote.os", "Linux");
        FileSystem fileSystem = null;
        try {
            fileSystem = FileSystem.get(conf);

            return fileSystem;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 关闭FileSystem
     *
     * @param fileSystem
     */
    private void closeFS(FileSystem fileSystem) {
        if (fileSystem != null) {
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿与禅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值