hadoop(五): shell命令

时间:2023-03-10 00:11:56
hadoop(五): shell命令

hdfs dfs


  • -cat URI : 查看文件内容
    1. hdfs dfs -cat hdfs dfs -cat hdfs://mycluster/user/root/rcc1
    2. hdfs dfs -cat file:///usr/hdp/2.4.2.0-258/hadoop/bin/rcc
  • -ls <args>: 查文件列表
    1. 返回格式:permissions number_of_replicas userid groupid filesize modification_date modification_time filename
      [root@hdp2 ~]# hdfs dfs -ls
      Found items
      drwx------ - root hdfs -- : .Trash
      drwxr-xr-x - root hdfs -- : .hiveJars
      -rw-r--r-- root hdfs -- : mapred
      -rw-r--r-- root hdfs -- : rcc1
  • -mkdir <paths>   : 创建目录
  1. The behavior is much like unix mkdir -p creating parent directories along the path
    [root@hdp2 ~]# hdfs dfs -ls
    Found items
    drwx------ - root hdfs -- : .Trash
    drwxr-xr-x - root hdfs -- : .hiveJars
    -rw-r--r-- root hdfs -- : mapred
    -rw-r--r-- root hdfs -- : rcc1
    [root@hdp2 ~]# hdfs dfs -mkdir hdfs://mycluster/user/root/zhu
    [root@hdp2 ~]# hdfs dfs -ls
    Found items
    drwx------ - root hdfs -- : .Trash
    drwxr-xr-x - root hdfs -- : .hiveJars
    -rw-r--r-- root hdfs -- : mapred
    -rw-r--r-- root hdfs -- : rcc1
    drwxr-xr-x - root hdfs -- : zhu
  • -copyFromLocal <localsrc> URI: 上传文件,类似于put
  • -put <localsrc> ... <dst>
  • [root@hdp2 ~]# hdfs dfs -ls /user/root/zhu
    [root@hdp2 ~]# hdfs dfs -put /usr/zhu/a1.png /user/root/zhu
    [root@hdp2 ~]# hdfs dfs -copyFormLocal /usr/zhu/a2.png /user/root/zhu
    -copyFormLocal: Unknown command
    [root@hdp2 ~]# hdfs dfs -copyFromLocal /usr/zhu/a2.png /user/root/zhu
    [root@hdp2 ~]# hdfs dfs -ls /user/root/zhu
    Found items
    -rw-r--r-- root hdfs -- : /user/root/zhu/a1.png
    -rw-r--r-- root hdfs -- : /user/root/zhu/a2.png
    [root@hdp2 ~]#
    • -copyToLocal [-ignorecrc] [-crc] URI <localdst> : 下载到本地
    • -get [-ignorecrc] [-crc] <src> <localdst>
      [root@hdp2 ~]# clear
      [root@hdp2 ~]# hdfs dfs -ls /user/root/zhu
      Found items
      -rw-r--r-- root hdfs -- : /user/root/zhu/a1.png
      -rw-r--r-- root hdfs -- : /user/root/zhu/a2.png
      [root@hdp2 ~]# hdfs dfs -copyToLocal hdfs://mycluster/user/root/zhu/a1.png /usr/zhu/tmp
      [root@hdp2 ~]# hdfs dfs -get /usr/root/zhu/a2.png /usr/zhu/tmp
      get: `/usr/root/zhu/a2.png': No such file or directory
      [root@hdp2 ~]# hdfs dfs -get /user/root/zhu/a2.png /usr/zhu/tmp
      [root@hdp2 ~]# cd /usr/zhu/tmp
      [root@hdp2 tmp]# ll
      total
      -rw-r--r-- root root Sep : a1.png
      -rw-r--r-- root root Sep : a2.png
      [root@hdp2 tmp]#
      • -count [-q] <paths> :统计
      1. The output columns with -count are: DIR_COUNT, FILE_COUNT, CONTENT_SIZE FILE_NAME
      2. The output columns with -q are: QUOTA, REMAINING_QUATA, SPACE_QUOTA, REMAINING_SPACE_QUOTA
        [root@hdp2 tmp]# hdfs dfs -count /user/root/zhu
        /user/root/zhu
        [root@hdp2 tmp]# hdfs dfs -count -q /user/root/zhu
        none inf none inf /user/root/zhu
        [root@hdp2 tmp]#
    1. -du [-s] [-h] URI [URI …]:  统计大小
        1. The -s option will result in an aggregate summary of file lengths being displayed, rather than the individual file
        2. The -h option will format file sizes in a "human-readable" fashion (e.g 64.0m instead of 67108864)
          [root@hdp2 tmp]# hdfs dfs -du -h /user/root/zhu
          84.9 K /user/root/zhu/a1.png
          54.5 K /user/root/zhu/a2.png
          [root@hdp2 tmp]#
      • -rm -r [-skipTrash] URI [URI …]
        1. -r: Recursive version of delete
          [root@hdp2 tmp]# hdfs dfs -ls /user/root/zhu
          Found items
          -rw-r--r-- root hdfs -- : /user/root/zhu/a1.png
          [root@hdp2 tmp]# hdfs dfs -rm -r /user/root/zhu
          // :: INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = minutes, Emptier interval = minutes.
          Moved: 'hdfs://mycluster/user/root/zhu' to trash at: hdfs://mycluster/user/root/.Trash/Current
          [root@hdp2 tmp]# hdfs dfs -ls /user/root/zhu
          ls: `/user/root/zhu': No such file or directory
          [root@hdp2 tmp]#
      • -touchz URI [URI …]
        1. Create a file of zero length
          [root@hdp2 tmp]# hdfs dfs -ls
          Found items
          drwx------ - root hdfs -- : .Trash
          drwxr-xr-x - root hdfs -- : .hiveJars
          -rw-r--r-- root hdfs -- : mapred
          -rw-r--r-- root hdfs -- : rcc1
          [root@hdp2 tmp]# hdfs dfs -touchz a.txt
          [root@hdp2 tmp]# hdfs dfs -ls
          Found items
          drwx------ - root hdfs -- : .Trash
          drwxr-xr-x - root hdfs -- : .hiveJars
          -rw-r--r-- root hdfs -- : a.txt
          -rw-r--r-- root hdfs -- : mapred
          -rw-r--r-- root hdfs -- : rcc1
          [root@hdp2 tmp]# hdfs dfs -cat /user/root/a.txt
          [root@hdp2 tmp]#
      • 小结:

      •  本质就是linux文件操作命令