You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用KQL解析Azure存储URI中分层命名空间的文件名?

通用提取ADLS Gen2 Blob目录与文件名的KQL方法

针对启用分层命名空间的ADLS Gen2存储账户,无需为每个容器定制处理逻辑,可通过以下通用KQL脚本提取Blob的容器名、目录路径、文件名等信息:

// Define variables
let varStartTime            = todatetime('2022-10-02T18:00:00Z');
let varEndTime              = todatetime('2022-10-02T20:00:00Z');
let varAccountName          = 'stgtest';
//
StorageBlobLogs
| where
    AccountName == varAccountName
    and TimeGenerated between (varStartTime .. varEndTime)
// 解析Uri获取路径部分,拆分路径段
| extend PathParts = split(parse_url(Uri).Path, '/')
// 移除split后产生的空字符串(因路径以/开头)
| extend PathParts = array_remove(PathParts, "")
// 提取各层级信息
| extend 
    ContainerName = PathParts[0],
    FullBlobPath = strcat_array(array_slice(PathParts, 1, -1), "/"),
    FileName = array_last(PathParts),
    DirectoryPath = strcat_array(array_slice(PathParts, 1, -2), "/")
// 处理无嵌套目录的场景,将DirectoryPath设为空字符串
| extend DirectoryPath = case(array_length(PathParts) <= 2, "", DirectoryPath)
| project
    TimeGenerated,
    AccountName,
    ContainerName,
    DirectoryPath,
    FileName,
    FullBlobPath,
    OperationName
| sort by TimeGenerated asc

关键逻辑说明:

  • parse_url(Uri).Path:从Uri中提取路径部分(例如/container/dir1/dir2/file.txt)
  • split(..., '/') + array_remove:拆分路径并移除开头的空字符串,得到干净的路径段数组(例如["container", "dir1", "dir2", "file.txt"])
  • array_slice:截取容器名之后的路径段,区分目录层级与文件名
  • array_last:直接获取数组最后一个元素作为文件名,无论嵌套层级多少
  • case 语句:处理文件直接存放在容器根目录的场景,此时目录路径为空

内容的提问来源于stack exchange,提问作者ericOnline

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 12:15:44