关于 eventhouse-consumption-cli
面向 Microsoft Fabric Eventhouse 数据探索的命令行技能,帮助 Agent 发现数据库结构并执行只读 KQL 查询。内容涵盖连接、身份验证、查询编写、结果处理和监控诊断。
主要功能
- 01通过 az CLI 发现 Fabric 工作区与项目
- 02执行只读 KQL 查询
- 03先发现表结构再生成查询
- 04提供查询性能与安全建议
- 05格式化及导出查询结果
- 06监控活动查询与命令
适用场景
- 01探索 Eventhouse 中未知的数据表
- 02按时间范围分析事件和指标
- 03诊断查询超时或空结果
- 04监控 Eventhouse 查询活动
MCP 相关
该技能建议在 Agent 集成工作流中使用 Fabric KQL MCP 服务;自身主要说明通过 az rest 执行查询。
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ microsoft/ skills-for-fabric --skill eventhouse-consumption-cli安装后在 Claude Code 中输入/eventhouse-consumption-cli确认已加载。
附带文件references/discovery-queries.md
详细介绍
更新检查 — 每个会话仅执行一次(必需) 首次在会话中使用此技能时,请先运行 check-updates 技能,再继续。
- GitHub Copilot CLI / VS Code:调用
check-updates技能。- Claude Code / Cowork / Cursor / Windsurf / Codex:比较本地与远程 package.json 的版本。
- 如果本会话中已执行过检查,则跳过。
重要提示
- 要根据工作区名称查找工作区详情(包括其 ID):先列出所有工作区,然后使用 JMESPath 进行筛选
- 要根据工作区 ID、项目类型和项目名称查找项目详情(包括其 ID):先列出该工作区中该类型的所有项目,然后使用 JMESPath 进行筛选
eventhouse-consumption-cli — 通过 CLI 执行只读 KQL 查询
目录
工具栈
| 工具 | 用途 | 安装 |
|---|---|---|
| az cli | 通过 Kusto REST API 执行 KQL 查询和管理命令;发现 Fabric 控制平面 | winget install Microsoft.AzureCLI |
| jq | JSON 处理和输出格式化 | winget install jqlang.jq |
连接
第 1 步 — 发现 KQL 数据库查询 URI
# Get workspace ID (if not known)
WS_ID=$(az rest --method GET \
--url "https://api.fabric.microsoft.com/v1/workspaces" \
--resource "https://api.fabric.microsoft.com" \
| jq -r '.value[] | select(.displayName=="MyWorkspace") | .id')
# List KQL Databases and get connection properties
az rest --method GET \
--url "https://api.fabric.microsoft.com/v1/workspaces/${WS_ID}/kqlDatabases" \
--resource "https://api.fabric.microsoft.com" \
| jq '.value[] | {name: .displayName, id: .id, queryUri: .properties.queryServiceUri, dbName: .properties.databaseName}'
第 2 步 — 设置连接变量
CLUSTER_URI="https://<cluster>.kusto.fabric.microsoft.com"
DB_NAME="MyKqlDatabase"
第 3 步 — 验证连接
重要 — 请求正文文件模式:KQL 查询包含
|(管道)字符,会破坏 bash 和 PowerShell 中的 shell 转义。务必将 JSON 请求正文写入临时文件,并通过--body @<file>引用。这是所有az restKQL 调用的推荐方式。 在 PowerShell 中,使用@{db="X";csl="..."} | ConvertTo-Json -Compress | Out-File $env:TEMP\kql_body.json -Encoding utf8NoBOM,然后使用--body "@$env:TEMP\kql_body.json"。
# Write body to temp file (avoids pipe escaping issues)
cat > /tmp/kql_body.json << 'EOF'
{"db":"MyKqlDatabase","csl":"print Message = 'Connected successfully', Cluster = current_cluster_endpoint(), Timestamp = now()"}
EOF
az rest --method POST \
--url "${CLUSTER_URI}/v1/rest/query" \
--resource "https://kusto.kusto.windows.net" \
--headers "Content-Type=application/json" \
--body @/tmp/kql_body.json \
| jq '.Tables[0].Rows'
Agent 式探索
“与我的数据聊天” — 发现序列
当用户要求探索或查询 Eventhouse,但未指定表时:
Step 1 → .show tables // discover tables
Step 2 → .show table <TABLE> schema as json // understand columns + types
Step 3 → <TABLE> | take 10 // see sample data
Step 4 → <TABLE> | summarize count() by bin(Timestamp, 1h) | render timechart // shape of data
Step 5 → Formulate targeted query based on user's question
感知架构的查询生成
发现架构后,使用实际的列名和类型生成查询:
// Example: user asks "show me errors in the last hour"
// After discovering table "AppEvents" with columns: Timestamp, Level, Message, Source
AppEvents
| where Timestamp > ago(1h)
| where Level == "Error"
| summarize ErrorCount = count() by Source, bin(Timestamp, 5m)
| order by ErrorCount desc
运行查询
通过 az rest
--body务必使用临时文件模式 — KQL 管道(|)会破坏内联 shell 转义。
# Run a KQL query
cat > /tmp/kql_body.json << 'EOF'
{"db":"MyDB","csl":"Events | where Timestamp > ago(1h) | count"}
EOF
az rest --method POST \
--url "${CLUSTER_URI}/v1/rest/query" \
--resource "https://kusto.kusto.windows.net" \
--headers "Content-Type=application/json" \
--body @/tmp/kql_body.json \
| jq '.Tables[0].Rows'
输出格式化
# Pretty-print results as a table with jq
cat > /tmp/kql_body.json << 'EOF'
{"db":"MyDB","csl":".show tables"}
EOF
az rest --method POST \
--url "${CLUSTER_URI}/v1/rest/query" \
--resource "https://kusto.kusto.windows.net" \
--headers "Content-Type=application/json" \
--body @/tmp/kql_body.json \
| jq '.Tables[0] | [.Columns[].ColumnName] as $cols | .Rows[] | [$cols, .] | transpose | map({(.[0]): .[1]}) | add'
# Save results to file
cat > /tmp/kql_body.json << 'EOF'
{"db":"MyDB","csl":"Events | where Timestamp > ago(1h) | summarize count() by EventType"}
EOF
az rest --method POST \
--url "${CLUSTER_URI}/v1/rest/query" \
--resource "https://kusto.kusto.windows.net" \
--headers "Content-Type=application/json" \
--body @/tmp/kql_body.json \
--output-file results.json
监控
// Active queries
.show queries
// Recent commands (last hour)
.show commands
| where StartedOn > ago(1h)
| project StartedOn, CommandType, Text = substring(Text, 0, 80), Duration, State
| order by StartedOn desc
// Ingestion failures (for context when data seems stale)
.show ingestion failures
| where FailedOn > ago(24h)
| summarize count() by ErrorCode
| top 5 by count_
必须 / 优先 / 避免 / 故障排除
必须
- 务必包含时间筛选条件 — 时间序列表中必须包含
where Timestamp > ago(...)。 - 查询前先发现架构 — 先运行
.show tables和.show table T schema as json。 - 使用
has搜索词项 — 已建立索引且速度快;仅在需要子字符串匹配时使用contains。 - 验证群集 URI — KQL 数据库 URI 按项目区分;务必通过 Fabric REST API 解析。
优先
- CLI 查询会话使用
az rest;与 Agent 集成的工作流使用 Fabric KQL MCP 服务。 - 尽早使用
project,在聚合前删除不需要的列。 - 当一个子表达式被多次使用时,使用
materialize()。 - 初步探索使用
take 100;避免扫描整张表。 - 时间序列使用
render timechart;分布使用render piechart。
避免
- 在大型表上使用
contains— 会进行全表扫描,且未建立索引。请使用has或has_cs。 - 未先筛选两侧数据就使用
join— 会导致内存激增。 - 在宽表上使用相当于
SELECT *的写法(project所有列)。 - 在时间序列
summarize中遗漏bin()— 会为每个唯一时间戳生成一行。 - 硬编码群集 URI — 务必从 Fabric REST API 或环境变量中解析。
故障排除
| 症状 | 修复方法 |
|---|---|
az rest 身份验证失败 | 先运行 az login;确保已设置 --resource "https://kusto.kusto.windows.net" |
| 有效表返回空结果 | 检查数据库上下文;可能需要使用 database("name").table |
| 查询超时 | 使用更严格的时间筛选条件;检查 .show queries,查看是否有冲突的查询 |
Forbidden (403) | 为 KQL 数据库申请 viewer 角色 |
| 结果被截断 | 默认上限为 500K 行;在查询前添加 set truncationmaxrecords = N; |
KQL 管道 | 导致 PowerShell 或 bash 出错 | 切勿在 --body 中内联 KQL。将 JSON 写入临时文件,并使用 --body @file.json(参见运行查询) |
示例
示例 1:发现并查询
# 1. Set connection variables (after discovering URI via Step 1)
CLUSTER_URI="https://<your-cluster>.kusto.fabric.microsoft.com"
DB_NAME="SalesDB"
# 2. Discover tables
cat > /tmp/kql_body.json << EOF
{"db":"${DB_NAME}","csl":".show tables"}
EOF
az rest --method POST \
--url "${CLUSTER_URI}/v1/rest/query" \
--resource "https://kusto.kusto.windows.net" \
--headers "Content-Type=application/json" \
--body @/tmp/kql_body.json \
| jq '.Tables[0].Rows'
# 3. Explore schema
cat > /tmp/kql_body.json << EOF
{"db":"${DB_NAME}","csl":".show table Orders schema as json"}
EOF
az rest --method POST \
--url "${CLUSTER_URI}/v1/rest/query" \
--resource "https://kusto.kusto.windows.net" \
--headers "Content-Type=application/json" \
--body @/tmp/kql_body.json \
| jq '.Tables[0].Rows'
# 4. Sample data
cat > /tmp/kql_body.json << EOF
{"db":"${DB_NAME}","csl":"Orders | take 10"}
EOF
az rest --method POST \
--url "${CLUSTER_URI}/v1/rest/query" \
--resource "https://kusto.kusto.windows.net" \
--headers "Content-Type=application/json" \
--body @/tmp/kql_body.json \
| jq '.Tables[0].Rows'
// 5. Analytical query (via az rest --body @file)
Orders
| where OrderDate > ago(30d)
| summarize
TotalOrders = count(),
TotalRevenue = sum(Amount)
by bin(OrderDate, 1d)
| render timechart
示例 2:跨数据库查询
// Query across KQL databases in the same Eventhouse
let orders = database("SalesDB").Orders | where OrderDate > ago(7d);
let products = database("CatalogDB").Products;
orders
| join kind=inner (products) on ProductId
| summarize Revenue = sum(Amount) by ProductName
| top 10 by Revenue desc
示例 3:将结果导出到文件
# Run query and save results to JSON
cat > /tmp/kql_body.json << 'EOF'
{"db":"MyDB","csl":"Events | where Timestamp > ago(1d) | summarize count() by EventType"}
EOF
az rest --method POST \
--url "${CLUSTER_URI}/v1/rest/query" \
--resource "https://kusto.kusto.windows.net" \
--headers "Content-Type=application/json" \
--body @/tmp/kql_body.json \
--output-file results.json
# Convert to CSV with jq
cat results.json \
| jq -r '.Tables[0] | (.Columns | map(.ColumnName)), (.Rows[]) | @csv' > results.csv
Agent 集成说明
- 此技能为只读——不会创建、修改或删除数据库对象。
- 对于创作操作(表管理、数据引入、策略),请委托给 eventhouse-authoring-cli。
- 对于跨工作负载编排(Spark + SQL + KQL),请委托给 FabricDataEngineer Agent。
- Fabric KQL MCP server(
mcp-setup/mcp-config-template.json中的fabric-kql)可用作az rest的替代方案,以执行集成到 Agent 中的查询。
评论
支持 Markdown