Hadoop总结 六 HDFS 一 HDFS基础

Hadoop总结 六 HDFS 一 基础

6.1 HDFS 概述

  1. HDFS 只是分布式文件管理系统中的一种

  2. HDFS 是一个文件系统,通过目录树定位文件,是分布式的

  3. HDFS 适合一次写入,多次读出的场景,且不支持文件的修改,适合文件存储和数据分析

  4. 优点:

    1.高容错性

    数据自动保存多个副本,通过增加副本的形式提高容错性

    某个副本丢失,可以自动恢复

    2.适合处理大数据

    数据规模:能处理数据规模达到GB,TB,PB级别的数据

    文件规模:能够处理百万规模以上的文件数量

    3.可以构建在成本较低的机器上,通过增加副本提高可靠性

  5. 缺点:

    1.不适合低延时数据访问,比如毫秒级的存储数据

    2.无法高效的对大量小文件进行存储

    3.不支持数据的随机修改,仅支持数据的追加(append)

  6. HDFS 的组成结构

    1. NameNode(nn):就是master,是对DataNode管理的对象

      1.管理HDFS的名称空间

      2.配置副本策略

      3.管理数据块映射信息

      4.处理客户端读写请求

    2. DataNode(dn):

      1.存储实际的数据块

      2.执行数据块的读写操作

    3. client:客户端

      1.文件切分:文件上传到HDFS时,client将文件切分成一个一个的block,然后进行上传

      2.与NameNode进行交互:获取文件的位置信息

      3.与DataNode进行交互:读或写入数据

      4.client提供一些命令管理HDFS,比如NameNode格式化

      5.client可以通过一些命令访问HDFS,比如hdfs增删改查的操作

    4. Secondary NameNode:

      1.并非NameNode的热备,当NameNode挂掉时,它并不能马上替换NameNode提供服务

      2.辅助NameNode,分担工作任务,比如定期合并Fsimage和Edits,并推送给NameNode

      3.在紧急情况下可以辅助恢复NameNode

  7. HDFS 文件块大小

    1. HDFS的文件在物理上的存储是分块存储,块的大小可以通过配置参数(dfs.blocksize)进行调整,在hadoop2.x版本及以上默认是128M,在1.x版本中是64M.

    2. 设置合适块大小:寻址时间为传输时间的1%为最佳状态

      如果寻址之间为10ms

      计算传输时间为10ms/0.01=1000ms = 1s

      目前磁盘的传输速度普遍为100MB/s

      即:100MB/S*1S =100MB

    3. 问题(面试警告):为什么块不能设置太小也不能设置太大?

      HDFS 的块设置块太小会增加寻址时间,块小代表着一个文件会被分成更多的块,那么NameNode会寻找更多次才能找到一个文件的所有块的地址

      HDFS 的块设置太大,磁盘传输数据的时间会明显大于定位这个块开始位置所需要的时间,导致程序在出路这块数据时,会非常的慢.

    4. HDFS文件块大小取决于磁盘传输的速率.

6.2 HDFS的shell操作

  1. 基本语法

    hadoop fs 命令

    hadoop dfs 命令

    两种形式的作用是完全相同的

  2. 命令大全

bin/hadoop fs
#显示内容为hadoop的指令
  1. 常用命令

    1.启动hadoop集群

    2.hadoop fs -help rm

    1.上传

    #1.从本地剪切上传到HDFS 相当于上传后自动删除本地文件
    hadoop fs -moveFromLocal /xxxx/xxx.xx(文件)
    #2.从本地复制到HDFS 
    hadoop fs -copyFromLocal /xxx/xxx(文件)
    #3.追加一个文件到已经存在的文件的末尾
    hadoop fs -appendToFile /xxx/xxx(本地文件)  /xx/xx.x(hdfs系统中的文件)
    #4.-put 等同于copyFromLocal
    hadoop fs -put /xxx/xx.x(本地文件)
    

​ 2.下载

#1.从HDFS拷贝到本地
hadoop fs -copyToLocal /xx.xx(hdfs中存储的文件)
#2.get 等同于拷贝到本地
hadoop fs -get /xx.xx
#3.合并下载多个文件
hadoop fs -getmerge /xx/* (hdfs中存储的文件) /xx.xx(合并到本地的文件名)

​ 3.HDFS直接操作

-ls: 显示目录信息
hadoop fs -ls /

-mkdir:在HDFS上创建目录
hadoop fs -mkdir -p /sanguo/shuguo

-cat:显示文件内容
hadoop fs -cat /sanguo/shuguo/kongming.txt

-chgrp 、-chmod、-chown:Linux文件系统中的用法一样,修改文件所属权限
hadoop fs  -chmod  666  /sanguo/shuguo/kongming.txt
hadoop fs  -chown  muxue:muxue   /sanguo/shuguo/kongming.txt

-cp :从HDFS的一个路径拷贝到HDFS的另一个路径
hadoop fs -cp /sanguo/shuguo/kongming.txt /zhuge.txt

-mv:在HDFS目录中移动文件
hadoop fs -mv /zhuge.txt /sanguo/shuguo/

-tail:显示一个文件的末尾1kb的数据
hadoop fs -tail /sanguo/shuguo/kongming.txt

-rm:删除文件或文件夹
hadoop fs -rm /user/muxue/test/jinlian2.txt

-rmdir:删除空目录
hadoop fs -mkdir /test
hadoop fs -rmdir /test

-du统计文件夹的大小信息
hadoop fs -du -s -h /user/muxue/test
2.7 K  /user/muxue/test

hadoop fs -du  -h /user/muxue/test
1.3 K  /user/muxue/test/README.txt
15     /user/muxue/test/jinlian.txt
1.4 K  /user/muxue/test/zaiyiqi.txt

-setrep:设置HDFS中文件的副本数量
hadoop fs -setrep 10 /sanguo/shuguo/kongming.txt

6.3 HDFS的客户端操作(代码)

  1. HDFS 客户端环境准备

    Windows环境下需要配置HDFS的环境变量

  2. 创建maven工程导入相应的依赖

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.3</version>
        </dependency>
    </dependencies>
    
  3. 创建一个测试类

    public class HdfsClient{	
    @Test
    public void testMkdirs() throws IOException, InterruptedException, URISyntaxException{
    		
    		// 1 获取文件系统
    		Configuration configuration = new Configuration();
    		// 配置在集群上运行
    		// configuration.set("fs.defaultFS", "hdfs://hadoop102:9820");
    		// FileSystem fs = FileSystem.get(configuration);
    
    		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9820"), configuration, "test");//test是用户名
    		
    		// 2 创建目录
    		fs.mkdirs(new Path("/1108/daxian/banzhang"));
    		
    		// 3 关闭资源
    		fs.close();
    	}
    }
    
  4. 文件上传

    @Test
    public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
    
    		// 1 获取文件系统
    		Configuration configuration = new Configuration();
        //dfs.replication 需要读取本地的配置文件
    		configuration.set("dfs.replication", "2");
    		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9820"), configuration, "test");
    
    		// 2 上传文件
    		fs.copyFromLocalFile(new Path("e:/xx.txt"), new Path("/banzhang.txt"));
    
    		// 3 关闭资源
    		fs.close();
    
    		System.out.println("over");
    
    <!-- hdfs-site.xml拷贝到项目的根目录下 的resource文件夹下面-->
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    
    <configuration>
    	<property>
    		<name>dfs.replication</name>
             <value>1</value>
    	</property>
    </configuration>
    
  5. 关于configuration参数的优先级

    参数优先级排序:(1)客户端代码中设置的值 >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的自定义配置(xxx-site.xml) >(4)服务器的默认配置(xxx-default.xml)

  6. 文件下载

    @Test
    public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
    
    		// 1 获取文件系统
    		Configuration configuration = new Configuration();
    		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9820"), configuration, "test");
    		
    		// 2 执行下载操作
    		// boolean delSrc 指是否将原文件删除
    		// Path src 指要下载的文件路径
    		// Path dst 指将文件下载到的路径
    		// boolean useRawLocalFileSystem 是否开启文件校验
    		fs.copyToLocalFile(false, new Path("/banzhang.txt"), new Path("e:/banhua.txt"), true);
    		
    		// 3 关闭资源
    		fs.close();
    }
    
  7. 删除HDFS文件和目录

    @Test
    public void testDelete() throws IOException, InterruptedException, URISyntaxException{
    
    	// 1 获取文件系统
    	Configuration configuration = new Configuration();
    	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9820"), configuration, "test");
    		
    	// 2 执行删除
    	fs.delete(new Path("/0508/"), true);
    		
    	// 3 关闭资源
    	fs.close();
    }
    
  8. HDFS 更名和移动

    @Test
    public void testRename() throws IOException, InterruptedException, URISyntaxException{
    
    	// 1 获取文件系统
    	Configuration configuration = new Configuration();
    	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9820"), configuration, "test"); 
    		
    	// 2 修改文件名称
    	fs.rename(new Path("/banzhang.txt"), new Path("/banhua.txt"));
    		
    	// 3 关闭资源
    	fs.close();
    }
    
  9. HDFS文件查看详情

    @Test
    public void testListFiles() throws IOException, InterruptedException, URISyntaxException{
    
    	// 1获取文件系统
    	Configuration configuration = new Configuration();
    	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9820"), configuration, "test"); 
    		
    	// 2 获取文件详情
    	RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
    		
    	while(listFiles.hasNext()){
    		LocatedFileStatus status = listFiles.next();
    			
    		// 输出详情
    		// 文件名称
    		System.out.println(status.getPath().getName());
    		// 长度
    		System.out.println(status.getLen());
    		// 权限
    		System.out.println(status.getPermission());
    		// 分组
    		System.out.println(status.getGroup());
    			
    		// 获取存储的块信息
    		BlockLocation[] blockLocations = status.getBlockLocations();
    			
    		for (BlockLocation blockLocation : blockLocations) {
    				
    			// 获取块存储的主机节点
    			String[] hosts = blockLocation.getHosts();
    				
    			for (String host : hosts) {
    				System.out.println(host);
    			}
    		}
    			
    		System.out.println("-----------班长的分割线----------");
    	}
    
    // 3 关闭资源
    fs.close();
    }
    
  10. HDFS 文件和文件夹的判断

    @Test
    public void testListStatus() throws IOException, InterruptedException, URISyntaxException{
    		
    	// 1 获取文件配置信息
    	Configuration configuration = new Configuration();
    	FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9820"), configuration, "muxue");
    		
    	// 2 判断是文件还是文件夹
    	FileStatus[] listStatus = fs.listStatus(new Path("/"));
    		
    	for (FileStatus fileStatus : listStatus) {
    		
    		// 如果是文件
    		if (fileStatus.isFile()) {
    				System.out.println("f:"+fileStatus.getPath().getName());
    			}else {
    				System.out.println("d:"+fileStatus.getPath().getName());
    			}
    		}
    		
    	// 3 关闭资源
    	fs.close();
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值