如何获取Azure Synapse Analytics中所有Synapse Notebook名称列表?
获取Azure Synapse Analytics中所有Notebook名称列表的方案
方案1:使用Azure Synapse Python SDK(azure-synapse-artifacts)
这是最直接的代码实现方式,步骤如下:
- 安装依赖包:
pip install azure-synapse-artifacts azure-identity
- 编写Python代码遍历所有Notebook:
from azure.identity import DefaultAzureCredential from azure.synapse.artifacts import ArtifactsClient # 替换为你的Synapse工作区URL synapse_workspace_url = "https://<your-synapse-workspace-name>.dev.azuresynapse.net" # 初始化客户端 credential = DefaultAzureCredential() client = ArtifactsClient(endpoint=synapse_workspace_url, credential=credential) # 分页获取所有Notebook资源 notebooks = [] continuation_token = None while True: response = client.workspace.list_by_workspace( filter="type eq 'Notebook'", continuation_token=continuation_token ) notebooks.extend(response.value) continuation_token = response.continuation_token if not continuation_token: break # 提取所有Notebook名称 notebook_names = [nb.name for nb in notebooks] print("所有Notebook名称:") for name in notebook_names: print(name)
说明:DefaultAzureCredential会自动从环境变量、Azure CLI登录信息、托管身份等渠道获取认证凭据,适配本地开发和生产环境。
方案2:调用Azure REST API
如果不想依赖SDK,直接调用Synapse REST API也可实现:
- 获取Azure AD访问令牌(可通过Azure CLI执行
az account get-access-token --resource https://dev.azuresynapse.net获取) - 发送GET请求到以下端点,分页获取Notebook:
GET https://<your-synapse-workspace-name>.dev.azuresynapse.net/workspace?filter=type%20eq%20'Notebook'&api-version=2021-06-01
请求头需包含Authorization: Bearer <你的访问令牌>
3. 解析响应的value字段获取Notebook列表,若存在continuationToken,则循环调用直到获取全部数据。
内容的提问来源于stack exchange,提问作者coding
相关产品推荐
相关产品推荐

