hadoop笔记之hdfs shell操作

HDFS命令行操作

HDFS命令行操作

(以下是hadoop 1.x 版本的命令使用)

装好hadoop之前首先要进行一个格式化

hadoop namenode -format

运行之后,可以将文件夹都列出来

hadoop fs -ls /

例如打印user目录

hadoop fs -ls /user

如何将本地文件提交到hadoop的inpput目录下去

hadoop fs -put xxx /input

(注意当前所在的位置pwd)

如何将文件从hadoop上input文件目录下的xxx文件下载下来

hadoop fs -get input/xxx xxx

查看占用多少空间,使用多少命令

hadoop dfsadmin -report

(以下是hadoop 2.x 版本的HDFS命令行操作,大家可以对比一下不同)

  1. 同时上传多个文件到HDFS里面
hdfs dfs -appendToFile <localsrc> ... <dst>
  1. 查看文件内容
hdfs dfs -cat URI [URI ...]
  1. 修改文件所属组
hdfs dfs -chgrp [-R] GROUP URI [URI ...]
  1. 修改文件权限
hdfs dfs -chmod [-R] <MODE[,MODE]... | OCTALMODE> URI [URI ...]
  1. 修改文件所有者,文件所属组,其他用户的读、写、执行权限
hdfs dfs -chown [-R] [OWNER][:[GROUP]] URI [URI ]
  1. 复制文件到hdfs
hdfs dfs -copyFromLocal <localsrc> URI
  1. 复制文件到本地
hdfs dfs -copyToLocal [-ignorecrc] [-crc] URI <localdst>
  1. 统计文件及文件夹数目
hdfs dfs -count [-q] <paths>
  1. Hadoop HDFS 文件系统间的文件复制
 hdfs dfs -cp [-f] URI [URI ...] <dest>
  1. 统计目录下的文件及大小
 hdfs dfs -du [-s] [-h] URI [URI ...]
  1. 汇总目录下的文件总大小
hdfs dfs -dus <args>
  1. 清空回收站,文件被删除时,它首先会移到临时目录.Trash/中,当超过延迟时间之后,文件才会被永久删除
 hdfs dfs -expunge
  1. 下载文件到本地
hdfs dfs -get [-ignorecrc] [-crc] <src> <localdst>
  1. 查看ACL (访问权限组拥有者)
hdfs dfs -getfacl [-R] <path>
  1. 合并下载文件到本地
 hdfs dfs -getmerge <src> <localdst> [addnl]
  1. 查看目录
hdfs dfs -ls <args>
  1. 循环列出目录、子目录及文件信息
hdfs dfs -lsr <args>
  1. 创建空白文件夹
hdfs dfs -mkdir [-p] <paths>
  1. 剪切文件到hdfs
dfs -moveFromLocal <localsrc> <dst>
  1. 剪切文件到本地
hdfs dfs -moveToLocal [-crc] <src> <dst>
  1. 剪切hdfs文件
hdfs dfs -mv URI [URI ...] <dest>
  1. 上传文件
hdfs dfs -put <localsrc> ... <dst>
  1. 删除文件/空白文件夹
hdfs dfs -rm [-skipTrash] URI [URI ...]
  1. 递归删除 删除文件及文件夹下的所有文件
hdfs dfs -rmr [-skipTrash] URI [URI ...]
hdfs dfs -setfacl [-R] [-b|-k -m|-x <acl_spec> <path>]|[--set <acl_spec> <path>]

Sets Access Control Lists (ACLs) of files and directories.

Options:

-b: Remove all but the base ACL entries. The entries for user, group and others are retained for compatibility with permission bits.

-k: Remove the default ACL.

-R: Apply operations to all files and directories recursively.

-m: Modify ACL. New entries are added to the ACL, and existing entries are retained.

-x: Remove specified ACL entries. Other ACL entries are retained.

–set: Fully replace the ACL, discarding all existing entries. The acl_spec must include entries for user, group, and others for compatibility with permission bits.

acl_spec: Comma separated list of ACL entries.

path: File or directory to modify.

Examples:

hdfs dfs -setfacl -m user:hadoop:rw- /file
hdfs dfs -setfacl -x user:hadoop /file
hdfs dfs -setfacl -b /file
hdfs dfs -setfacl -k /dir
hdfs dfs -setfacl --set user::rw-,user:hadoop:rw-,group::r--,other::r-- /file
hdfs dfs -setfacl -R -m user:hadoop:r-x /dir
hdfs dfs -setfacl -m default:user:hadoop:r-x /dir

Exit Code:

Returns 0 on success and non-zero on error.

  1. 修改副本数
hdfs dfs -setrep [-R] [-w] <numReplicas> <path>
  1. 显示文件统计信息
hdfs dfs -stat URI [URI ...]
  1. 查看文件尾部信息
hdfs dfs -tail [-f] URI
hdfs dfs -test -[ezd] URI

对PATH进行如下类型的检查:

-e PATH是否存在,如果PATH存在,返回0,否则返回1

-z 文件是否为空,如果长度为0,返回0,否则返回1

-d 是否为目录,如果PATH为目录,返回0,否则返回1

  1. 查看文件内容
hdfs dfs -text <src>
  1. 创建长度为0的空文件
hdfs dfs -touchz URI [URI ...]

总而言之,hadoop使用命令跟linux的命令是很相似的。

上一篇:ICMP 隧道——将流量封装进 IMCP 的 ping 数据包中,旨在利用 ping 穿透防火墙的检测


下一篇:RDS MySQL 连接数满情况的处理