Hadoop3.x基础入门(一)

771 阅读7分钟

Hadoop的三大发行版本

  • Apache
  • Cloudera
    • CDH(Cloudera发行的开源Hadoop,比Apache Hadoop在兼容性、稳定性、安全性有所增强)
    • 开发贡献了可实时处理大数据的Impala
  • Hortonworks(已经与Cloudera合并)
    • HDP(Hortonworks Data Platform,开源,还包括了Ambari(一款开源的安装和管理系统))

Hadoop的架构模块介绍

  1. 分布式存储HDFS(Hadoop Distributed File System) - 主从架构
    • NameNode: 主节点,主要负责HDFS集群的管理以及元数据信息管理
    • DataNode: 从节点,主要负责存储用户数据
    • SecondaryNameNode: 辅助NameNode管理元数据信息,以及元数据信息的冷备份(非必需存在)
  2. 分布式计算MapReduce
  3. 资源调度引擎Yarn(Yet Another Resource Negotiator) - 主从架构
    • ResourceManager: 主节点,主要负责资源分配
    • NodeManager: 从节点,主要负责执行任务

HDFS架构详解

一句话,靠着集体的力量干大事!

  1. 分块存储(Block)

    • 一般默认为按照128M的大小对文件进行切分,进而存储(Hadoop1.x默认Block大小是64M)
    • 可在hdfs-site.xml进行配置文件修改
    <property>
    	<name>dfs.blocksize</name>
    	<value>(Unit: Byte)</value> <!-- 只写数值就可以 -->
    </property>
    
    • Block元数据:每个Block的元数据大小大概为150 Bytes。
    • 面对不足128M的Block进行存储时,仍按照原大小进行存储
    eg. 200M = 128M + 72M
    First Block: 128M
    Second Block: 72M
    问:用了多少内存进行存储200M(不考虑副本)? 用了200M+元数据相关内存(大概为150字节)。
    
  2. 三个副本的存储

    • 为了保证Block的安全性(数据的安全性),在Hadoop3.x中默认保存三个副本
    • 可在hdfs-site.xml进行配置文件修改
    <property>
    	<name>dfs.replication</name>
        	<value>3</value>
    </property>
    
  3. 机架感知

Replica Placement: The First Baby Steps

Large HDFS instances run on a cluster of computers that commonly spread across many racks. Communication between two nodes in different racks has to go through switches. In most cases, network bandwidth between machines in the same rack is greater than network bandwidth between machines in different racks. 理解:在大多数情况下,同一机架的机器之间的网络带宽大于不同机架的机器之间的网络带宽。

The NameNode determines the rack id each DataNode belongs to via the process outlined in Hadoop Rack Awareness. A simple but non-optimal policy is to place replicas on unique racks. This prevents losing data when an entire rack fails and allows use of bandwidth from multiple racks when reading data. This policy evenly distributes replicas in the cluster which makes it easy to balance load on component failure. However, this policy increases the cost of writes because a write needs to transfer blocks to multiple racks. 理解:NameNode通过Hadoop机架感知中列出的进程可以确定每个DataNode所属的机架号。一个简单但不是最优的策略是将副本们放在不同的机架上。这可以防止在整个机架故障时丢失数据,并允许在读取数据时使用来自多个机架的带宽。该策略在集群中可以均匀地分布副本,使得在组件故障时很容易平衡负载。但是,这个策略增加了写的成本,因为写入需要将块传输到多个机架上。

For the common case, when the replication factor is three, HDFS’s placement policy is to put one replica on the local machine if the writer is on a datanode, otherwise on a random datanode, another replica on a node in a different (remote) rack, and the last on a different node in the same remote rack. This policy cuts the inter-rack write traffic which generally improves write performance. The chance of rack failure is far less than that of node failure; this policy does not impact data reliability and availability guarantees. However, it does reduce the aggregate network bandwidth used when reading data since a block is placed in only two unique racks rather than three. With this policy, the replicas of a file do not evenly distribute across the racks. One third of replicas are on one node, two thirds of replicas are on one rack, and the other third are evenly distributed across the remaining racks. This policy improves write performance without compromising data reliability or read performance. 理解:常见的复制因子为三(即拷贝三个副本)。HDFS的安置政策是将一个副本放在本地机器上如果写入者是在一个datanode上,否则就要随机选择一个datanode。另一个副本放在一个不同(远程)机架的一个节点上,最后一个副本放在同一个远程机架的一个不同的节点上。该策略可以减少机架间的写流量,从而提高写性能。机架失效的概率远小于节点失效的概率,所以该策略不影响数据的可靠性和可用性。然而,它确实减少了读取数据时使用的总网络带宽,因为一个块只放在两个机架中,而不是之前说的三个。所以使用此策略,文件的副本不会均匀地分布在机架上。但是这个策略在不影响数据可靠性和读性能的前提下,提高了写性能。

If the replication factor is greater than 3, the placement of the 4th and following replicas are determined randomly while keeping the number of replicas per rack below the upper limit (which is basically (replicas - 1) / racks + 2). 理解:如果复制因子大于3,第4个副本的放置是随机确定的,但是同时要保持每个机架的副本数量低于上限( (副本数- 1) / 机架数 + 2)。

eg. 副本数:4 机架数: 2

(4 - 1) / 2 + 2 = 1 + 2 = 3

Because the NameNode does not allow DataNodes to have multiple replicas of the same block, maximum number of replicas created is the total number of DataNodes at that time. 理解:创建的最大副本数为同一时刻datanode的总数。

  1. NameNode与DataNode
    • NameNode:
      1. 存储元数据
      2. 元数据保存在内存中
      3. 保存文件、Block、DataNode之间的映射关系
    • DataNode:
      1. 存储文件内容
      2. 文件内容保存在硬盘上
      3. 维护了BlockId和DataNode本地文件的映射关系

The NameNode makes all decisions regarding replication of blocks. It periodically receives a Heartbeat and a Blockreport from each of the DataNodes in the cluster. Receipt of a Heartbeat implies that the DataNode is functioning properly. A Blockreport contains a list of all blocks on a DataNode. 理解: NameNode决定所有关于块复制的事情。它定期接收集群内每个DataNode节点的心跳和块报告。收到心跳就表示这个DataNode在正常工作。块报告中包含一个DataNode上所有块的列表。

HDFS的Shell命令操作

  1. 查看子命令的帮助信息
    hdfs dfs -help ls
    
  2. 查看HDFS文件系统中指定目录的文件列表
    hdfs dfs -ls /
    hdfs dfs -ls -R /
    
  3. 在HDFS文件系统中创建文件
    hdfs dfs -touchz /xxx.txt
    
  4. 向HDFS文件中追加内容
    hdfs dfs -appendToFile xxx.xml /xxx.txt
    #将本地当前目录中的xxx.xml文件中的内容加入到HDFS根目录的xxx.txt文件中
    
  5. 查看HDFS文件内容
    hdfs dfs -cat /xxx.txt
    hdfs dfs -text /xxx.txt
    
  6. 从本地路径上传文件至HDFS
    hdfs dfs -put source destination
    hdfs dfs -copyFromLocal source destination
    #与put作用相同
    hdfs dfs -moveFromLocal source destination
    #与put作用几乎相同,但是source拷贝成功之后,会被删除
    
  7. 在hdfs文件系统中下载文件
    hdfs dfs -get source destination
    hdfs dfs -copyToLocal source destination
    #与get作用相同
    
  8. 在hdfs文件系统中创建目录
    hdfs dfs -mkdir /xxx
    
  9. 在hdfs文件系统中删除文件
    hdfs dfs -rm /xxx.txt
    #文件删除之后会放在垃圾桶中
    #要想彻底删除(慎重)
    hdfs dfs -rm -skipTrash /xxx.txt
    
  10. 在hdfs文件系统中修改文件名称(也可以用来移动文件到目录中)
    hdfs dfs -mv sourcefile destinationfile
    hdfs dfs -mv sourcefile destinationfolder
    
  11. 在HDFS中拷贝文件到目录
    hdfs dfs -cp sourcefile destinationfolder
    
  12. 递归删除目录
    hdfs dfs -rm -r destinationfolder
    
  13. 查找文件
    hdfs dfs -find / -name filename
    

HDFS的安全模式

  • 安全模式:只接受读请求,不接受写请求
  • NameNode主节点启动时,HDFS会首先进入安全模式,一般HDFS集群刚启动时也会有一个默认30S的安全期,但是具体何时结束?
  • 何时结束安全期
    • NameNode知道总共有多少个Block(Total)
    • NameNode启动后会上报Block Report(Num)
    • Num / Total > 99.9%时,退出安全模式
[hadoop@node hadoop]$ hdfs dfsadmin -safemode  
Usage: hdfs dfsadmin [-safemode enter | leave | get | wait]