如何在Azure Data Lake中按时间范围计算数据大小并筛选文件
筛选Azure Data Lake特定时间范围文件并统计总大小
方法一:使用Azure CLI
- 先登录Azure账号:
az login - 切换到目标订阅:
az account set --subscription <你的订阅ID> - 执行递归筛选+统计命令(以2024年1月1日至2月1日为例):
az storage fs file list -f <文件系统名称> --account-name <存储账户名称> --recursive --query "[?properties.lastModified >= '2024-01-01T00:00:00Z' && properties.lastModified <= '2024-02-01T23:59:59Z'].properties.contentLength" --output tsv | awk '{sum+=$1} END {print sum/1024/1024/1024 " GB"}'
说明:通过--query过滤修改时间范围,提取文件大小后用awk求和并转换为GB单位。
方法二:使用PowerShell
- 若未安装Az模块,先执行:
Install-Module -Name Az.Storage -Force - 登录并切换订阅:
Connect-AzAccount Set-AzContext -Subscription <订阅ID>
- 运行统计脚本:
$storageAccountName = "<存储账户名称>" $fileSystemName = "<文件系统名称>" $startTime = Get-Date "2024-01-01" $endTime = Get-Date "2024-02-01" # 递归获取符合时间条件的文件 $targetFiles = Get-AzDataLakeGen2ChildItem -FileSystem $fileSystemName -AccountName $storageAccountName -Recurse | Where-Object { $_.LastWriteTime -ge $startTime -and $_.LastWriteTime -le $endTime } # 计算总大小并转换为GB $totalBytes = ($targetFiles | Measure-Object -Property Length -Sum).Sum $totalGB = [math]::Round($totalBytes / 1GB, 2) Write-Host "指定时间范围内文件总占用空间: $totalGB GB"
方法三:使用Azure Storage Explorer(可视化操作)
- 打开Azure Storage Explorer,连接到你的ADLS Gen2存储账户,找到目标文件系统。
- 右键点击目标文件夹,选择搜索。
- 在搜索面板的筛选器中,选择修改时间,设置起始和结束时间后点击搜索。
- 选中所有搜索结果文件,右键选择属性,在弹出窗口中即可查看总大小。
方法四:使用Python SDK
- 先安装依赖包:
pip install azure-storage-file-datalake azure-identity - 编写统计脚本:
from azure.storage.filedatalake import DataLakeServiceClient from azure.identity import DefaultAzureCredential from datetime import datetime # 配置参数 storage_account = "<存储账户名称>" file_system = "<文件系统名称>" start_dt = datetime(2024, 1, 1) end_dt = datetime(2024, 2, 1) # 初始化客户端 credential = DefaultAzureCredential() service_client = DataLakeServiceClient(account_url=f"https://{storage_account}.dfs.core.windows.net", credential=credential) fs_client = service_client.get_file_system_client(file_system=file_system) total_size = 0 # 递归遍历文件系统 def scan_directory(dir_client): global total_size for path in dir_client.get_paths(): if path.is_file: # 转换时间格式并判断是否在范围内 modified_time = path.last_modified.replace(tzinfo=None) if start_dt <= modified_time <= end_dt: total_size += path.content_length else: # 递归处理子目录 sub_dir_client = dir_client.get_sub_directory_client(path.name) scan_directory(sub_dir_client) # 从根目录开始扫描 scan_directory(fs_client.get_directory_client("")) # 输出总大小(转换为GB) print(f"指定时间范围文件总占用空间: {round(total_size / (1024**3), 2)} GB")
内容的提问来源于stack exchange,提问作者Shikha
相关产品推荐
相关产品推荐

