第7章 HDFS 2.X新特性

7.1 集群间数据拷贝

1scp实现两个远程主机之间的文件复制

       scp -r hello.txt root@hadoop103:/user/atguigu/hello.txt             // push

       scp -r root@hadoop103:/user/atguigu/hello.txt  hello.txt          // pull

       scp -r root@hadoop103:/user/atguigu/hello.txt root@hadoop104:/user/atguigu   //是通过本地主机中转实现两个远程主机的文件复制;如果在两个远程主机之间ssh没有配置的情况下可以使用该方式。

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

[atguigu@hadoop102 hadoop-2.7.2]$  bin/hadoop distcp

hdfs://haoop102:9000/user/atguigu/hello.txt hdfs://hadoop103:9000/user/atguigu/hello.txt

image.png

3.案例实操

1)需要启动YARN进程

[atguigu@hadoop102 hadoop-2.7.2]$ start-yarn.sh

2)归档文件

       /user/atguigu/input目录里面的所有文件归档成一个叫input.har的归档文件,并把归档后文件存储到/user/atguigu/output路径下。

[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop archive -archiveName input.har –p  /user/atguigu/input   /user/atguigu/output

3)查看归档

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -lsr /user/atguigu/output/input.har

 

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -lsr har:///user/atguigu/output/input.har

4)解归档文件

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -cp har:/// user/atguigu/output/input.har/*    /user/atguigu

7.3 回收站

开启回收站功能,可以将删除的文件在不超时的情况下,恢复原数据,起到防止误删除、备份等作用。


1.回收站参数设置及工作机制

image.png

2.启用回收站

修改core-site.xml,配置垃圾回收时间为1分钟。

<property>

   <name>fs.trash.interval</name>

<value>1</value>

</property>

3.查看回收站

回收站在集群中的路径:/user/atguigu/.Trash/….

4.修改访问垃圾回收站用户名称

       进入垃圾回收站用户名称,默认是dr.who,修改为atguigu用户

       [core-site.xml]

<property>

  <name>hadoop.http.staticuser.user</name>

  <value>atguigu</value>

</property>

5.    通过程序删除的文件不会经过回收站,需要调用moveToTrash()才进入回收站

Trash trash = New Trash(conf);

trash.moveToTrash(path);

6. 恢复回收站数据

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -mv

/user/atguigu/.Trash/Current/user/atguigu/input    /user/atguigu/input

7. 清空回收站

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -expunge

7.4 快照管理

image.png

2.案例实操

       1)开启/禁用指定目录的快照功能

[atguigu@hadoop102 hadoop-2.7.2]$ hdfs dfsadmin -allowSnapshot /user/atguigu/input

 

[atguigu@hadoop102 hadoop-2.7.2]$ hdfs dfsadmin -disallowSnapshot /user/atguigu/input

       2)对目录创建快照

[atguigu@hadoop102 hadoop-2.7.2]$ hdfs dfs -createSnapshot /user/atguigu/input

通过web访问hdfs://hadoop102:50070/user/atguigu/input/.snapshot/s…..// 快照和源文件使用相同数据

[atguigu@hadoop102 hadoop-2.7.2]$ hdfs dfs -lsr /user/atguigu/input/.snapshot/

       3)指定名称创建快照

[atguigu@hadoop102 hadoop-2.7.2]$ hdfs dfs -createSnapshot /user/atguigu/input  miao170508

       4)重命名快照

[atguigu@hadoop102 hadoop-2.7.2]$ hdfs dfs -renameSnapshot /user/atguigu/input/  miao170508 atguigu170508

       5)列出当前用户所有可快照目录

[atguigu@hadoop102 hadoop-2.7.2]$ hdfs lsSnapshottableDir

       6)比较两个快照目录的不同之处

[atguigu@hadoop102 hadoop-2.7.2]$ hdfs snapshotDiff

 /user/atguigu/input/  .  .snapshot/atguigu170508 

       7)恢复快照

[atguigu@hadoop102 hadoop-2.7.2]$ hdfs dfs -cp

/user/atguigu/input/.snapshot/s20170708-134303.027 /user