How to create a directory on HDFS with Hadoop?
In Hadoop, you can create a directory on HDFS using either command line tools or Java API. To create a directory using command line tools, open the terminal and execute the following command:
hdfs dfs -mkdir /path/to/directory
This will create a directory named ‘directory’ in the root directory of HDFS. Additionally, ensure that you have added the Hadoop dependency to your Java project before creating the directory using the Java API.
import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class CreateDirectoryExample {
public static void main(String[] args) {
try {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path directoryPath = new Path("/path/to/directory");
fs.mkdirs(directoryPath);
fs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Running the above code will create a directory named `directory` in the root directory of HDFS. Whether you choose to use command-line tools or the Java API, creating a directory on HDFS is similar – just specify the path of the directory.