HDFS文件系统的基本使用(2)

HDFS文件系统的基本架构

在这里插入图片描述
hdfs分布式文件系统的设计目标:

1、硬件错误是常态,特别是硬盘的损坏是常态,需要副本机制

2、数据流访问 所有的访问都是访问大量的数据  使用IO流一直操作,追求的是稳定,不是效率

3、大数据集  假设所有存储到hdfs的数据都是海量的数据,不擅长处理小文件,一个小文件占用一个元数据

4、简单的相关模型  假设文件是一次写入、多次读取,不会有频繁的更新,比较擅长存储一些历史数据

5、移动计算比移动数据便宜

6、多种软硬件的可移植性

HDFS的副本机制和block存储

HDFS文件写入过程:

在这里插入图片描述
HDFS文件读取过程:

在这里插入图片描述

一个packet大小默认为64kb
占用磁盘空间大小只与文件大小有关系,跟多少个block块没关系,默认block块大小是128MB,文件副本数是3个。

NameNode与DataNode之间的机制:

 Heartbeats:心跳机制

balanceing:平衡机制

replication:副本机制

在这里插入图片描述

数据副本的存放机制

namenode会首先找离客户端最近的一台机器上传block块(跨交换机最少),然后再去作备份。

namenode负责数据block块的复制,定期检测block的副本数,如果不够三个,继续复制。

namenode的元数据保存在两个地方,一个是内存,一个是磁盘,存是元数据的快照,如果快照非常大,停机再启动的代价非常大。

block块的大小,可以根据实际工作当中的文件特性来调整,如果都是一些大文件,可以稍微调大block块的大小,如下:

<property>

     <name>dfs.block.size</name>

     <value>块大小 以字节为单位</value> //只要填写数值

</property>

比如说:100M的文件 128MB的产生3个block块

3个block块元数据信息会储存在namenode当中

如果block大小为258MB则为两个

块缓存:distributedCache 可以用来实现我们文件的缓存

hdfs的权限验证:采用与linux类似的权限验证机制 权限验证比较弱(hdfs相信你告诉hdfs是谁,你就是谁)

hdfs元数据信息以及fsImage元数据信息的查看

元数据管理的架构图:
在这里插入图片描述
fsimage:保存的是一份最完整的元数据信息

edits:保存的是最近一段时间操作的元数据信息

namenode可以将我们产生的edits文件通过一定的条件控制,合并到我们的一个大的数据中来,叫做fsimage,namenode的是RAM,内存大小不能拓展。

hdfs文件可以保存多少数据,取决于namenode的内存大小,hdfs推荐存储大量文件,不擅长存储小文件。

详细查看fsimage信息:

      hdfs oiv

例如:

  解析成XML储存在hello.txt当中: 

  hdfs oiv -i fsimag_xxxxxxxxxxxxxxx -0  hello.txt  - p  XML

sencondarynamenode的合并:

在这里插入图片描述
edit信息查看:

 hdfs oev -i edits_xxxxxxxxxxx-xxxxxxx  -o edits.txt -p XML

控制edits文件的大小:时间短,文件小,

fs.checkpoint.period:默认的是一个小时

fs.checkpoint.size:达到一定大小时也会触发合并(默认64MB)

JAVA的API操作

导包:

找CDH版本用的包:

      https://docs.cloudera.com/documentation/enterprise/release-notes/topics/cdh_vd_cdh5_maven_repo_514x.html

解决winutils的问题

 第一步:把\解决winutils问题\hadoop-2.6.0-cdh5.14.0 拷贝到一个没有中文没有空格的路径下
 
 第二步:配置windows的HADOOP_HOME环境变量

 第三步:将hadoop-2.6.0-cdh5.14.0\bin\hadoop.dll这个文件放在c:\windows\system32

 第四步:关闭windows重启。

POM配置文件:

<repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
    </repositories>


    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0-mr1-cdh5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.6.0-cdh5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.6.0-cdh5.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>2.6.0-cdh5.14.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <!--    <verbal>true</verbal>-->
                </configuration>
            </plugin>


            <!--  打包的插件,我们项目当中需要用到的其他的jar包都打到一起去
              -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <minimizeJar>true</minimizeJar>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

JAVA源代码:

  获取文件系统的方式:
      @Test
    public  void  downHdfsFile() throws Exception {

        //第一步:注册hdfs的驱动文件,表示我们使用hdfs://这种协议访问我们的hdfs文件系统
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
        //URL需要接受一个文件访问的地址
        String  url = "hdfs://node01:8020/install2.log";
        //获取到了一个inputStream
        InputStream inputStream = new URL(url).openStream();

        OutputStream outputStream = new FileOutputStream(new File("c:\\hello.txt"));

        //通过工具类,将我们 的输入流,读取到输出流,里面去
        IOUtils.copy(inputStream,outputStream);
        //关闭io流
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);

    }


    /**
     * 获取文件系统的几种方式
     */
    @Test
    public  void getFileSystem1() throws IOException {
        //FileSystem是一个抽象类,获取抽象类的实例 有两种方式,第一种,看看这个抽象类有么有提供什么方法,返回他本身
        //第二种方式,找子类
        Configuration configuration = new Configuration();
        //如果这里不加任何配置,这里获取到的就是本地文件系统
        configuration.set("fs.defaultFS","hdfs://node01:8020");
        FileSystem fileSystem = FileSystem.get(configuration);
        System.out.println(fileSystem.toString());
        fileSystem.close();

    }

    /**
     * 第二种方式获取hdfs分布式文件系统
     */
    @Test
    public  void  getFileSystem2() throws Exception {
        Configuration configuration = new Configuration();
        //通过我们指定URI来获取分布式文件系统
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), configuration);
        System.out.println(fileSystem.toString());
        fileSystem.close();
    }


    /**
     * 第三种获取分布式文件系统的方法
     */
    @Test
    public  void getFileSystem3() throws IOException {
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS","hdfs://node01:8020");
        FileSystem fileSystem = FileSystem.newInstance(configuration);
        System.out.println(fileSystem.toString());
        fileSystem.close();
    }
  递归遍历hdfs:
       /**
     * 递归遍历hdfs上面所有的文件
     */
    @Test
    public  void  getAllFiles()throws  Exception{
        //获取文件系统
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());

        //获取到我们的文件的状态,可以通过fileStatuses来判断究竟是文件还是文件夹
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("hdfs://node01:8020/"));
        //循环遍历FileStatus  判断文件究竟是文件夹,还是文件,如果是文件,直接输出路径,如果是文件夹,继续进去遍历
        for (FileStatus fileStatus : fileStatuses) {
            if(fileStatus.isDirectory()){
                //文件夹
            getDirFiles(fileStatus.getPath(),fileSystem);

            }else{
                //文件
                Path path = fileStatus.getPath();
                System.out.println(path.toString());

            }

        }

    }



  // 如果为文件夹则继续遍历
  
  public  void  getDirFiles(Path path,FileSystem fileSystem) throws IOException {
        FileStatus[] fileStatuses = fileSystem.listStatus(path);
        for (FileStatus fileStatus : fileStatuses) {
            if(fileStatus.isDirectory()){
                getDirFiles(fileStatus.getPath(),fileSystem);
            }else{
                System.out.println( fileStatus.getPath().toString());


            }
        }


    }

    下载文件到本地目录:

        /**
     * 下载hdfs的文件到本地目录
     */
    @Test
    public void downFileTolocal() throws  Exception{
        //获取文件系统
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
      //第一种下载文件方式
        FSDataInputStream inputStream = fileSystem.open(new Path("hdfs://node01:8020/install2.log"));
        FileOutputStream fileOutputStream = new FileOutputStream(new File("c:\\hello2.txt"));
        IOUtils.copy(inputStream,fileOutputStream);
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(fileOutputStream);
        fileSystem.close();

       //第二种文件下载的方式
        fileSystem.copyToLocalFile(new Path("hdfs://node01:8020/install2.log"),new Path("file:///c:\\hello3.txt"));
        fileSystem.close();

    }
    hdfs上文件的读取:
/**
 * 读取文件
 */
    @Test
    public  void  readFile() throws  Exception{
        FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://node01:8020"), new Configuration(),"root");

        fileSystem.copyToLocalFile(new Path("hdfs://node01:8020/config/core-site.xml"),new Path("file:///c:\\core-site.xml"));

        fileSystem.close();

    }
          小文件的合并: 

    @Test
    public   void  mergeSmallFile() throws  Exception{
        //获取分布式文件系统
        FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://node01:8020"), new Configuration(),"root");
        //获取hdfs文件上面的输出流
        FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("hdfs://node01:8020/bigFile.xml"));

        //获取本地文件系统,遍历得到我们本地的小文件,将每一个小文件读成一个输入流
        LocalFileSystem localFileSystem = FileSystem.getLocal(new Configuration());
        //通过本地文件系统,遍历本地的所有的小文件
        FileStatus[] fileStatuses = localFileSystem.listStatus(new Path("file:///F:\\传智播客大数据离线阶段课程资料\\3、大数据离线第三天\\上传小文件合并"));
        for (FileStatus fileStatus : fileStatuses) {
            //获取每一个文件的路径
            Path path = fileStatus.getPath();
            //循环遍历读取我们每一个文件的输入流
            FSDataInputStream fsDataInputStream = localFileSystem.open(path);
            IOUtils.copy(fsDataInputStream, fsDataOutputStream);
            IOUtils.closeQuietly(fsDataInputStream);


        }

        IOUtils.closeQuietly(fsDataOutputStream);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值