zoukankan      html  css  js  c++  java
  • Hadoop 核心-HDFS基础

    hadoop集群启动

    cd /export/servers/hadoop-2.7.5/
    sbin/start-dfs.sh
    sbin/start-yarn.sh
    sbin/mr-jobhistory-daemon.sh start historyserver
    

    三个端口查看界面

    http://node01:50070/explorer.html#/ 查看hdfs
    http://node01:8088/cluster 查看yarn集群
    http://node01:19888/jobhistory 查看历史完成的任务
    

    HDFS 的架构

    HDFS是一个主/从(Mater/Slave)体系结构

    HDFS由四部分组成,HDFS Client、NameNode、DataNode和Secondary NameNode。

    HDFS Client: 就是客户端。

    文件切分。文件上传 HDFS 的时候,Client 将文件切分成 一个一个的Block,然后进行存
    储。
    与 NameNode 交互,获取文件的位置信息。
    与 DataNode 交互,读取或者写入数据。
    Client 提供一些命令来管理 和访问HDFS,比如启动或者关闭HDFS。

    NameNode:就是 master,它是一个主管、管理者。

    • 管理 HDFS 的名称空间
    • 管理数据块(Block)映射信息
    • 配置副本策略
    • 处理客户端读写请求。

    DataNode:就是Slave。NameNode 下达命令,DataNode 执行实际的操作。

    • 存储实际的数据块。
    • 执行数据块的读/写操作。

    Secondary NameNode:并非 NameNode 的热备。当NameNode 挂掉的时候,它并不能马上替换 NameNode 并提供服务。

    • 辅助 NameNode,分担其工作量。
    • 定期合并 fsimage和fsedits,并推送给NameNode。
    • 在紧急情况下,可辅助恢复 NameNode。

    hdfs的命令行使用

    • ls

      格式: hdfs dfs -ls URI
      作用:类似于Linux的ls命令,显示文件列表
      eg. hdfs dfs -ls /
      
    • lsr

      格式 : hdfs dfs -lsr URI
      作用 : 在整个目录下递归执行ls, 与UNIX中的ls-R类似
      eg. hdfs dfs -lsr /
      
    • mkdir

      格式 : hdfs dfs [-p] -mkdir <paths>
      作用 : 以<paths>中的URI作为参数,创建目录。使用-p参数可以递归创建目录
      
    • put

      格式 : hdfs dfs -put <localsrc > ... <dst>
      作用 : 将单个的源文件src或者多个源文件srcs从本地文件系统拷贝到目标文件系统中(<dst>对应的路径)。也可以从标准输入中读取输入,写入目标文件系统中
      hdfs dfs -put /rooot/a.txt /dir1
      
    • moveFromLocal

      格式: hdfs dfs -moveFromLocal <localsrc> <dst>
      作用: 和put命令类似,但是源文件localsrc拷贝之后自身被删除
      hdfs dfs -moveFromLocal /root/install.log /
      
    • get

      格式 hdfs dfs -get [-ignorecrc ] [-crc] <src> <localdst>
      作用:将文件拷贝到本地文件系统。 CRC 校验失败的文件通过-ignorecrc选项拷贝。 文件和CRC校验和可以通过-CRC选项拷贝
      hdfs dfs -get /install.log /export/servers
      
    • mv

      格式 : hdfs dfs -mv URI <dest>
      作用: 将hdfs上的文件从原路径移动到目标路径(移动之后文件删除),该命令不能跨文件系统
      hdfs dfs -mv /dir1/a.txt /dir2
      
    • rm

      格式: hdfs dfs -rm [-r] 【-skipTrash】 URI 【URI 。。。】
      作用: 删除参数指定的文件,参数可以有多个。 此命令只删除文件和非空目录。
      如果指定-skipTrash选项,那么在回收站可用的情况下,该选项将跳过回收站而直接删除文件;否则,在回收站可用时,在HDFS Shell 中执行此命令,会将文件暂时放到回收站中。
      hdfs dfs -rm -r /dir1
      
    • cp

      格式: hdfs dfs -cp URI [URI ...] <dest>
      作用: 将文件拷贝到目标路径中。如果<dest> 为目录的话,可以将多个文件拷贝到该目录
      下。
      -f
      选项将覆盖目标,如果它已经存在。
      -p
      选项将保留文件属性(时间戳、所有权、许可、ACL、XAttr)。
      hdfs dfs -cp /dir1/a.txt /dir2/b.txt
      
    • cat

      hdfs dfs -cat URI [uri ...]
      作用:将参数所指示的文件内容输出到stdout
      hdfs dfs -cat /install.log
      
    • chmod

      格式: hdfs dfs -chmod [-R] URI[URI ...]
      作用: 改变文件权限。如果使用 -R 选项,则对整个目录有效递归执行。使用这一命令的用户
      必须是文件的所属用户,或者超级用户。
      hdfs dfs -chmod -R 777 /install.log
      
    • chown

      格式: hdfs dfs -chmod [-R] URI[URI ...]
      作用: 改变文件的所属用户和用户组。如果使用 -R 选项,则对整个目录有效递归执行。使用
      这一命令的用户必须是文件的所属用户,或者超级用户。
      hdfs dfs -chown -R hadoop:hadoop /install.log
      
    • appendToFile

      格式: hdfs dfs -appendToFile <localsrc> ... <dst>
      作用: 追加一个或者多个文件到hdfs指定文件中.也可以从命令行读取输入.
      hdfs dfs -appendToFile a.xml b.xml /big.xml
      

    HDFs JAVA API操作

    hdfs的URL访问,输入输出

        @Test
        public void urlHdfs() throws IOException {
            // 1. 注册HDFs的url
            // 在该处,思考括号内应该放置的对象:URLStreamHandlerFactory fac
            // 查看一个类的所有子类以及子类的子类并以层级关系显示
            // URLStreamHandlerFactory 实例用于从协议名称构造流协议处理程序。
            URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
            // 2. 获取hdfs文件的输入流
            InputStream inputStream=new URL("hdfs://node01:8020/a.txt").openStream();
    
            // 3. 获取本地文件的输出流
                FileOutputStream OutputStream=new FileOutputStream(new File("D:\hello.txt"));
            // 4. 实现文件的拷贝
            IOUtils.copy(inputStream, OutputStream);
    
            // 5. 关流
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(OutputStream);
        } 
    

    获取hdfs文件系统的四种主要方式

        @Test
        public void getFileSystem4() throws IOException, URISyntaxException {
            // 1: 获取指定的文件系统
            FileSystem fileSystem=FileSystem.newInstance(new URI("hdfs://node01:8020"),new Configuration());
            // 2: 输出
            System.out.println(fileSystem);
        }
    
    @Test
        public void getFileSystem3() throws IOException {
            // 1: 创建 Configuration 对象
            // Configuration 封装了客户端或者服务器端的配置,设置对应的文件系统类型,使用 FileSystem.get方法获取对应的文件系统对象
            //进而可对该文件系统进行操作
            Configuration configuration=new Configuration();
            // 2: 设置文件系统类型
            configuration.set("fs.defaultFS", "hdfs://node01:8020");
            // 3: 获取指定的文件系统
            FileSystem fileSystem=FileSystem.newInstance(configuration);
            // 4: 输出
            System.out.println(fileSystem);
    
        }
    @Test
        public void getFileSystem2() throws IOException, URISyntaxException {
            // 1: 获取指定的文件系统
            FileSystem fileSystem=FileSystem.get(new URI("hdfs://node01:8020"),new Configuration());
            // 2: 输出
            System.out.println(fileSystem);
        }
    
    
        @Test
        public void getFileSystem1() throws IOException {
            // 1: 创建Configuration 对象
            Configuration configuration=new Configuration();
            // 2: 设置文件系统类型
            configuration.set("fs.defaultFS", "hdfs://node01:8020");
            // 3: 获取指定的文件系统
            FileSystem fileSystem=FileSystem.get(configuration);
            // 4: 输出
            System.out.println(fileSystem);
        }
    
    

    HDFs文件的遍历

        @Test
        public void listFiles() throws URISyntaxException, IOException {
            // 1. 获取FileSystem 实例
            FileSystem fileSystem=FileSystem.get(new URI("hdfs://node01:8020"),new Configuration());
    
            // 2. 调用方法listFiles 获取/目录下所有的文件信息
            RemoteIterator<LocatedFileStatus> iterator=fileSystem.listFiles(new Path("/"),true);
            // 3. 遍历迭代器
            while(iterator.hasNext()){
                LocatedFileStatus fileStatus=iterator.next();
                System.out.println("路径:"+(fileStatus.getPath())+"---"+fileStatus.getPath().getName());
                BlockLocation[] blockLocations=fileStatus.getBlockLocations();
                System.out.println("分成多少块:"+blockLocations.length);
            }
        }
    

    HDFs上创建文件夹

        @Test
        public void mkdirs() throws Exception{
            // 1. 获取FileSystem实例
            FileSystem fileSystem=FileSystem.get(new URI("hdfs://node01:8020"),new Configuration());
            // 2. 创建文件夹
            boolean mkdirs= fileSystem.mkdirs(new Path("/hello/123"));
            fileSystem.create(new Path("/hello/123/a.txt"));
            System.out.println(mkdirs);
            // 3.
            fileSystem.close();
        }
    

    文件下载1

        @Test
        public void downloadFile1() throws URISyntaxException, IOException {
            // 1. 获取FileSystem
            FileSystem fileSystem=FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
            // 2. 获取hdfs的输入流
            FSDataInputStream inputStream=fileSystem.open(new Path("/a.txt"));
            // 3. 获取本地路径的输出流
            OutputStream outputStream=new FileOutputStream("D://a.txt");
            // 4. 文件的拷贝
            IOUtils.copy(inputStream, outputStream);
            // 5. 关闭流
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(outputStream);
            fileSystem.close();
        }
    

    文件下载2

        @Test
        public void downloadFile2() throws URISyntaxException, IOException {
            // 1. 获取FileSystem
            FileSystem fileSystem=FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
            // 2. 文件的拷贝
            fileSystem.copyToLocalFile(new Path("/a.txt"),new Path("D://b.txt"));
            // 3. 关闭文件系统
            fileSystem.close();
        }
    

    文件上传

        @Test
        public void uploadFile() throws URISyntaxException, IOException {
            // 1. 获取FileSystem
            FileSystem fileSystem=FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
            // 2. 文件的拷贝
            fileSystem.copyFromLocalFile(new Path("D://b.txt"),new Path("/b.txt"));
            // 3. 关闭文件系统
            fileSystem.close();
        }
    

    小文件合并上传

        @Test
        public void mergeFile() throws URISyntaxException, IOException, InterruptedException {
            //Configuration 封装了客户端或者服务器端的配置,设置对应的文件系统类型,使用FileSystem.get方法获取对应的文件系统对象
            //进而可对该文件系统进行操作
            //1. 获取FileSystem 分布式文件系统
            FileSystem fileSystem=FileSystem.get(new URI("hdfs://node01:8020"),new Configuration(),"root");
            //2. 获取hdfs大文件的输出流
            FSDataOutputStream outputStream=fileSystem.create(new Path("/big.txt"));
            //3. 获取本地文件系统
            LocalFileSystem localFileSystem=FileSystem.getLocal(new Configuration());
            //4. 通过本地文件系统获取文件列表,为一个集合
            FileStatus[] fileStatus=localFileSystem.listStatus(new Path("D://input"));
            //5. 遍历每个文件,获取每个文件的输入流
            for(FileStatus file:fileStatus){
                FSDataInputStream inputStream=localFileSystem.open(file.getPath());
                //6. 将小文件的数据复制到大文件
                IOUtils.copy(inputStream, outputStream);
    
                IOUtils.closeQuietly(inputStream);
            }
            IOUtils.closeQuietly(outputStream);
            localFileSystem.close();
            fileSystem.close();
        }
    
  • 相关阅读:
    javascript 之迭代器
    前端数据结构--二叉树先序、中序、后序 递归、非递归遍历
    前端数据结构--树
    前端数据结构--散列表(哈希表)
    前端数据结构--线性结构-队列、栈
    前端数据结构--线性结构-链表
    前端数据结构--线性结构-数组
    前端数据结构---复杂度分析
    前端数据结构---相关基础概念
    css整理之-----------基本知识
  • 原文地址:https://www.cnblogs.com/alidata/p/13456272.html
Copyright © 2011-2022 走看看