Managing HDFS Directories¶
Now let us have a look at how to create directories and manage ownership.
By default hdfs is superuser of HDFS
hadoop fs -mkdirorhdfs dfs -mkdir– to create directorieshadoop fs -chownorhdfs dfs -chown– to change ownership of fileschowncan also be used to change the group. We can change the group using-chgrpcommand as well. Make sure to run-helpon chgrp and check the details.Here are the steps to create user space. Only users in HDFS group can take care of it.
Create directory with user id
itversityunder /userChange ownership to the same name as the directory created earlier (/user/itversity)
You can validate permissions by using
hadoop fs -lsorhdfs dfs -lscommand on /user. Make sure to grep for the user name you are looking for.
Let’s go ahead and create user space in HDFS for
itversity. I have to login as sudoer and run below commands.
sudo -u hdfs hdfs dfs -mkdir /user/itversity
sudo -u hdfs hdfs dfs -chown -R itversity:students /user/itversity
hdfs dfs -ls /user|grep itversity
You should be able to create folders under your home directory.
%%sh
hdfs dfs -ls /user/${USER}
%%sh
hdfs dfs -mkdir /user/${USER}/retail_db
%%sh
hdfs dfs -ls /user/${USER}
You can create the directory structure using
mkdir -p. The existing folders will be ignored and non existing folders will be created.Let us run
hdfs dfs -mkdir -p /user/${USER}/retail_db/orders/year=2020.As
/user/${USER}/retail_dbalready exists, it will be ignored.Both
/user/${USER}/retail_db/ordersas well as/user/${USER}/retail_db/orders/year=2020will be created.
%%sh
hdfs dfs -help mkdir
%%sh
hdfs dfs -ls -R /user/${USER}/retail_db
%%sh
hdfs dfs -mkdir -p /user/${USER}/retail_db/orders/year=2020
%%sh
hdfs dfs -ls -R /user/${USER}/retail_db
We can delete non empty directory using
hdfs dfs -rm -Rand empty directory usinghdfs dfs -rmdir. We will explorehdfs dfs -rmin detail later.
%%sh
hdfs dfs -help rmdir
%%sh
hdfs dfs -rmdir /user/${USER}/retail_db/orders/year=2020
%%sh
hdfs dfs -rm /user/${USER}/retail_db
%%sh
hdfs dfs -rmdir /user/${USER}/retail_db
%%sh
hdfs dfs -rm -R /user/${USER}/retail_db
%%sh
hdfs dfs -ls /user/${USER}