HDFS-Shell 文件操作

一、操作 HDFS 上的文件有两个命令可以用

hdfs dfs:只能操作 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
  dfsrouter            run the DFS router
  dfsrouteradmin       manage Router-based federation
  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.
View Code

hadoop fs:除了 HDFS 上的文件,还可以操作本地文件

Usage: hadoop [--config confdir] [COMMAND | CLASSNAME]
  CLASSNAME            run the class named CLASSNAME
 or
  where COMMAND is one of:
  fs                   run a generic filesystem user client
  version              print the version
  jar <jar>            run a jar file
                       note: please use "yarn jar" to launch
                             YARN applications, not this command.
  checknative [-a|-h]  check native hadoop and compression libraries availability
  distcp <srcurl> <desturl> copy file or directories recursively
  archive -archiveName NAME -p <parent path> <src>* <dest> create a hadoop archive
  classpath            prints the class path needed to get the
                       Hadoop jar and the required libraries
  credential           interact with credential providers
  daemonlog            get/set the log level for each daemon
  trace                view and modify Hadoop tracing settings

Most commands print help when invoked w/o parameters.
View Code

二、使用

help:查看命令帮助

hadoop fs -help ls

ls:显示目录信息

# 查看根目录
hadoop fs -ls /

# 递归查看所有目录
hadoop fs -ls -R /
hadoop fs -lsr /

mkdir:创建目录

# 创建多级目录
hadoop fs -mkdir -p /china/hubei/

moveFromLocal:移动本地文件到 HDFS 上

# 移动本地 /opt/java-linux-x64.tar.gz 至 HDFS 的 /china/hubei/ 路径下
hadoop fs -moveFromLocal /opt/java-linux-x64.tar.gz /china/hubei/

appendToFile:把本地文件的内容追加到 HDFS 上的文件末尾

# 创建两个文件
echo "AAA" > /tmp/AAA.txt
echo "BBB" > /tmp/BBB.txt

# 把本地 /tmp/AAA.txt 移动至 HDFS 上的 /china/ 目录下
hadoop fs -moveFromLocal /tmp/AAA.txt /china/

# 把本地 /tmp/BBB.txt 追加到 HDFS 上的 /china/AAA.txt 文件末尾
hadoop fs -appendToFile /tmp/BBB.txt /china/AAA.txt

cat:查看文件内容

hadoop fs -cat /china/AAA.txt

chgrp 、chmod、chown:修改文件属性和权限

# 修改 /china/ 目录及其所有子目录的用户组为 root
hadoop fs -chgrp -R root /china/

# 修改 /china/ 目录及其所有子目录的权限为 777
hadoop fs -chmod -R 777 /china/

# 修改 /china/ 目录及其所有子目录的所有者为 root
hadoop fs -chown -R root /china/

put、copyFromLocal:拷贝本地文件到 HDFS 上(上传)

# 复制本地 /tmp/ 目录到 HDFS 的 /china/ 目录下
hadoop fs -copyFromLocal /tmp/ /china/
hadoop fs -put /tmp/ /china/

get、copyToLocal:拷贝 HDFS 上的文件到本地(下载)

# 复制 HDFS 上 /china/BBB.txt 文件到本地的当前目录
hadoop fs -copyToLocal /china/BBB.txt ./
hadoop fs -get /china/BBB.txt ./

moveToLocal:移动 HDFS 上的文件到本地 

# Hadoop 目前版本(2.9.2)尚未实现该功能

cp:在 HDFS 上复制文件

# 将 HDFS 上的 /china/AAA.txt 复制到 HDFS 的 / 目录下
hadoop fs -cp /china/AAA.txt /

mv:在 HDFS 上移动文件

# 将 HDFS 上的 /china/BBB.txt 移动到 HDFS 的 / 目录下
hadoop fs -mv /china/BBB.txt /

getmerge:合并下载

# 清空本地 /tmp/ 目录
rm -rf /tmp/*

# 在本地 /tmp/ 中创建两个文件
echo "AAA" > /tmp/AAA.txt
echo "BBB" > /tmp/BBB.txt

# 把本地 /tmp/*.txt 上传至 HDFS 上的 /china/ 目录下
hadoop fs -mkdir -p /china/txt/
hadoop fs -put /tmp/*.txt /china/txt/

# 下载 HDFS 上 /china/txt/ 路径下所有文件的内容到本地
hadoop fs -getmerge /china/txt/* /tmp/CCC.txt

tail:显示 HDFS 上的文件最后 1KB 的内容

# 直接显示
hadoop fs -tail /AAA.txt

# 监控显示,有新数据追加进来时会实时显示
hadoop fs -tail -f /AAA.txt

rmdir:删除空文件夹

# 需要确保 HDFS 上的 /temp/ 目录为空
hadoop fs -rmdir /temp/

rm:删除文件或文件夹

# 删除 HDFS 上的 /china/ 目录
# f 目标目录不存在不提示
# r|R 递归删除
hadoop fs -rm -f -r /china/
hadoop fs -rmr -f /china/

如果启用了垃圾箱,则文件系统会将已删除的文件移动到垃圾箱目录,默认禁用垃圾箱功能

<!-- core-site.xml -->
<!-- value 的值单位为分钟,设置大于零的值来启用垃圾 -->
<property>
    <name>fs.trash.interval</name>
    <value>60*24*2</value>
</property>

count,du:统计文件大小

hadoop fs -du -s -h /
hadoop fs -count /

find:查找文件

# name 不区分大小写
# iname 区分大小写
# print 打印(默认)
# print0 打印在一行
hadoop fs -find / -name *.txt -print

https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/FileSystemShell.html

http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html

https://www.codercto.com/a/42708.html

猜你喜欢

转载自www.cnblogs.com/jhxxb/p/10686398.html