zoukankan      html  css  js  c++  java
  • Windows下使用Java API操作HDFS的常用方法

    场景

    Windows下配置Hadoop的Java开发环境以及用Java API操作HDFS:

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119379055

    在上面将Hadoop的开发环境搭建起来之后,使用Java API 简单输出了文件目录。

    那么对应HDFS的常用文件的操作还有哪些。

    注:

    博客:
    https://blog.csdn.net/badao_liumang_qizhi
    关注公众号
    霸道的程序猿
    获取编程相关电子书、教程推送与免费下载。

    实现

    1、获取HDFS文件系统

        /**
         * 获取HDFS文件系统
         * @return
         * @throws IOException
         */
        public static FileSystem getFileSystem() throws IOException {
            Configuration configuration = new Configuration();
            configuration.set("fs.defaultFS","hdfs://192.168.148.128:9000");
            configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
            System.setProperty("HADOOP_USER_NAME","root");
            FileSystem fileSystem = FileSystem.get(configuration);
            return fileSystem;
        }

    2、获取HDFS中所有的dataNodes

        /**
         * 获取HDFS中所有的dataNode
         * @return
         * @throws IOException
         */
        public static void listDataNodeInfo() throws IOException {
            FileSystem fs = getFileSystem();
            DistributedFileSystem hdfs = (DistributedFileSystem) fs;
            DatanodeInfo[] dataNodeStats =hdfs.getDataNodeStats();
            String[] names = new String[dataNodeStats.length];
            System.out.println("HDFS中所有的dataNode:");
            for(int i=0;i<names.length;i++)
            {
                names[i]=dataNodeStats[i].getHostName();
                System.out.println(names[i]);
            }
        }

     

    3、创建文件夹

        /**
         * 创建文件夹
         * @return
         * @throws IOException
         */
        public static void mkdir() throws IOException {
            FileSystem fs = getFileSystem();
            Path srcPath = new Path("/newDir");
            boolean isok = fs.mkdirs(srcPath);
            if(isok)
            {
                System.out.printf("创建文件夹成功");
            }else
            {
                System.out.printf("创建文件夹失败");
            }
            fs.close();
        }

    4、删除文件夹

        /**
         * 删除文件夹
         * @return
         * @throws IOException
         */
        public static void deletedir() throws IOException {
            FileSystem fs = getFileSystem();
            Path srcPath = new Path("/newDir");
            boolean isok = fs.deleteOnExit(srcPath);
            if(isok)
            {
                System.out.printf("删除文件夹成功");
            }else
            {
                System.out.printf("删除文件夹失败");
            }
            fs.close();
        }

    5、判断文件是否存在

        /**
         * 判断文件是否存在
         * @return
         * @throws IOException
         */
        public static void checkFileExists() throws IOException {
            FileSystem fs = getFileSystem();
            Path srcPath = new Path("/badao.txt");
            boolean isexist = fs.exists(srcPath);
            if(isexist)
            {
                System.out.printf("文件存在");
            }else
            {
                System.out.printf("文件不存在");
            }
        }

    6、上传文件

        /**
         * 上传文件
         * @return
         * @throws IOException
         */
        public static void uploadFile() throws IOException {
    
            FileSystem fs = getFileSystem();
            //源路径
            Path srcPath = new Path("D:\windows.txt");
            //目标路径
            Path targetPath = new Path("/");
            fs.copyFromLocalFile(false,srcPath,targetPath);
            fs.close();
        }

    7、下载文件

        /**
         * 下载文件
         * @return
         * @throws IOException
         */
        public static void downloadFile() throws IOException {
    
            FileSystem fs = getFileSystem();
            //源路径
            Path targetPath = new Path("D:\");
            //目标路径
            Path srcPath = new Path("/user/1.txt");
            fs.copyToLocalFile(srcPath,targetPath);
            fs.close();
        }

    5、重命名文件

        /**
         * 重命名文件
         * @return
         * @throws IOException
         */
        public static void renameFile() throws IOException {
    
            FileSystem fs = getFileSystem();
            //源路径
            Path oldPath = new Path("/windows.txt");
            //目标路径
            Path newPath = new Path("/centos.txt");
            boolean isok = fs.rename(oldPath,newPath);
            if(isok)
            {
                System.out.printf("重命名成功");
            }else
            {
                System.out.printf("重命名失败");
            }
            fs.close();
        }

    6、遍历目录和文件

        /**
         * 遍历目录和文件
         * @return
         * @throws IOException
         */
        public static void showDir(Path path) throws IOException {
    
            FileSystem fs = getFileSystem();
            DistributedFileSystem hdfs = (DistributedFileSystem) fs;
            FileStatus[] fileStatuses = hdfs.listStatus(path);
            if(fileStatuses.length>0)
            {
                for (FileStatus status:fileStatuses) {
                    Path f = status.getPath();
                    System.out.println(f.toString());
                    if(status.isDirectory())
                    {
                        FileStatus[] files = hdfs.listStatus(f);
                        if(files.length>0)
                        {
                            for (FileStatus file:files) {
                                showDir(file.getPath());
                            }
                        }
                    }
                }
            }
        }

    博客园: https://www.cnblogs.com/badaoliumangqizhi/ 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。
  • 相关阅读:
    Android基站定位
    删除远程分支
    docker的相关使用
    [整理]-端口的概念、常用端口及其对应服务
    [转]-nohup-真正的Shell后台运行
    转- 在ubuntu下安装Nginx
    转: 命名关键字参数
    远程SCP报错,报22号口错误
    vim保存时提示: 无法打开并写入文件
    如何处理导出的csv无法查看身份证后三位的情况?
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/15098497.html
Copyright © 2011-2022 走看看