We have folders and subfolders in it with year,month, day folders in it. How can we get only the last leaf level folder list using dbutils.fs.ls utility?
Example path:
abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2021/ abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/ abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/03/24/15/a.parquet abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/03/25/15/b.parquet . .
The function should return only last leaf level folder list i.e
abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/03/24/15 abfss://cont@storage.dfs.core.windows.net/customer/data/V1/2022/03/25/15
EDIT:
I have tried below function and it works but it fails when some folder is empty with error "java.lang.UnsupportedOperationException: empty.reduceLeft". Please help.
def listLeafDirectories(path: String): Array[String] =
dbutils.fs.ls(path).map(file => {
// Work around double encoding bug
val path = file.path.replace("%25", "%").replace("%25", "%")
if (file.isDir) listLeafDirectories(path)
else Array[String](path.substring(0,path.lastIndexOf("/")+1))
}).reduce(_ ++ _).distinct