hadoop3的Java接口工具类

hadoop3的Java接口工具类

准备工作

配置好对应版本hadoop winutils

pom文件

     <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-common</artifactId>
                <version>${hadoop.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-hdfs</artifactId>
                <version>${hadoop.version}</version>

            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-client</artifactId>
                <version>${hadoop.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-mapreduce-client-core</artifactId>
                <version>${hadoop.version}</version>
            </dependency>
      <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.18.0</version>
            </dependency>

java工具类


/**
 * @author wangqinglu
 * &#064;date  2022/9/2
 */
public class HDFSUtils {

    /** hdfs服务器地址 */
    private static final String hdfsUrl = "hdfs://192.168.228.132:8020/";
    /** 访问用户 */
    private static final String username = "root";
    /** 日志接口 */
   private static final  Configuration conf = new Configuration();



   /**
     * 建文件夹
     * @param hdfsPath 文件路径
     */
    public  static void makdirFolder(String hdfsPath) throws  Exception {
        try (FileSystem hdfs =  FileSystem.get(new URI(hdfsUrl),conf,username)){
            Path path = new Path(hdfsPath);
            if (hdfs.exists(path)){
                System.out.println("文件夹已存在");
            }else {hdfs.mkdirs(path);
                System.out.println("创建文件成功,路径"+hdfsPath);}
        }}

    /**
     * 上传文件
     * @param hdfsPath 文件路径
     * @param localPath 本地文件路径
     */
    public static void putFile(String localPath,String hdfsPath)throws  Exception {
        try (FileSystem hdfs =  FileSystem.get(new URI(hdfsUrl),conf,username)){
            hdfs.copyFromLocalFile(false,true,new Path(localPath),new Path(hdfsPath));
            System.out.println("上传文件成功,路径"+hdfsPath);
        }
    }

    /**
     * 下载文件
     * @param hdfsPath 文件路径
     * @param localPath 本地文件路径
     */
    public static void getFile(String hdfsPath,String localPath)throws  Exception {
        try (FileSystem hdfs =  FileSystem.get(new URI(hdfsUrl),conf,username)){
            hdfs.copyToLocalFile(false,new Path(hdfsPath),new Path(localPath),true);
            System.out.println("文件下载成功");
        }
    }

    /**
     * 文件删除
     * @param hdfsPath 文件路径
     */
    public static void deleteFile(String hdfsPath)throws  Exception {
        try (FileSystem hdfs =  FileSystem.get(new URI(hdfsUrl),conf,username)){
            hdfs.delete(new Path(hdfsPath),true);
            System.out.println("文件删除成功");
        }}

    /**
     * 移动文件,可修改名称
     *@param newHdfsPile 文件新路径
     * @param hdfsPath 文件路径
     */
    public static void mvFile(String hdfsPath,String newHdfsPile)throws  Exception {
        try (FileSystem hdfs =  FileSystem.get(new URI(hdfsUrl),conf,username)){
            hdfs.rename(new Path(hdfsPath),new Path(newHdfsPile));
            System.out.println("文件移动成功,新路径"+newHdfsPile);
        }}

    /**
     * 获取文件详细信息
     *
     * @param hdfsPath 文件路径
     */
    public static void fileDetail(String hdfsPath)throws  Exception {
       try (FileSystem hdfs =  FileSystem.get(new URI(hdfsUrl),conf,username)){
           RemoteIterator<LocatedFileStatus> listFiles = hdfs.listFiles(new Path(hdfsPath), true);
           while (listFiles.hasNext()) {
               LocatedFileStatus fileStatus = listFiles.next();
               System.out.println("========"+fileStatus.getPath()+"========");
               System.out.println(fileStatus.getPermission());
               System.out.println(fileStatus.getOwner());
               System.out.println(fileStatus.getGroup());
               System.out.println(fileStatus.getLen());
               System.out.println(fileStatus.getModificationTime());
               System.out.println(fileStatus.getReplication());
               System.out.println(fileStatus.getBlockSize());
               System.out.println(fileStatus.getPath().getName());
               BlockLocation[] blockLocations = fileStatus.getBlockLocations();
               System.out.println(Arrays.toString(blockLocations));
           }}}
    /**
     * 判断是否是文件夹
     *
     * @param hdfsPath 文件路径
     */
    public static void judgeFile(String hdfsPath)throws  Exception {
        try (FileSystem hdfs =  FileSystem.get(new URI(hdfsUrl),conf,username)){
            FileStatus[] listStatus = hdfs.listStatus(new Path(hdfsPath));
            for (FileStatus status:listStatus){
                if (status.isFile()) System.out.println("文件: " + status.getPath().getName());
                 else System.out.println("目录: "+ status.getPath().getName());
            }
        }}

    /**
     * 读取文件内容
     *
     * @param filePath 文件路径
     */
    public static void HDFSReadFileValue(String filePath) throws Exception {
        try (FileSystem fileSystem = FileSystem.get(new URI(hdfsUrl), conf, username)) {
            Path demo = new Path(filePath);
            if (fileSystem.exists(demo)) {
                System.out.println("文件存在");
                FSDataInputStream fsdis = fileSystem.open(demo);
                byte[] bytes = new byte[fsdis.available()];
                fsdis.readFully(bytes);
                System.out.println(new String(bytes));
            } else {
                System.out.println(" 文件不存在");
            }
        }
    }

效果

例:建文件夹
调用madkdirFolder方法
调用madkdirFolder方法
添加文件夹前hdfs系统
在这里插入图片描述
在这里插入图片描述
添加后
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值