美文网首页大数据,机器学习,人工智能Mac学习大数据玩转大数据
【Mac大数据开发】第七篇-HDFS的开发环境和JavaAPI

【Mac大数据开发】第七篇-HDFS的开发环境和JavaAPI

作者: irving_yuan | 来源:发表于2019-06-29 13:44 被阅读0次

    1. 开发环境配置

    1. 添加Hadoop环境变量
    # hadoop安装目录
    export HADOOP_HOME="/opt/hadoop2.8.4"
    # 配置PATH
    export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
    

    注:网上相关教程要求本地也安装hadoop,添加环境变量,然后才能使用Java客户端。没有看过官方文档说明是否如此。欢迎勘误

    1. 引入pom依赖
      使用maven管理项目依赖,导入依赖jar如下.
      下载比较慢,建议使用阿里云nexus镜像
      <dependencies>
          <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.8.4</version>
          </dependency>
          <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.8.4</version>
          </dependency>
          <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.8.4</version>
          </dependency>
    
          <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
          </dependency>
    
          <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
          </dependency>
          <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
          </dependency>
    
          <!-- https://mvnrepository.com/artifact/junit/junit -->
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
          </dependency>
      </dependencies>
    

    2. JavaAPI

    HDFS的client使用非常简单。它的Java API使用FileSystem类作为功能接口,首先需要获取该链接,然后进行操作,最后关闭链接。基础代码如下

        // 使用namenode地址,namenode管理数据的元信息
        private static final String NAME_NODE_URI = "hdfs://192.168.56.101:9000";
        private static final String USER = "root";
    
        public static void main(String[] args) {
    
            FileSystem fileSystem = null;
            try {
                listForder(fileSystem);
    //            upload(fileSystem);
    //            download(fileSystem);
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != fileSystem) {
                        fileSystem.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 获取文件系统链接
         */
        private static FileSystem getFileSystem() throws Exception{
            // 支持自定义配置
            Configuration configuration = new Configuration();
            // NameNode的链接,client通过namenode获取元信息
            URI uri = new URI(NAME_NODE_URI);
            FileSystem fileSystem = FileSystem.get(uri, configuration, USER);
            return fileSystem;
        }
    
    1. 展示文件
        /**
         * 打印目录信息
         */
        public static void listForder(FileSystem fileSystem) throws Exception{
            System.out.println("打印hdfs文件目录:");
    
            // 获取HDFS文件系统
            fileSystem = getFileSystem();
    
            Path path = new Path("/");
            // 递归获取
            RemoteIterator<LocatedFileStatus> it = fileSystem.listFiles(path, true);
            while (it.hasNext()) {
                LocatedFileStatus status = it.next();
                System.out.println("文件名: " + status.getPath().getName());
                System.out.println("文件大小: " + status.getLen());
                System.out.println("块大小: " + status.getBlockSize());
                System.out.println("块位置: ");
                for (BlockLocation location : status.getBlockLocations()) {
                    for (String host : location.getHosts()) {
                        System.out.println(host);
                    }
                }
                System.out.println("全部信息:" + status);
                System.out.println("-------------------");
            }
    
            System.out.println("文件目录打印完毕");
        }
    

    测试结果:


    文件目录
    1. 上传文件
        /**
         * 上传文件
         */
        public static void upload(FileSystem fileSystem) throws Exception{
            System.out.println("测试上传文件");
            fileSystem = getFileSystem();
            // 源路径
            Path src = new Path("/Users/yuanyc/Desktop/soa.xml");
            // 目标路径
            Path dst = new Path("/");
            fileSystem.copyFromLocalFile(src, dst);
            System.out.println("上传文件结束");
        }
    

    测试结果:


    soa.xml上传结果
    1. 下载文件
        /**
         * 下载文件
         */
        public static void download(FileSystem fileSystem) throws Exception {
            System.out.println("下载文件开始");
            fileSystem = getFileSystem();
            // 下载1.jpg
            Path src = new Path("/1.jpg");
            Path dst = new Path("/Users/yuanyc/Desktop");
            fileSystem.copyToLocalFile(src, dst);
            System.out.println("下载文件结束");
        }
    

    测试结果:
    成功下载

    1. 创建目录
        /**
         * 创建目录
         */
        public static void mkDir(FileSystem fileSystem) throws Exception {
            System.out.println("创建目录");
    
            fileSystem = getFileSystem();
    
            Path dir = new Path("/hello/world");
            fileSystem.mkdirs(dir);
    
            System.out.println("创建目录完成");
        }
    

    测试结果:web端看到该目录


    web浏览
    1. 删除目录
        /**
         * 删除目录
         */
        public static void rmDir(FileSystem fileSystem) throws Exception {
            System.out.println("删除目录");
    
            fileSystem = getFileSystem();
            boolean isrecuirse = true;
            Path dir = new Path("/hello");
            fileSystem.delete(dir, isrecuirse);
    
            System.out.println("删除目录完成");
        }
    

    测试结果:成功删除

    1. 流操作
      通过fileSystem的create和open方法可以创建和获取输入和输出流
        /**
         * 流方式下载
         */
        public static void streamDownLoad(FileSystem fileSystem) throws Exception {
            fileSystem = getFileSystem();
    
            Path path = new Path("/soa.xml");
            FSDataInputStream inputStream = fileSystem.open(path);
            // 使用IOUtils操作buffer,减少nio使用的复杂度
            IOUtils.copyBytes(inputStream, System.out, 1024*1024,false);
        }
    

    测试结果:


    测试结果

    相关文章

      网友评论

        本文标题:【Mac大数据开发】第七篇-HDFS的开发环境和JavaAPI

        本文链接:https://www.haomeiwen.com/subject/smfpcctx.html