HADOOP常用指令

HDFS调用指令:hdfs dfs <args>

hdfs dfs=hadoop dfs=hadoop fs(版本先后造成的差异,但不影响正常使用)

appendToFile           //追加文件内容到hdfs文件           
    //用法
    Usage: hadoop fs -appendToFile <localsrc> ... <dst>
    eg:
        //本地单个文件内容追加到hdfs文件
        hadoop fs -appendToFile localfile /user/hadoop/hadoopfile                                                             
        //本地多个文件内容追加到hdfs文件
        hadoop fs -appendToFile localfile1 localfile2 /user/hadoop/hadoopfile
        //本地单个文件内容追加到hdfs文件(这里的hdfs是全路径)
        hadoop fs -appendToFile localfile hdfs://nn.example.com/hadoop/hadoopfile
        //控制台输入的内容追加到hdfs文件
        hadoop fs -appendToFile - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin
注:
    HDFS不支持随机读和写。
    HDFS追加文件内容只能在文件末尾添加,不能随机添加。
cat                 //查看文件内容
    //用法
    Usage: hadoop fs -cat [-ignoreCrc] URI [URI ...]
    eg:
        //查看hdfs文件内容,这里可以查看多个文件,内容按照文件进行展示
        hadoop fs -cat hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
        //file:///表示本地文件
        hadoop fs -cat file:///file3 /user/hadoop/file4
chmod           //更改文件权限
    //用法,更改文件权限,这里的-R表示递归
    Usage: hadoop fs -chmod [-R] <MODE[,MODE]... | OCTALMODE> URI [URI ...]
chown          //更改文件的所有者权限
    //用法,这里可以变更文件的所有者,-R表示递归
    Usage: hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]
copyFromLocal       //复制本地文件到hdfs
    //用法,-f参数表示如果文件在hdfs已经存在,则进行覆盖
    Usage: hadoop fs -copyFromLocal [-f] <localsrc> URI
copyToLocal            //复制文件到本地
    //用法
    Usage: hadoop fs -copyToLocal [-ignorecrc] [-crc] URI <localdst>
count           //统计文件夹和文件的数目,以及字节数
    //用法
    Usage: hadoop fs -count [-q] [-h] [-v] [-x] [-t [<storage type>]] [-u] [-e] <paths>
    eg:
         1(目录数)    3(文件数)    23742(所占空间大小)    /tmp/hdfs_test
cp           //文件复制
    //用法
    Usage: hadoop fs -cp [-f] [-p | -p[topax]] URI [URI ...] <dest>
    eg:

        hadoop fs -cp /user/hadoop/file1 /user/hadoop/file2

        hadoop fs -cp /user/hadoop/file1 /user/hadoop/file2 /user/hadoop/dir
du          //显示给定目录中包含的文件和目录的大小或文件的长度,用字节大小表示,不包含递归
    //用法
    Usage: hadoop fs -du [-s] [-h] [-v] [-x] URI [URI ...]
    eg:
        //查看path下的磁盘情况,单位字节
        hadoop fs -du /user/hadoop/dir1 /user/hadoop/file1 hdfs://nn.example.com/user/hadoop/dir1
        //查看path下的磁盘情况,单位M
        hadoop fs -du -s -h  path  
        //查看各级文件大小(K、M、G)
        hadoop fs -du -h  path
        //过滤G存储级别的文件
        hadoop fs -du -h  path  |grep 'G'  
find           //查找文件
    //用法
    Usage: hadoop fs -find <path> ... <expression> ...
    eg:
        //按照名字查找问价
        hadoop fs -find / -name test -print
get          //下载文件
    //用法,-f表示覆盖
    Usage: hadoop fs -get [-ignorecrc] [-crc] [-p] [-f] <src> <localdst>
    eg:
        //从hdfs下载文件到本地
        hadoop fs -get /user/hadoop/file localfile
        //hdfs全路径
        hadoop fs -get hdfs://nn.example.com/user/hadoop/file localfile
getfacl        //获取目录或文件访问控制列表
    //用法,-R表示递归
    Usage: hadoop fs -getfacl [-R] <path>
    eg:
        
        hadoop fs -getfacl /file
        hadoop fs -getfacl -R /dir

getmerge           //合并文件
    //用法
    Usage: hadoop fs -getmerge [-nl] <src> <localdst>
    eg:
        //将hdfs目录下所有文件合并,并链接到本地文件
        hadoop fs -getmerge -nl /src /opt/output.txt
        //将多个hdfs目录下所有文件合并,并链接到本地文件
        hadoop fs -getmerge -nl /src/file1.txt /src/file2.txt /output.txt
help       //命令帮助手册
    //用法
    Usage: hadoop fs -help
ls            //文件列表
    //用法,-R表示递归
    Usage: hadoop fs -ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [-e] <args>
    eg:

        hadoop fs -ls /user/hadoop/file1
        hadoop fs -ls -e /ecdir
mkdir          //创建文件夹
    //用法,-p表示递归
    Usage: hadoop fs -mkdir [-p] <paths>
    eg:
        //创建文件夹
        hadoop fs -mkdir /user/hadoop/dir1 /user/hadoop/dir2
        //创建多个文件夹
        hadoop fs -mkdir hdfs://nn1.example.com/user/hadoop/dir hdfs://nn2.example.com/user/hadoop/dir
moveFromLocal       //剪切文件到hdfs
    //用法
    Usage: hadoop fs -moveFromLocal <localsrc> <dst>

moveToLocal         //剪切文件到本地
    //用法
    Usage: hadoop fs -moveToLocal [-crc] <src> <dst>
mv           //剪切问价
    //用法,在hdfs文件系统之间进行
    Usage: hadoop fs -mv URI [URI ...] <dest>
    eg:

        hadoop fs -mv /user/hadoop/file1 /user/hadoop/file2
    
        hadoop fs -mv hdfs://nn.example.com/file1 hdfs://nn.example.com/file2 hdfs://nn.example.com/file3 hdfs://nn.example.com/dir1
put           //上传文件
    //用法,-f表示覆盖
    Usage: hadoop fs -put [-f] [-p] [-l] [-d] [ - | <localsrc1> .. ]. <dst>
    eg:
        //本地文件上传到hdfs
        hadoop fs -put localfile /user/hadoop/hadoopfile
        //-f表示覆盖
        hadoop fs -put -f localfile1 localfile2 /user/hadoop/hadoopdir
        //跳过临时文件(with the suffix ._COPYING_.)
        hadoop fs -put -d localfile hdfs://nn.example.com/hadoop/hadoopfile
        //控制台的输入数据进行上传
        hadoop fs -put - hdfs://nn.example.com/hadoop/hadoopfile Reads the input from stdin.
rm           //删除一个文件或目录
    //用法
    Usage: hadoop fs -rm [-f] [-r |-R] [-skipTrash] [-safely] URI [URI ...]
    eg:
        //删除文件和空文件夹
        hadoop fs -rm hdfs://nn.example.com/file /user/hadoop/emptydir

rmdir        //删除空文件夹
    //用法
    Usage: hadoop fs -rmdir [--ignore-fail-on-non-empty] URI [URI ...]
    eg:
        //删除空文件夹
        hadoop fs -rmdir /user/hadoop/emptydir

setfacl        //细分linux下的文件权限
    //用法,-R表示递归
    Usage: hadoop fs -setfacl [-R] [-b |-k -m |-x <acl_spec> <path>] |[--set <acl_spec> <path>]
    eg:

        hadoop fs -setfacl -m user:hadoop:rw- /file
        hadoop fs -setfacl -x user:hadoop /file
        hadoop fs -setfacl -b /file
        hadoop fs -setfacl -k /dir
        hadoop fs -setfacl --set user::rw-,user:hadoop:rw-,group::r--,other::r-- /file
        hadoop fs -setfacl -R -m user:hadoop:r-x /dir
        hadoop fs -setfacl -m default:user:hadoop:r-x /dir
setrep        //修改副本数
    //用法
    Usage: hadoop fs -setrep [-R] [-w] <numReplicas> <path>
    eg:
        hadoop fs -setrep -w 3 /user/hadoop/dir1

[aps@bdsitapp318 ~]$ hdfs dfs -ls /zyp/123.txt
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=100m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=150m; support was removed in 8.0
-rw-r--r--   3(这里的3表示的是副本数) aps supergroup         23 2020-11-11 16:49 /zyp/123.txt
//修改为2个副本
[aps@bdsitapp318 ~]$ hdfs dfs -setrep -w 2 /zyp/123.txt
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=100m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=150m; support was removed in 8.0
Replication 2 set: /zyp/123.txt
Waiting for /zyp/123.txt ...
WARNING: the waiting time may be long for DECREASING the number of replications.
. done
//查看结果
[aps@bdsitapp318 ~]$ hdfs dfs -ls /zyp/123.txt
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=100m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=150m; support was removed in 8.0
-rw-r--r--   2(设置成功) aps supergroup         23 2020-11-11 16:49 /zyp/123.txt
[aps@bdsitapp318 ~]$ 

stat        //展示文件的属性信息
    //用法
    Usage: hadoop fs -stat [format] <path> ...
tail        //默认展示最后1k数据
    //用法

    Usage: hadoop fs -tail [-f] URI
    eg:
        //1k数据中的前10行数据
        hadoop fs -tail path | head -10
        //从尾部查看最后1K
        hadoop fs -tail path 
        //1k数据中的最后10行数据
        hadoop fs -tail path | tail -10
text        //以文本形式展示数据
    //用法
    Usage: hadoop fs -text <src>

    压缩文件需要使用text查看内容
    hdfs dfs -text /user/mklein/testfile
touchz        //创建文件
    //用法
    Usage: hadoop fs -touchz URI [URI ...]
    eg:
        //创建文件
        hadoop fs -touchz pathname
usage        //查看某个命令的用法
    //用法
    Usage: hadoop fs -usage command
safemode        //HDFS中safemode模式和集群状态

    查看safemode状态:

          hdfs dfsadmin -safemode get

    进入safemode:

          hdfs dfsadmin -safemode enter 


    退出safemode:

          hdfs dfsadmin -safemode leave

    查看集群状态

          hdfs dfsadmin -report

hdfs dfs -head(此命令不存在hdfs文件系统),但可以借助管道命令使用。

hdfs dfs -test(此命令无效果在hdfs文件系统)

随机返回指定行数的样本数据

hadoop fs -cat /test/gonganbu/scene_analysis_suggestion/* | shuf -n 5

查看文件行数

hadoop fs -cat /test/gonganbu/scene_analysis_suggestion/* | wc -l

查看文件数

hadoop fs -ls /test/gonganbu/scene_analysis_suggestion/* |wc -l

查看正在执行的job

      hadoop job -list来列出当前hadoop正在执行的jobs

然后使用hadoop job -kill job_1546932571227_0082来杀死该job任务

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值