Azure Resource Graph Python SDK中Skip Token始终为None无法实现分页的问题求助
Azure Resource Graph Python SDK中Skip Token始终为None无法实现分页的问题求助
问题核心分析
我注意到你遇到的关键矛盾点:明明查询返回结果显示result_truncated: true(结果被截断)、total_records(10070)远大于当前返回的count(1000),但skip_token却始终为None,导致无法触发后续分页。而添加了排序和额外过滤的查询却能正常生成skip_token完成分页。
本质原因其实非常明确:Azure Resource Graph服务仅会为稳定有序的查询结果集生成有效的skip_token。
为什么无排序的查询无法生成skip_token?
如果你的Kusto查询没有指定order by语句,返回结果的顺序是完全不确定的(依赖服务端临时的处理逻辑)。这种情况下,服务无法保证你后续的分页请求能准确获取未返回的数据(很可能出现重复或遗漏),因此不会生成有效的skip_token——这是服务为了避免数据一致性问题做出的设计。
对比你的两段查询就能找到差异:
- 有问题的查询:没有
order by语句,结果集顺序不稳定 - 可正常工作的查询:明确添加了
order by name asc,让结果集拥有了稳定的排序逻辑
解决方案:给查询添加稳定排序
只需要在你的查询语句末尾添加order by逻辑,指定一个稳定的字段(比如资源ID、名称等)即可,不需要修改其他代码逻辑。
修复后的查询示例
Resources | where type == "microsoft.network/networkinterfaces" | where isnotnull(managedBy) | mv-expand ipConfig = properties.ipConfigurations | where isnotnull(ipConfig.properties.privateLinkConnectionProperties.fqdns) | project fqdns = ipConfig.properties.privateLinkConnectionProperties.fqdns, id | order by id asc // 新增:按资源ID稳定排序,保证结果顺序唯一
对应的Python代码(可复用原有分页逻辑)
from azure.identity import DefaultAzureCredential from azure.mgmt.resourcegraph import ResourceGraphClient from azure.mgmt.resourcegraph.models import QueryRequest, QueryRequestOptions credential = DefaultAzureCredential() client = ResourceGraphClient(credential) query = """ Resources | where type == "microsoft.network/networkinterfaces" | where isnotnull(managedBy) | mv-expand ipConfig = properties.ipConfigurations | where isnotnull(ipConfig.properties.privateLinkConnectionProperties.fqdns) | project fqdns = ipConfig.properties.privateLinkConnectionProperties.fqdns, id | order by id asc """ fqdns = [] skip_token = None while True: request = QueryRequest( query=query, options=QueryRequestOptions( skip_token=skip_token, top=1000, ), ) response = client.resources(request) # 处理当前页数据 fqdns.extend(item["fqdns"] for item in response.data) # 更新skip_token,此时就能正常获取到值了 skip_token = response.skip_token if skip_token is None: break
额外补充说明
你在工作查询中添加的location过滤、array_length校验等条件,只是缩小了数据范围,并不是让分页生效的核心原因——真正起作用的是order by name asc这条排序语句。即使你不添加那些过滤条件,只要保留排序逻辑,服务就能正常生成skip_token。
内容来源于stack exchange
相关产品推荐
相关产品推荐

