一、HDFS 概念

1、HDFS 是一个分布式文件系统。适合一次写入,多次读出的场景,不支持文件修改;适合用来做数据分析,但不适合用来做网盘。
2、 由nameNode 、dataNode和secondarynameNode组成。
3、nameNode负责管理整个文件系统的元数据,及每个文件对应的数据块信息。
4、dataNode负责管理文件数据块,且一个文件数据块可能存在多个datanode中。
5、secondarynodeName作为nameNode的备用,没隔一段时间复制HDFS元数据快照,同时负责监控hdfs的状态。
6、 数据块大小:

  • hdfs文件物理上分块存储
  • Hadoop2.x默认大小是128M,老版本是64M
  • 块越大寻找时间越小。
  • 磁盘寻址时间仅占传输时间的1%时,状态最佳,此时块大小为100M(磁盘读取速度100M/s),而官方设置为128M,这大概和二进制表示相关(2的整数次幂)。

二、HDFS文件系统操作命令

1、 查看命令ls的参数

hadoop fs –help ls

2、 查看目录/user

hadoop fs –ls  /user

hadoop fs ls时间排序命令 hdfs文件按时间排序_hdfs


hadoop fs ls时间排序命令 hdfs文件按时间排序_hadoop_02


3、 创建目录hello

hadoop fs –mkdir  /user/hello

hadoop fs ls时间排序命令 hdfs文件按时间排序_文件系统_03


4、将本地文件移动到hdfs

hadoop fs  -moveFromLocal  lys.txt  /user/hello ll

hadoop fs ls时间排序命令 hdfs文件按时间排序_hadoop fs ls时间排序命令_04


5、 将hdfs文件移动到本地(暂未实现)

hadoop fs -moveToLocal  /user/hello/hellotxt   ./
moveToLocal: Option '-moveToLocal' is not implemented yet.

6、 将一个文件的内容追加到另一个文件的末尾

hadoop fs   –appendToFile   hello1.txt  /user/hello/hello.txt

7、 查看文件内容

hadoop fs –cat  /user/hello/hello.txt

8、 从尾部查看

hadoop fs –tail  /user/hello/hello.txt

9、 从hdfs 中拷贝文件到本地

hadoop fs –copyToLocal   /user/hello/lys.txt  ./

10、 从本地复制文件到hdfs

hadoop fs –copyFromLocal  ./hello2.txt    /user/hello/

11、 hdfs中文件拷贝

hadoop fs –cp  /user/hello/hello.txt     /user

12、hadoop fs -get 从hdfs下载

hadoop fs   -get  /user/hello/hello2.txt     ./

13、 将文件合并下载到本地

hadoop fs  –getmerge /user/hello/*.txt   ./new.txt

14、hadoop fs -put 上传到hdfs

hadoop fs   -put ./hello.txt   /user/hello

15、 删除文件(文件夹)

hadoop fs –rm  /user/hello/*.txt

16、hadoop fs –rmdir /user/hello 删除空目录

hadoop fs –rmdir  /user/hello

17、 查看文件系统可用空间

hadoop fs –df   [-h]

hadoop fs ls时间排序命令 hdfs文件按时间排序_hadoop_05


18、 统计文件夹大小

hadoop fs -du   [-s]  /user

19、 统计目录树高,和文件总数

hadoop fs –count  –h  /user

hadoop fs ls时间排序命令 hdfs文件按时间排序_文件系统_06


20、修改文件所属的权限:将文件的owner改为lys,文件的group改为lys

hadoop fs -chown  lys:lys  /user/hello/hello.txt

21、文件权限修改

u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示这三者皆是。
+ 表示增加权限、- 表示取消权限。
r 表示可读取,w 表示可写入,x 表示可执行。
  • 给所有人添加读写执行权限
hadoop fs -chmod  777  /user/hello/hello.txt
 hadoop fs -chmod  ugo+rwx  /user/hello/hello.txt
  • 取消可执行权限
hadoop fs -chmod  666  /user/hello/hello.txt
   hadoop fs -chmod  ugo-x  /user/hello/hello.txt
  • 文件拥有者添加写权限
hadoop fs -chmod  u+w,go-w  /user/hello/hello.txt

总结:HDFS文件系统的操作命令和Linux文件系统操作类似,只需多添加前缀 “hadoop fs –” 即可。