Skip to content

Commit f1f275d

Browse files
committed
Create folder if not exits on HDFS write (5938)
1 parent 8c4c0bf commit f1f275d

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

velox/connectors/hive/storage_adapters/hdfs/HdfsWriteFile.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ HdfsWriteFile::HdfsWriteFile(
2525
short replication,
2626
int blockSize)
2727
: hdfsClient_(hdfsClient), filePath_(path) {
28+
auto pos = filePath_.rfind("/");
29+
auto parentDir = filePath_.substr(0, pos + 1);
30+
// Check whether the parentDir exist, create it if not exist.
31+
if (hdfsExists(hdfsClient_, parentDir.c_str()) == -1) {
32+
hdfsCreateDirectory(hdfsClient_, parentDir.c_str());
33+
}
34+
2835
hdfsFile_ = hdfsOpenFile(
2936
hdfsClient_,
3037
filePath_.c_str(),

velox/connectors/hive/storage_adapters/hdfs/tests/HdfsFileSystemTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,20 @@ TEST_F(HdfsFileSystemTest, write) {
404404
ASSERT_EQ(writeFile->size(), data.size() * 3);
405405
}
406406

407+
TEST_F(HdfsFileSystemTest, writeWithParentDirNotExist) {
408+
std::string path = "/parent/directory/that/does/not/exist/a.txt";
409+
auto writeFile = openFileForWrite(path);
410+
std::string data = "abcdefghijk";
411+
writeFile->append(data);
412+
writeFile->flush();
413+
ASSERT_EQ(writeFile->size(), 0);
414+
writeFile->append(data);
415+
writeFile->append(data);
416+
writeFile->flush();
417+
writeFile->close();
418+
ASSERT_EQ(writeFile->size(), data.size() * 3);
419+
}
420+
407421
TEST_F(HdfsFileSystemTest, missingFileForWrite) {
408422
const std::string filePath = "hdfs://localhost:7777/path/that/does/not/exist";
409423
const std::string errorMsg =

0 commit comments

Comments
 (0)