30最常用的Hadoop HDFS shell命令
在本教程中,我们将浏览Hadoop分布式文件系统(HDFS)命令,我们需要在HDFS上管理文件。
使用Hadoop文件系统时大部分时间使用HDFS命令。
它包括与Hadoop分布式文件系统(HDFS)直接交互的各种shell 的命令以及Hadoop支持的其他文件系统。
大多数命令都表现类似于相应的UNIX命令。
错误信息被发送到stderr,输出发送到stdout。
所以,让我们开始。
1)版本检查
检查Hadoop的版本。
ubuntu@ubuntu-VirtualBox:~$hadoop version Hadoop 2.7.3 Subversion https://git-wip-us.apache.org/repos/asf/hadoop.git -r baa91f7c6bc9cb92be5982de4719c1c8af91ccff Compiled by root on 2015-08-18T01:41Z Compiled with protoc 2.5.0 From source with checksum 2e4ce5f957ea4db193bce3734ff29ff4 This command was run using /home/ubuntu/hadoop-2.7.3/share/hadoop/common/hadoop-common-2.7.3.jar
2)列表命令
列出给定HDFS目标路径的所有文件/目录。
ubuntu@ubuntu-VirtualBox:~ $hdfs dfs -ls / Found 3 items drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:11 /test drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:09 /tmp drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:09 /usr
3)DF命令
在给定HDFS目的地显示可用空间
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -df hdfs:/ Filesystem Size Used Available Use% hdfs://master:9000 6206062592 32768 316289024 0%
4)计数命令
- 计算与指定文件模式匹配的路径下的目录,文件和字节数。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -count hdfs:/ 4 0 0 hdfs:///
5)FSCK命令
HDFS命令检查Hadoop文件系统的运行状况。
ubuntu@ubuntu-VirtualBox:~$hdfs fsck / Connecting to namenode via http://master:50070/fsck?ugi=ubuntu&path=%2F FSCK started by ubuntu (auth:SIMPLE) from /192.168.1.36 for path/at Mon Nov 07 01:23:54 GMT+05:30 2015 Status: HEALTHY Total size: 0 B Total dirs: 4 Total files: 0 Total symlinks: 0 Total blocks (validated): 0 Minimally replicated blocks: 0 Over-replicated blocks: 0 Under-replicated blocks: 0 Mis-replicated blocks: 0 Default replication factor: 2 Average block replication: 0.0 Corrupt blocks: 0 Missing replicas: 0 Number of data-nodes: 1 Number of racks: 1 FSCK ended at Mon Nov 07 01:23:54 GMT+05:30 2015 in 33 milliseconds The filesystem under path '/' is HEALTHY
6)Balancer命令
运行群集平衡实用程序。
ubuntu@ubuntu-VirtualBox:~$hdfs balancer 16/11/07 01:26:29 INFO balancer.Balancer: namenodes = [hdfs://master:9000] 16/11/07 01:26:29 INFO balancer.Balancer: parameters = Balancer.Parameters[BalancingPolicy.Node, threshold=10.0, max idle iteration = 5, number of nodes to be excluded = 0, number of nodes to be included = 0] Time Stamp Iteration# Bytes Already Moved Bytes Left To Move Bytes Being Moved 16/11/07 01:26:38 INFO net.NetworkTopology: Adding a new node: /default-rack/192.168.1.36:50010 16/11/07 01:26:38 INFO balancer.Balancer: 0 over-utilized: [] 16/11/07 01:26:38 INFO balancer.Balancer: 0 underutilized: [] The cluster is balanced. Exiting... 7 Nov, 2015 1:26:38 AM 0 0 B 0 B -1 B 7 Nov, 2015 1:26:39 AM Balancing took 13.153 seconds
7)MKDIR命令
HDFS命令以在HDFS中创建目录。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -mkdir /hadoop ubuntu@ubuntu-VirtualBox:~$hdfs dfs -ls / Found 5 items drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:29 /hadoop drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:26 /system drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:11 /test drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:09 /tmp drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:09 /usr
8)PUT命令
文件
将文件从单个SRC复制到从本地文件系统到目标文件系统的多个SRC。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -put test /hadoop ubuntu@ubuntu-VirtualBox:~$hdfs dfs -ls /hadoop Found 1 items -rw-r--r-- 2 ubuntu supergroup 16 2015-11-07 01:35 /hadoop/test
目录
HDFS命令将目录从单个源复制到从本地文件系统到目标文件系统的多个源。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -put hello /hadoop/ ubuntu@ubuntu-VirtualBox:~$hdfs dfs -ls /hadoop Found 2 items drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:43 /hadoop/hello -rw-r--r-- 2 ubuntu supergroup 16 2015-11-07 01:35 /hadoop/test
9)du命令
显示给定目录中包含的文件和目录的大小或者文件的大小如果只是一个文件。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -du / 59 /hadoop 0 /system 0 /test 0 /tmp 0 /usr
10)RM命令
hdfs命令从hdfs删除文件。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -rm /hadoop/test 16/11/07 01:53:29 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes. Deleted /hadoop/test
11)expunge命令
HDFS命令使垃圾空置为空。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -expunge 16/11/07 01:55:54 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes.
12)RM -R命令
hdfs命令用来从hdfs中删除整个目录和所有内容。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -rm -r /hadoop/hello 16/11/07 01:58:52 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes. Deleted /hadoop/hello
13)CHMOD命令
更改文件的权限。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -chmod 777 /hadoop ubuntu@ubuntu-VirtualBox:~$hdfs dfs -ls / Found 5 items drwxrwxrwx - ubuntu supergroup 0 2015-11-07 01:58 /hadoop drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:26 /system drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:11 /test drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:09 /tmp drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:09 /usr
14)获取命令
hdfs命令将文件从hdfs复制到本地文件系统。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -get /hadoop/test /home/ubuntu/Desktop/ ubuntu@ubuntu-VirtualBox:~$ls -l /home/ubuntu/Desktop/ total 4 -rw-r--r-- 1 ubuntu ubuntu 16 Nov 8 00:47 test
15)CAT命令
HDFS命令将源路径复制到stdout。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -cat /hadoop/test This is a test.
16)Touchz命令
hdfs命令用文件大小为0字节创建HDFS中的文件。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -touchz /hadoop/sample ubuntu@ubuntu-VirtualBox:~$hdfs dfs -ls /hadoop Found 2 items -rw-r--r-- 2 ubuntu supergroup 0 2015-11-08 00:57 /hadoop/sample -rw-r--r-- 2 ubuntu supergroup 16 2015-11-08 00:45 /hadoop/test
17)文本命令
HDFS命令拍摄源文件并以文本格式输出文件。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -text /hadoop/test This is a test.
18)CopyFromlocal命令
HDFS命令将文件从本地文件系统复制到HDFS。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -copyFromLocal /home/ubuntu/new /hadoop ubuntu@ubuntu-VirtualBox:~$hdfs dfs -ls /hadoop Found 3 items -rw-r--r-- 2 ubuntu supergroup 43 2015-11-08 01:08 /hadoop/new -rw-r--r-- 2 ubuntu supergroup 0 2015-11-08 00:57 /hadoop/sample -rw-r--r-- 2 ubuntu supergroup 16 2015-11-08 00:45 /hadoop/test
19)CopyTolocal命令
类似于get命令,除了目的地仅限于本地文件引用。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -copyToLocal /hadoop/sample /home/ubuntu/ ubuntu@ubuntu-VirtualBox:~$ls -l s* -rw-r--r-- 1 ubuntu ubuntu 0 Nov 8 01:12 sample -rw-rw-r-- 1 ubuntu ubuntu 102436055 May 20 04:47 sqoop-1.99.7-bin-hadoop200.tar.gz
20)MV命令
HDFS命令将文件从源移动到目标。
此命令允许多个源,在这种情况下,目的地需要成为目录。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -mv /hadoop/sample /tmp ubuntu@ubuntu-VirtualBox:~$hdfs dfs -ls /tmp Found 1 items -rw-r--r-- 2 ubuntu supergroup 0 2015-11-08 00:57 /tmp/sample
21)CP命令
hdfs命令将文件从源复制到目标。
此命令允许多个源,在这种情况下,目的地必须是目录。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -cp /tmp/sample /usr ubuntu@ubuntu-VirtualBox:~$hdfs dfs -ls /usr Found 1 items -rw-r--r-- 2 ubuntu supergroup 0 2015-11-08 01:22 /usr/sample
22)尾部命令
将文件"new"文件的最后千字节显示为stdout
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -tail /hadoop/new This is a new file. Running HDFS commands.
23)Chown命令
hdfs命令以更改文件的所有者。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -chown root:root /tmp ubuntu@ubuntu-VirtualBox:~$hdfs dfs -ls / Found 5 items drwxrwxrwx - ubuntu supergroup 0 2015-11-08 01:17 /hadoop drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:26 /system drwxr-xr-x - ubuntu supergroup 0 2015-11-07 01:11 /test drwxr-xr-x - root root 0 2015-11-08 01:17 /tmp drwxr-xr-x - ubuntu supergroup 0 2015-11-08 01:22 /usr
24)SetRep命令
默认复制因子为文件为3.以下HDFS命令用于更改文件的复制因子。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -setrep -w 2 /usr/sample Replication 2 set: /usr/sample Waiting for /usr/sample ... done
25)DistCP命令
将目录从群集中的一个节点复制到另一个
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -distcp hdfs://namenodeA/apache_hadoop hdfs://namenodeB/hadoop
26)stat命令
以指定格式打印关于<path>处的文件/目录的统计信息。
格式在块(%b)中接受文件大小,键入(%f),boous所有者名称(%g),名称(%n),块大小(%n),replication(%r),所有者的用户名(% U)和修改日期(%Y,%Y)。
%Y显示UTC日期为"YYYY-MM-DD HH:MM:SS",%Y显示自1970年1月1日UTC以来的毫秒。
如果未指定格式,则默认使用%y。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -stat "%F %u:%g %b %y %n" /hadoop/test regular file ubuntu:supergroup 16 2015-11-07 19:15:22 test
27)getfacl命令
显示文件和目录的访问控制列表(ACL)。
如果目录具有默认ACL,则GetFacl还会显示默认ACL。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -getfacl /hadoop # file: /hadoop # owner: ubuntu # group: supergroup
28)du -s命令
显示文件长度的摘要。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -du -s /hadoop 59 /hadoop
29)校验和命令
返回文件的校验和信息。
ubuntu@ubuntu-VirtualBox:~$hdfs dfs -checksum /hadoop/new /hadoop/new MD5-of-0MD5-of-512CRC32C 000002000000000000000000639a5d8ac275be8d0c2b055d75208265
30)GetMerge命令
将源目录和目标文件作为输入,将文件串联在src到目标本地文件中。
ubuntu@ubuntu-VirtualBox:~$cat test This is a test. ubuntu@ubuntu-VirtualBox:~$hdfs dfs -cat /hadoop/new This is a new file. Running HDFS commands. ubuntu@ubuntu-VirtualBox:~$hdfs dfs -getmerge /hadoop/new test ubuntu@ubuntu-VirtualBox:~$cat test This is a new file. Running HDFS commands.