Skip to content

Commit 7cac359

Browse files
JkSelfrui-mo
authored andcommitted
Create the hdfs path if not exist (8343)
1 parent d61060c commit 7cac359

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
@@ -429,6 +429,20 @@ TEST_F(HdfsFileSystemTest, writeFlushFailures) {
429429
"Cannot flush HDFS file because file handle is null, file path: /a.txt");
430430
}
431431

432+
TEST_F(HdfsFileSystemTest, writeWithParentDirNotExist) {
433+
std::string path = "/parent/directory/that/does/not/exist/a.txt";
434+
auto writeFile = openFileForWrite(path);
435+
std::string data = "abcdefghijk";
436+
writeFile->append(data);
437+
writeFile->flush();
438+
ASSERT_EQ(writeFile->size(), 0);
439+
writeFile->append(data);
440+
writeFile->append(data);
441+
writeFile->flush();
442+
writeFile->close();
443+
ASSERT_EQ(writeFile->size(), data.size() * 3);
444+
}
445+
432446
TEST_F(HdfsFileSystemTest, readFailures) {
433447
struct hdfsBuilder* builder = hdfsNewBuilder();
434448
hdfsBuilderSetNameNode(builder, localhost.c_str());

0 commit comments

Comments
 (0)