本文共 15791 字,大约阅读时间需要 52 分钟。
1)HDFS 产生背景
随着数据量越来越大,在一个操作系统存不下所有的数据,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,迫切需要一种系统来管理多台机器上的文件,这就是分布式文件管理系统。HDFS 只是分布式文件管理系统中的一种。 2)HDFS 定义 HDFS(Hadoop Distributed File System),它是一个文件系统,用于存储文件,通过目录树来定位文件;其次,它是分布式的,由很多服务器联合起来实现其功能,集群中的服务器有各自的角色。 HDFS 的使用场景:适合一次写入,多次读出的场景。一个文件经过创建、写入和关闭 之后就不需要改变。hadoop fs 具体命令 OR hdfs dfs 具体命令
两个是完全相同的。[oldlu@hadoop102 hadoop-3.1.3]$ bin/hadoop fs[-appendToFile... ] [-cat [-ignoreCrc] ...] [-chgrp [-R] GROUP PATH...] [-chmod [-R] PATH...] [-chown [-R] [OWNER][:[GROUP]] PATH...] [-copyFromLocal [-f] [-p] ... ] [-copyToLocal [-p] [-ignoreCrc] [-crc] ... ] [-count [-q] ...] [-cp [-f] [-p] ... ] [-df [-h] [ ...]] [-du [-s] [-h] ...] [-get [-p] [-ignoreCrc] [-crc] ... ] [-getmerge [-nl] ] [-help [cmd ...]] [-ls [-d] [-h] [-R] [ ...]] [-mkdir [-p] ...] [-moveFromLocal ... ] [-moveToLocal ] [-mv ... ] [-put [-f] [-p] ... ] [-rm [-f] [-r|-R] [-skipTrash] ...] [-rmdir [--ignore-fail-on-non-empty] ...] ]] [-setrep [-R] [-w] ...][-stat [format] ...] [-tail [-f] ] [-test -[defsz] ] [-text [-ignoreCrc] ...]
1)启动 Hadoop 集群(方便后续的测试)
[oldlu@hadoop102 hadoop-3.1.3]$ sbin/start-dfs.sh[oldlu@hadoop103 hadoop-3.1.3]$ sbin/start-yarn.sh
中间遇到很多坑需要总结下
最后启动成功2)-help:输出这个命令参数
[oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -help rm
3)创建/sanguo 文件夹
[oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -mkdir /sanguo
1)-moveFromLocal:从本地剪切粘贴到 HDFS
[oldlu@hadoop102 hadoop-3.1.3]$ vim shuguo.txt
输入:
shuguo[oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -moveFromLocal ./shuguo.txt /sanguo
2)-copyFromLocal:从本地文件系统中拷贝文件到 HDFS 路径去
[oldlu@hadoop102 hadoop-3.1.3]$ vim weiguo.txt输入:weiguo[oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -copyFromLocal weiguo.txt /sanguo
3)-put:等同于 copyFromLocal,生产环境更习惯用 put
[oldlu@hadoop102 hadoop-3.1.3]$ vim wuguo.txt 输入:wuguo[oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -put ./wuguo.txt /sanguo
4)-appendToFile:追加一个文件到已经存在的文件末尾
[oldlu@hadoop102 hadoop-3.1.3]$ vim liubei.txt输入:liubei[oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo.txt
1)-copyToLocal:从 HDFS 拷贝到本地
[oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -copyToLocal /shuguo.txt 2)-get:等同于 copyToLocal,生产环境更习惯用 get [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -get /sanguo/shuguo.txt ./shuguo2.txt 2.3.4 HDFS 直接操作 1)-ls: 显示目录信息 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -ls /sanguo 2)-cat:显示文件内容 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -cat /sanguo/shuguo.txt 3)-chgrp、-chmod、-chown:Linux 文件系统中的用法一样,修改文件所属权限 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -chmod 666 /sanguo/shuguo.txt [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -chown oldlu:oldlu /sanguo/shuguo.txt 4)-mkdir:创建路径 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -mkdir /jinguo 5)-cp:从 HDFS 的一个路径拷贝到 HDFS 的另一个路径 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -cp /sanguo/shuguo.txt /jinguo 6)-mv:在 HDFS 目录中移动文件 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -mv /sanguo/wuguo.txt /jinguo [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -mv /sanguo/weiguo.txt /jinguo 7)-tail:显示一个文件的末尾 1kb 的数据 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -tail /jinguo/shuguo.txt 8)-rm:删除文件或文件夹 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -rm /sanguo/shuguo.txt 9)-rm -r:递归删除目录及目录里面内容 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -rm -r /sanguo 10)-du 统计文件夹的大小信息 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -du -s -h /jinguo 27 81 /jinguo [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -du -h /jinguo 14 42 /jinguo/shuguo.txt 7 21 /jinguo/weiguo.txt 6 18 /jinguo/wuguo.tx 说明:27 表示文件大小;81 表示 27*3 个副本;/jinguo 表示查看的目录 11)-setrep:设置 HDFS 中文件的副本数量 [oldlu@hadoop102 hadoop-3.1.3]$ hadoop fs -setrep 10 /jinguo/shuguo.txt1)找到资料包路径下的 Windows 依赖文件夹,拷贝 hadoop-3.1.0 到非中文路径(比如 d:\)。 2)配置 HADOOP_HOME 环境变量
1)编写源代码
@Test public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException { // 1 获取文件系统 Configuration configuration = new Configuration(); configuration.set(“dfs.replication”, “2”); FileSystem fs = FileSystem.get(new URI(“hdfs://hadoop102:8020”), configuration, “oldlu”); // 2 上传文件 fs.copyFromLocalFile(new Path(“d:/sunwukong.txt”), new Path("/xiyou/huaguoshan")); // 3 关闭资源 fs.close(); } 2)将 hdfs-site.xml 拷贝到项目的 resources 资源目录下 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> dfs.replication 1 3)参数优先级 参数优先级排序:(1)客户端代码中设置的值 >(2)ClassPath 下的用户自定义配置文 件 >(3)然后是服务器的自定义配置(xxx-site.xml)>(4)服务器的默认配置(xxx-default.xml)@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI(“hdfs://hadoop102:8020”), configuration, “oldlu”);// 2 执行下载操作
// boolean delSrc 指是否将原文件删除 // Path src 指要下载的文件路径 // Path dst 指将文件下载到的路径 // boolean useRawLocalFileSystem 是否开启文件校验 fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path(“d:/sunwukong2.txt”), true);// 3 关闭资源
fs.close(); } 注意:如果执行上面代码,下载不了文件,有可能是你电脑的微软支持的运行库少,需 要安装一下微软运行库。@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI(“hdfs://hadoop102:8020”), configuration, “oldlu”); // 2 修改文件名称 fs.rename(new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("/xiyou/huaguoshan/meihouwang.txt")); // 3 关闭资源 fs.close(); }@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI(“hdfs://hadoop102:8020”), configuration, “oldlu”); // 2 执行删除 fs.delete(new Path("/xiyou"), true); // 3 关闭资源 fs.close(); }查看文件名称、权限、长度、块信息
@Test public void testListFiles() throws IOException, InterruptedException, URISyntaxException { // 1 获取文件系统 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI(“hdfs://hadoop102:8020”), configuration, “oldlu”); // 2 获取文件详情 RemoteIterator listFiles = fs.listFiles(new Path("/"), true); while (listFiles.hasNext()) { LocatedFileStatus fileStatus = listFiles.next(); System.out.println("" + fileStatus.getPath() + "="); System.out.println(fileStatus.getPermission()); System.out.println(fileStatus.getOwner()); System.out.println(fileStatus.getGroup()); System.out.println(fileStatus.getLen()); System.out.println(fileStatus.getModificationTime()); System.out.println(fileStatus.getReplication()); System.out.println(fileStatus.getBlockSize()); System.out.println(fileStatus.getPath().getName()); // 获取块信息 BlockLocation[] blockLocations = fileStatus.getBlockLocations(); System.out.println(Arrays.toString(blockLocations)); } // 3 关闭资源 fs.close(); }@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{ // 1 获取文件配置信息 Configuration configuration = new Configuration(); FileSystem fs = FileSystem.get(new URI(“hdfs://hadoop102:8020”), configuration, “oldlu”); // 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(); }在 HDFS 写数据的过程中,NameNode 会选择距离待上传数据最近距离的 DataNode 接
收数据。那么这个最近距离怎么计算呢? 节点距离:两个节点到达最近的共同祖先的距离总和。1)机架感知说明
(1)官方说明 http://hadoop.apache.org/docs/r3.1.3/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html#Data_Replication 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. (2)源码说明 Crtl + n 查找 BlockPlacementPolicyDefault,在该类中查找 chooseTargetInOrder 方法。 2)Hadoop3.1.3 副本节点选择思考:NameNode 中的元数据是存储在哪里的?
首先,我们做个假设,如果存储在 NameNode 节点的磁盘中,因为经常需要进行随机访 问,还有响应客户请求,必然是效率过低。因此,元数据需要存放在内存中。但如果只存在 内存中,一旦断电,元数据丢失,整个集群就无法工作了。因此产生在磁盘中备份元数据的 FsImage。 这样又会带来新的问题,当在内存中的元数据更新时,如果同时更新 FsImage,就会导 致效率过低,但如果不更新,就会发生一致性问题,一旦 NameNode 节点断电,就会产生数 据丢失。因此,引入 Edits 文件(只进行追加操作,效率很高)。每当元数据有更新或者添 加元数据时,修改内存中的元数据并追加到 Edits 中。这样,一旦 NameNode 节点断电,可 以通过 FsImage 和 Edits 的合并,合成元数据。 但是,如果长时间添加数据到 Edits 中,会导致该文件数据过大,效率降低,而且一旦 断电,恢复元数据需要的时间过长。因此,需要定期进行 FsImage 和 Edits 的合并,如果这 个操作由NameNode节点完成,又会效率过低。因此,引入一个新的节点SecondaryNamenode, 专门用于 FsImage 和 Edits 的合并。Fsimage和Edits概念
思考:如果电脑磁盘里面存储的数据是控制高铁信号灯的红灯信号(1)和绿灯信号(0),
但是存储该数据的磁盘坏了,一直显示是绿灯,是否很危险?同理 DataNode 节点上的数据 损坏了,却没有发现,是否也很危险,那么如何解决呢? 如下是 DataNode 节点保证数据完整性的方法。 (1)当 DataNode 读取 Block 的时候,它会计算 CheckSum。 (2)如果计算后的 CheckSum,与 Block 创建时值不一样,说明 Block 已经损坏。 (3)Client 读取其他 DataNode 上的 Block。 (4)常见的校验算法 crc(32),md5(128),sha1(160) (5)DataNode 在其文件创建后周期验证 CheckSum。转载地址:http://xqzr.baihongyu.com/