目录操作
首先检查一下自己的hdfs是不是有/user路径
http://localhost:50070/explorer.html#/
没有就新建一个
mac@kkkMac hadoop-2.7.3 % ./bin/hdfs dfs -mkdir -p /user/hadoop
22/05/02 15:43:54 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
mac@kkkMac hadoop-2.7.3 %
该命令中表示在HDFS中创建一个“/user/hadoop”目录,“–mkdir”是创建目录的操作,“-p”表示如果是多级目录,则父目录和子目录一起创建,这里“/user/hadoop”就是一个多级目录,因此必须使用参数“-p”,否则会出错。
mac@kkkMac hadoop-2.7.3 % ./bin/hdfs dfs -ls /
22/05/02 15:48:05 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 1 items
drwxr-xr-x - mac supergroup 0 2022-05-02 15:43 /user
也可以用这个命令,就可以不用管路径了
% hadoop fs -ls /
文件操作
新建一个文件input(删除的话 -rm -r可以删除目录和目录下的所有内容)
hadoop fs -mkdir /input
在本地也新建一个路径和文件
mac@kkkMac hadoop-2.7.3 % mkdir /users/mac/desktop/hadoop/input
mac@kkkMac hadoop-2.7.3 % echo 'hadoop\nSpark\nXMU DBLAB'> /users/mac/desktop/hadoop/input/file1.txt
把本地的文件放到hdfs的input文件中去
hadoop fs -put /users/mac/desktop/hadoop/input/file1.txt /input
查看hdfs上的file1.txt文件
mac@kkkMac hadoop-2.7.3 % hadoop fs -cat /input/file1.txt
22/05/02 15:58:03 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
hadoop
Spark
XMU DBLAB
也可以从hdfs上面下载到本地,顺便改名为file2.txt(file1.txt
已经存在了)
mac@kkkMac hadoop-2.7.3 % hadoop fs -get /input/file1.txt /users/mac/desktop/hadoop/input
22/05/02 15:59:50 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
get: `/users/mac/desktop/hadoop/input/file1.txt': File exists
mac@kkkMac hadoop-2.7.3 % hadoop fs -get /input/file1.txt /users/mac/desktop/hadoop/input/file2.txt
22/05/02 16:00:10 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
追加文件
把本地的file1.txt内容追加到hdfs上的file1.txt中去
mac@kkkMac hadoop-2.7.3 % hdfs dfs -appendToFile /users/mac/desktop/hadoop/input/file1.txt /input/file1.txt
mac@kkkMac hadoop-2.7.3 % hdfs dfs -cat /input/file1.txt
hadoop
Spark
XMU DBLAB
hadoop
Spark
XMU DBLAB