导语
安装完Hadoop之后下面就来看看如何使用HDFS
打印文件列表
标准写法
hadoop fs -ls hdfs:/ # hdfs:明确的说明是HDFS系统路径
简写
hadoop fs -ls / # 默认是HDFS系统下的根目录
打印指定的子目录
hadoop fs -ls /
上传文件、目录
put 的用法
上传新文件
hadoop fs -put file:/root/test.txt hdfs:/ # 上传本地的test.txt 到HDFS的根目录,HDFS根目录中必须不存在这个文件不然就会报错,文件已存在
hadoop fs -put /root/test.txt /test2.txt # 上传并且重命名文件
上传文件夹
hadoop fs -put mypkg /newpkg # 上传并重命名文件夹
覆盖上传
hadoop fs -put -f /root/test.txt # 如果HDFS的目录中有同名的文件就会被覆盖
copyFromLocal 的用法
上传文件并重命名
hadoop -fs -copyFromLocal file:/test.txt hdfs:/test2.txt
覆盖上传
hadoop fs -copyFromLocal -f test.txt /test.txt
下载文件、目录
get 用法
拷贝文件到本地目录
hadoop fs -get hdfs:/test2.txt file:/root/
拷贝文件并重命名,可以简写
hadoop fs -get /test2.txt /root/test0.txt
copyToLocal 用法
拷贝文件到本地目录
hadoop fs -copyToLocal hdfs:/test2.txt file:/root/
拷贝文件并重命名
hadoop fs -copyToLocal hdfs:/test2.txt file:/root/test21.txt
拷贝文件、目录
从本地拷贝到HDFS中,跟put类似
hadoop fs -cp /root/test.txt hdfs:/
从HDFS 到HDFS
hadoop fs -cp hdfs:/root/test.txt hdfs:/root/test1.txt
移动文件
hadoop fs -mv hdfs:/root/test.txt hdfs:/
删除文件、目录
删除指定文件
hadoop fs -rm /a.txt
删除全部txt文件
hadoop fs -rm /*.txt
递归删除全部文件和目录
hadoop fs -rm -R /dir/
读取文件
hadoop fs -cat /test.txt
hadoop fs -tail /test.txt
创建空文件
hadoop fs - touchz /newfile.txt
创建文件夹
hadoop fs -mkdir /newdir /newdir2
hadoop fs -mkdir -p /newpkg/newpkg2/newpkg3
获取逻辑空间大小
hadoop fs -du /
hadoop fs -du -h /
hadoop fs -du -s /