Databricks、dbutils、获取 Azure Data Lake gen 2 路径中所有子文件夹的文件计数和文件大小
Databricks, dbutils, get filecount and filesize of all subfolders in Azure Data Lake gen 2 path
我在 Databricks notebook (pyspark) 中编写代码,并尝试使用 dbutils 获取特定 Azure Data Lake gen2 安装路径中所有子文件夹的文件数和文件大小。
我在特定文件夹中有它的代码,但我对如何编写递归部分感到困惑...
这个怎么样?
def deep_ls(path: str):
"""List all files in base path recursively."""
for x in dbutils.fs.ls(path):
if x.path[-1] is not '/':
yield x
else:
for y in deep_ls(x.path):
yield y
感谢
https://gist.github.com/Menziess/bfcbea6a309e0990e8c296ce23125059
从目录中获取文件列表,
使用以下代码打印并获取计数。
def get_dir_content(ls_path):
dir_paths = dbutils.fs.ls(ls_path)
subdir_paths = [get_dir_content(p.path) for p in dir_paths if p.isDir() and p.path != ls_path]
flat_subdir_paths = [p for subdir in subdir_paths for p in subdir]
return list(map(lambda p: p.path, dir_paths)) + flat_subdir_paths
paths = get_dir_content('dbfs:/')
或
paths = get_dir_content('abfss://')
下一行打印文件名,最后是路径和文件数。
len([print(p) for p in paths])
如果您只想计算文件数,请使用以下方法:
len([p for p in paths])
我在 Databricks notebook (pyspark) 中编写代码,并尝试使用 dbutils 获取特定 Azure Data Lake gen2 安装路径中所有子文件夹的文件数和文件大小。
我在特定文件夹中有它的代码,但我对如何编写递归部分感到困惑...
这个怎么样?
def deep_ls(path: str):
"""List all files in base path recursively."""
for x in dbutils.fs.ls(path):
if x.path[-1] is not '/':
yield x
else:
for y in deep_ls(x.path):
yield y
感谢
https://gist.github.com/Menziess/bfcbea6a309e0990e8c296ce23125059
从目录中获取文件列表, 使用以下代码打印并获取计数。
def get_dir_content(ls_path):
dir_paths = dbutils.fs.ls(ls_path)
subdir_paths = [get_dir_content(p.path) for p in dir_paths if p.isDir() and p.path != ls_path]
flat_subdir_paths = [p for subdir in subdir_paths for p in subdir]
return list(map(lambda p: p.path, dir_paths)) + flat_subdir_paths
paths = get_dir_content('dbfs:/')
或
paths = get_dir_content('abfss://')
下一行打印文件名,最后是路径和文件数。
len([print(p) for p in paths])
如果您只想计算文件数,请使用以下方法:
len([p for p in paths])