hdfs common operation commands

  Let's talk about " the difference between hadoop fs  and hadoop dfs " first. I read the two Hadoop books that are useful, but the effect is the same. The following explanation is more pertinent for verification and network discovery.

  Roughly speaking, fs is a relatively abstract level. In a distributed environment, fs is dfs, but in a local environment, fs is a local file system, and dfs cannot be used at this time.

5.1 File Operations

  1) List HDFS files

  Here is how to list files under HDFS with "-ls" command:

hadoop fs -ls

  The execution result is shown in Figure 5-1-1. It should be noted here: the "-ls" name without parameters in HDFS does not return any value, it defaults to the content of the " home " directory in HDFS. In HDFS, there is no such a concept as the current directory , and there is no cd command.

  

Figure 5-1-1 List HDFS files

  2) List the files in a document in the HDFS directory

  Here's how to browse files in a document named "input" in HDFS with the "-ls filename" command:

hadoop fs –ls input

  The execution result is shown in Figure 5-1-2.

  

Figure 5-1-2 List the files under the document named input under HDFS

  3) Upload files to HDFS

  此处为你展示如何通过"-put 文件1 文件2"命令将"Master.Hadoop"机器下的"/home/hadoop"目录下的file文件上传到HDFS上并重命名test

hadoop fs –put ~/file test

  执行结果如图5-1-3所示。在执行"-put"时两种可能,即是执行成功执行失败。在上传文件时,文件首先复制到DataNode上。只有所有的DataNode都成功接收完数据,文件上传才是成功的。其他情况(如文件上传终端等)对HDFS来说都是做了无用功。

  

图5-1-3 成功上传file到HDFS

  4)将HDFS中文件复制本地系统

  此处为你展示如何通过"-get 文件1 文件2"命令将HDFS中的"output"文件复制到本地系统并命名为"getout"。

hadoop fs –get output getout

  执行结果如图5-1-4所示。

  

图5-1-4 成功将HDFS中output文件复制到本地系统

  备注:与"-put"命令一样,"-get"操作既可以操作文件,也可以操作目录

  5)删除HDFS下的文档

  此处为你展示如何通过"-rmr 文件"命令删除HDFS下名为"newoutput"的文档:

hadoop fs –rmr newoutput

  执行结果如图5-1-5所示。

  

图5-1-5 成功删除HDFS下的newoutput文档

  6)查看HDFS下某个文件

  此处为你展示如何通过"-cat 文件"命令查看HDFS下input文件中内容:

hadoop fs -cat input/*

  执行结果如图5-1-6所示。

  

图5-1-6 HDFS下input文件的内容

  "hadoop fs"的命令远不止这些,本小节介绍的命令已可以在HDFS上完成大多数常规操作。对于其他操作,可以通过"-help commandName"命令所列出的清单来进一步学习与探索。

5.2 管理与更新

  1)报告HDFS的基本统计情况

  此处为你展示通过"-report"命令如何查看HDFS的基本统计信息:

hadoop dfsadmin -report

  执行结果如图5-2-1所示。

  

图5-2-1 HDFS基本统计信息

  2)退出安全模式

  NameNode在启动时自动进入安全模式。安全模式是NameNode的一种状态,在这个阶段,文件系统不允许有任何修改。安全模式的目的是在系统启动时检查各个DataNode上数据块的有效性,同时根据策略对数据块进行必要的复制删除,当数据块最小百分比数满足的最小副本数条件时,会自动退出安全模式。

  系统显示"Name node is in safe mode",说明系统正处于安全模式,这时只需要等待17秒即可,也可以通过下面的命令退出安全模式:

hadoop dfsadmin –safemode enter

  成功退出安全模式结果如图5-2-2所示。

  

图5-2-2 成功退出安全模式

  3)进入安全模式

  在必要情况下,可以通过以下命令把HDFS置于安全模式:

hadoop dfsadmin –safemode enter

  执行结果如图5-2-3所示。

  

图5-2-3 进入HDFS安全模式

  4)添加节点

  可扩展性是HDFS的一个重要特性,向HDFS集群中添加节点是很容易实现的。添加一个新的DataNode节点,首先在新加节点上安装好Hadoop,要和NameNode使用相同的配置(可以直接从NameNode复制),修改"/usr/hadoop/conf/master"文件,加入NameNode主机名。然后在NameNode节点上修改"/usr/hadoop/conf/slaves"文件,加入新节点主机名,再建立到新加点无密码的SSH连接,运行启动命令:

start-all.sh

  5)负载均衡

  HDFS的数据在各个DataNode中的分布肯能很不均匀,尤其是在DataNode节点出现故障新增DataNode节点时。新增数据块时NameNode对DataNode节点的选择策略也有可能导致数据块分布的不均匀。用户可以使用命令重新平衡DataNode上的数据块的分布:

start-balancer.sh

  执行命令前,DataNode节点上数据分布情况如图5-2-4所示。

  

  负载均衡完毕后,DataNode节点上数据的分布情况如图5-2-5所示。

  

  执行负载均衡命令如图5-2-6所示。

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939702&siteId=291194637