更新时间:2024-10-23 GMT+08:00
创建HDFS目录
功能简介
创建目录过程为:
- 调用FileSystem实例的exists方法查看该目录是否存在。
- 如果存在,则直接返回。
- 如果不存在,则调用FileSystem实例的mkdirs方法创建该目录。
代码样例
如下是写文件的代码片段,详细代码请参考com.huawei.bigdata.hdfs.examples中的HdfsExample类。
/**
* 创建目录
*
* @throws java.io.IOException
*/
private void mkdir() throws IOException {
Path destPath = new Path(DEST_PATH);
if (!createPath(destPath)) {
LOG.error("failed to create destPath " + DEST_PATH);
return;
}
LOG.info("success to create path " + DEST_PATH);
}
/**
* create file path
*
* @param filePath
* @return
* @throws java.io.IOException
*/
private boolean createPath(final Path filePath) throws IOException {
if (!fSystem.exists(filePath)) {
fSystem.mkdirs(filePath);
}
return true;
}
父主题: 开发HDFS应用