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

如何通过编程检查Databricks任务集群是否启用Unity Catalog

检查Databricks任务集群的Unity Catalog启用状态

可以通过Databricks API结合Python实现该需求。jobs/get接口确实不直接返回Unity Catalog(UC)相关配置,需根据任务使用的集群类型分别处理:

场景1:任务绑定现有集群

若任务使用已创建的固定集群,调用clusters/get接口获取集群详情,其中unity_catalog_enabled字段直接标识UC是否启用。

代码实现

import requests

def check_existing_cluster_uc(host_name, auth_token, cluster_id):
    url = f"https://{host_name}/api/2.0/clusters/get"
    headers = {"Authorization": f"Bearer {auth_token}"}
    payload = {"cluster_id": cluster_id}
    
    resp = requests.get(url, headers=headers, json=payload)
    resp.raise_for_status()
    cluster_details = resp.json()
    
    return cluster_details.get("unity_catalog_enabled", False)

# 示例调用
workspace_host = "your-workspace.databricks.com"
personal_token = "your-pat-token"
target_job_id = "12345"

# 先获取任务基本信息
job_resp = requests.get(
    f"https://{workspace_host}/api/2.0/jobs/get?job_id={target_job_id}",
    headers={"Authorization": f"Bearer {personal_token}"}
)
job_resp.raise_for_status()
job_details = job_resp.json()

# 提取现有集群ID并检查UC状态
existing_cluster_id = job_details["settings"].get("existing_cluster_id")
if existing_cluster_id:
    uc_status = check_existing_cluster_uc(workspace_host, personal_token, existing_cluster_id)
    print(f"现有集群 {existing_cluster_id} 的UC启用状态: {uc_status}")
else:
    print("任务未使用现有集群,检查任务集群配置")

场景2:任务使用动态任务集群

若任务运行时自动创建临时集群,从jobs/get返回的job_clusters中提取集群配置,通过两种方式判断UC启用状态:

  1. 检查spark_conf中的spark.databricks.unityCatalog.enabled是否设为true
  2. 检查data_security_mode是否为SINGLE_USER或USER_ISOLATION(UC仅支持这两种安全模式)

代码实现

def check_job_cluster_uc(job_details):
    job_clusters = job_details["settings"].get("job_clusters", [])
    if not job_clusters:
        return None
    
    # 取第一个任务集群配置(多集群场景可按需遍历)
    cluster_spec = job_clusters[0]["new_cluster"]
    
    # 方式1:检查Spark配置参数
    spark_config = cluster_spec.get("spark_conf", {})
    uc_via_conf = spark_config.get("spark.databricks.unityCatalog.enabled") == "true"
    
    # 方式2:检查安全模式
    valid_uc_modes = {"SINGLE_USER", "USER_ISOLATION"}
    uc_via_mode = cluster_spec.get("data_security_mode") in valid_uc_modes
    
    return uc_via_conf or uc_via_mode

# 接场景1的示例代码
if not existing_cluster_id:
    uc_status = check_job_cluster_uc(job_details)
    print(f"任务集群的UC启用状态: {uc_status}")

关键注意事项

  • 确保你的个人访问令牌(PAT)拥有clusters:read和jobs:read权限
  • 对于旧版Databricks Runtime,unity_catalog_enabled字段可能缺失,此时优先通过安全模式+Spark配置判断
  • 若任务同时配置了现有集群和任务集群,需根据实际运行使用的集群类型对应检查

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 06:05:23