4_大数据之Hadoop(HDFS)

HDFS简介
  1. HDFS产出背景以及定义
    2.HDFS优缺点
    3. HDFS组成架构
    4. HDFS文件块大小

HDFSShell操作
  1. 基本语法
    bin/hadoop fs具体命令 OR bin/hdfs dfs具体命令
    dfsfs的实现类。
  2. 常用命令实操
    2.1 启动Hadoop集群(方便后续的测试)
sbin/start-dfs.sh
sbin/start-yarn.sh

 2.2 -help:输出这个命令参数

hadoop fs -help rm

 2.3 -ls: 显示目录信息

hadoop fs -ls /

 2.4. -mkdir:在HDFS上创建目录

hadoop fs -mkdir -p /sanguo/shuguo

 2.5 -moveFromLocal:从本地剪切粘贴到HDFS

hadoop fs  -moveFromLocal  ./kongming.txt  /sanguo/shuguo

 2.6 -appendToFile:追加一个文件到已经存在的文件末尾

hadoop fs -appendToFile liubei.txt /sanguo/shuguo/kongming.txt

 2.7 -cat:显示文件内容

hadoop fs -cat /sanguo/shuguo/kongming.txt

 2.8 -chgrp-chmod-chownLinux文件系统中的用法一样,修改文件所属权限

hadoop fs  -chmod  666  /sanguo/shuguo/kongming.txt
hadoop fs  -chown  xxx:xxx   /sanguo/shuguo/kongming.txt

 2.9 -copyFromLocal:从本地文件系统中拷贝文件到HDFS路径去

hadoop fs -copyFromLocal README.txt /

 2.10 -copyToLocal:从HDFS拷贝到本地

hadoop fs -copyToLocal /sanguo/shuguo/kongming.txt ./

 2.11 -cp :从HDFS的一个路径拷贝到HDFS的另一个路径

hadoop fs -cp /sanguo/shuguo/kongming.txt /zhuge.txt

 2.12 -mv:在HDFS目录中移动文件

hadoop fs -mv /zhuge.txt /sanguo/shuguo/

 2.13 -get:等同于copyToLocal,就是从HDFS下载文件到本地

hadoop fs -get /sanguo/shuguo/kongming.txt ./

 2.14 -getmerge:合并下载多个文件,比如HDFS的目录 /user/xxx/test下有多个文件:log.1, log.2,log.3,...

hadoop fs -getmerge /user/xxx/test/* ./zaiyiqi.txt

 2.15 -put:等同于copyFromLocal

hadoop fs -put ./zaiyiqi.txt /user/xxx/test/

 2.16 -tail:显示一个文件的末尾

hadoop fs -tail /sanguo/shuguo/kongming.txt

 2.17 -rm:删除文件或文件夹

hadoop fs -rm /user/xxx/test/jinlian2.txt

 2.18 -rmdir:删除空目录

hadoop fs -rmdir /test

 2.19 -du : 统计文件夹的大小信息

hadoop fs -du -s -h /user/xxx/test

 2.20 -setrep:设置HDFS中文件的副本数量

hadoop fs -setrep 10 /sanguo/shuguo/kongming.txt
`这里设置的副本数只是记录在NameNode的元数据中,是否真的会有这么多副本,还得看DataNode的数量。`
`因为目前只有3台设备,最多也就3个副本,只有节点数的增加到10台时,副本数才能达到10。`

HDFS客户端操作

1️⃣HDFS客户端环境准备
 1.根据自己电脑的操作系统拷贝对应的编译后的hadoop jar包到非中文路径(例如:D:\Develop\hadoop-2.7.2
 2.配置HADOOP_HOME环境变量
 3.配置Path环境变量
 4.创建一个Maven工程HdfsClientDemo
 5.导入相应的依赖坐标+日志添加

<dependencies>
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>RELEASE</version>
   </dependency>
   <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-core</artifactId>
       <version>2.8.2</version>
   </dependency>
   <dependency>
       <groupId>org.apache.hadoop</groupId>
       <artifactId>hadoop-common</artifactId>
       <version>2.7.2</version>
   </dependency>
   <dependency>
       <groupId>org.apache.hadoop</groupId>
       <artifactId>hadoop-client</artifactId>
       <version>2.7.2</version>
   </dependency>
   <dependency>
       <groupId>org.apache.hadoop</groupId>
       <artifactId>hadoop-hdfs</artifactId>
       <version>2.7.2</version>
   </dependency>
   <dependency>
      <groupId>jdk.tools</groupId>
       <artifactId>jdk.tools</artifactId>
       <version>1.8</version>
       <scope>system</scope>
       <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
   </dependency>
</dependencies>

注意:如果Eclipse/Idea打印不出日志,在控制台上只显示

1.log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).  
2.log4j:WARN Please initialize the log4j system properly.  
3.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

 需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

 6.创建包名:com.xxx.hdfs
 7.创建HdfsClient

public class HdfsClient{   
@Test
public void testMkdirs() throws IOException, InterruptedException, >URISyntaxException{
      
      // 1 获取文件系统
      Configuration configuration = new Configuration();
      // 配置在集群上运行
      // configuration.set("fs.defaultFS", "hdfs://hadoop102:9000");
      // FileSystem fs = FileSystem.get(configuration);

      FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "xxx");
      
      // 2 创建目录
      fs.mkdirs(new Path("/1108/daxian/banzhang"));
      
      // 3 关闭资源
      fs.close();
  }
}

 8.执行程序运行时需要配置用户名称

 客户端去操作HDFS时,是有一个用户身份的。默认情况下,HDFS客户端API会从JVM中获取一个参数来作为自己的用户身份:-DHADOOP_USER_NAME=xxxxxx为用户名称。

2️⃣HDFSAPI操作
 1. HDFS文件上传

@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:9000"), configuration, "xxx");

      // 2 上传文件
      fs.copyFromLocalFile(new Path("e:/banzhang.txt"), new Path("/banzhang.txt"));

      // 3 关闭资源
      fs.close();

      System.out.println("over");
}

 将hdfs-site.xml拷贝到项目的根目录下

<?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>

 参数优先级排序 : 客户端代码中设置的值 > ClassPath下的用户自定义配置文件 > 然后是服务器的默认配置
 2. HDFS文件下载

@Test
public void testCopyToLocalFile() throws IOException, >InterruptedException, URISyntaxException{

      // 1 获取文件系统
      Configuration configuration = new Configuration();
      FileSystem fs = FileSystem.get(new >URI("hdfs://hadoop102:9000"), configuration, "xxx");
      
      // 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();
}
  1. HDFS文件夹删除
@Test
public void testDelete() throws IOException, InterruptedException, >URISyntaxException{

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

  // 1 获取文件系统
  Configuration configuration = new Configuration();
  FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "xxx"); 
      
  // 2 修改文件名称
  fs.rename(new Path("/banzhang.txt"), new Path("/banhua.txt"));
      
  // 3 关闭资源
  fs.close();
}
  1. HDFS文件详情查看 (查看文件名称、权限、长度、块信息)
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException{

  // 1获取文件系统
  Configuration configuration = new Configuration();
  FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "xxx"); 
      
  // 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();
}
  1. HDFS文件和文件夹判断
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{
      
  // 1 获取文件配置信息
  Configuration configuration = new Configuration();
  FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "xxx");
      
  // 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的数据流

1️⃣HDFS写数据流程
 1. HDFS写数据流程

  1)客户端通过Distributed FileSystem模块向NameNode请求上传文件,NameNode检查目标文件是否已存在,父目录是否存在。
  2)NameNode返回是否可以上传。
  3)客户端请求第一个Block上传到哪几个DataNode服务器上。
  4)NameNode返回3DataNode节点,分别为dn1dn2dn3
  5)客户端通过FSDataOutputStream模块请求dn1上传数据,dn1收到请求会继续调用dn2,然后dn2调用dn3,将这个通信管道建立完成。
  6)dn1dn2dn3逐级应答客户端。
  7)客户端开始往dn1上传第一个Block(先从磁盘读取数据放到一个本地内存缓存),以Packet为单位,dn1收到一个Packet就会传给dn2dn2传给dn3dn1每传一个packet会放入一个应答队列等待应答。
  8)当一个Block传输完成之后,客户端再次请求NameNode上传第二个Block的服务器。(重复执行3-7步)。

 2. 网络拓扑-节点距离计算
  在HDFS写数据的过程中,NameNode会选择距离待上传数据最近距离的DataNode接收数据。那么这个最近距离怎么计算呢?
  节点距离:两个节点到达最近的共同祖先的距离总和。

  例如,假设有数据中心d1机架r1中的节点n1。该节点可以表示为/d1/r1/n1。利用这种标记,这里给出四种距离描述

 3. 机架感知(副本存储节点选择)
  1. 官方说明

For the common case, when the replication factor is three, HDFS’s placement policy is to put one replica on one node in the local rack, another on a different node in the local rack, and the last on a different node in a different rack.

  2. Hadoop2.7.2副本节点选择

2️⃣HDFS读数据流程
 1. HDFS的读数据流程

  1)客户端通过Distributed FileSystemNameNode请求下载文件,NameNode通过查询元数据,找到文件块所在的DataNode地址。
  2)挑选一台DataNode(就近原则,然后随机)服务器,请求读取数据。
  3)DataNode开始传输数据给客户端(从磁盘里面读取数据输入流,以Packet为单位来做校验)。
  4)客户端以Packet为单位接收,先在本地缓存,然后写入目标文件。


HDFS 2.X新特性

1️⃣集群间数据拷贝
 1.scp实现两个远程主机之间的文件复制

# 推 push
scp -r hello.txt [root@hadoop103:/user/xxx/hello.txt](mailto:root@hadoop103%253A/user/xxx/hello.txt)  
# 拉 pull
scp -r [root@hadoop103:/user/xxx/hello.txt hello.txt](mailto:root@hadoop103%253A/user/xxx/hello.txt%2520%2520hello.txt)
# 是通过本地主机中转实现两个远程主机的文件复制;如果在两个远程主机之间ssh没有配置的情况下可以使用该方式。
scp -r [root@hadoop103:/user/xxx/hello.txt](mailto:root@hadoop103%253A/user/xxx/hello.txt) root@hadoop104:/user/xxx

 2.采用distcp命令实现两个Hadoop集群之间的递归数据复制

bin/hadoop distcp hdfs://haoop102:9000/user/xxx/hello.txt hdfs://hadoop103:9000/user/xxx/hello.txt

2️⃣小文件存档

 1. 案例实操

(1)需要启动YARN进程 start-yarn.sh
(2)归档文件,把/user/xxx/input目录里面的所有文件归档成一个叫input.har的归档文件,并把归档后文件存储到/user/xxx/output路径下。

bin/hadoop archive -archiveName input.har –p  /user/xxx/input   /user/xxx/output

(3)查看归档

hadoop fs -lsr /user/xxx/output/input.har
hadoop fs -lsr har:///user/xxx/output/input.har

(4)解归档文件 hadoop fs -cp har:/// user/xxx/output/input.har/* /user/xxx

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272