购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

2.4
HDFS操作命令

hdfs命令位于$HADOOP_HOME/bin目录下。由于已经配置了HADOOP_HOME和PATH的环境变量,所以此命令可以在任意目录下执行。可以通过直接输入hdfs命令,查看它的使用帮助:

 
    $ hdfs
    Usage: hdfs [--config confdir] [--loglevel loglevel] COMMAND
           where COMMAND is one of:
    dfs    run a filesystem command on the file systems supported in Hadoop.
    classpath            prints the classpath
    namenode -format     format the DFS filesystem
    secondarynamenode    run the DFS secondary namenode
    namenode             run the DFS namenode
    journalnode          run the DFS journalnode
    zkfc                 run the ZK Failover Controller daemon
    datanode             run a DFS datanode
    debug                run a Debug Admin to execute HDFS debug commands
    dfsadmin             run a DFS admin client
    haadmin              run a DFS HA admin client
    fsck                 run a DFS filesystem checking utility
    balancer             run a cluster balancing utility
    jmxget               get JMX exported values from NameNode or DataNode.
    mover                run a utility to move block replicas across
                           storage types
    oiv                  apply the offline fsimage viewer to an fsimage
    oiv_legacy           apply the offline fsimage viewer to an legacy fsimage
    oev                  apply the offline edits viewer to an edits file
    fetchdt              fetch a delegation token from the NameNode
    getconf              get config values from configuration
    groups               get the groups which users belong to
    snapshotDiff         diff two snapshots of a directory or diff the
                           current directory contents with a snapshot
    lsSnapshottableDir   list all snapshottable dirs owned by the current user
                                Use -help to see options
    portmap              run a portmap service
    nfs3                 run an NFS version 3 gateway
    cacheadmin           configure the HDFS cache
    crypto               configure HDFS encryption zones
    storagepolicies      list/get/set block storage policies
    version              print the version
    Most commands print help when invoked w/o parameters.

上面的这些命令,在后面的课程中基本都会涉及。现在让我们来查看几个使用比较多的命令。在上面的列表中,第一个dfs是经常被使用的命令。可以通过hdfs dfs -help查看dfs的具体使用方法。由于参数过多,本书就不一一列举了。dfs命令,就是通过命令行操作HDFS目录或是文件的命令,类似于Linux文件命令一样,只不过dfs操作的是HDFS文件系统中的文件。表2-1列出HDFS几个常用命令。

表2-1 HDFS常用命令

下面看几个示例。显示根目录下的所有文件和目录:

 
    [hadoop@server201 ~]$ hdfs dfs -ls /
    Found 1 items
    drwxr-xr-x   - hadoop supergroup      0 2021-03-10 20:41 /test

以递归的形式显示根目录下的所有文件或目录,注意-R参数:

 
    [hadoop@server201 ~]$ hdfs dfs -ls -R /
    drwxr-xr-x   - hadoop supergroup         0 2021-03-10 20:41 /test
    -rw-r--r--   1 hadoop supergroup         6 2021-03-10 20:41 /test/a.txt

删除HDFS上的文件:

 
    [hadoop@server201 ~]$ hdfs dfs -rm  /test/a.txt

删除HDFS上的目录:

 
    [hadoop@server201 ~]$ hdfs dfs -rm -r /test

将本地文件上传到HDFS上:

 
    [hadoop@server201 ~]$ hdfs dfs -copyFromLocal a.txt /test/a.txt

使用put命令,同样可以将本地文件上传到HDFS上:

 
    [hadoop@server201 ~]$ hdfs dfs -put a.txt /test/b.txt

使用moveFromLocal选项,可以同时将本地文件删除:

 
    [hadoop@server201 ~]$ hdfs dfs -moveFromLocal a.txt /test/c.txt

使用get/copyToLocal/moveToLocal选项,可以下载文件到本地: ZIK9bnMauBtctf9bzlqTTbbL1UC4/1uoR5sCF7TuAFtp1d4mTJEhBA9BBNDaBfps

 
    [hadoop@server201 ~]$ hdfs dfs -get /test/c.txt a.txt
    [hadoop@server201 ~]$ hdfs dfs -copyToLocal /test/a.txt a1.txt
    [hadoop@server201 ~]$ hdfs dfs -moveToLocal /test/a.txt a2.txt
点击中间区域
呼出菜单
上一章
目录
下一章
×