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

Azure Databricks写入Cosmos DB Gremlin API遇类未找到错误求助

解决Azure Databricks写入Cosmos DB Gremlin API时的ClassNotFoundException问题

问题背景

使用Azure Databricks 10.4 LTS(Apache Spark 3.2.1、Scala 2.12)集群,安装com.azure.cosmos.spark:azure-cosmos-spark_3-2_2-12:4.11.1库,尝试将顶点写入Cosmos DB Gremlin API时触发ClassNotFoundException,提示找不到数据源com.microsoft.azure.cosmosdb.spark。

错误核心原因

你安装的是新版Azure Cosmos Spark SDK(com.azure.cosmos.spark开头),但代码中使用的是旧版SDK的数据源类名com.microsoft.azure.cosmosdb.spark,新旧SDK的数据源标识、配置参数均不兼容。

解决方案

1. 修正数据源Format类名

新版SDK针对Gremlin API的数据源类名为com.azure.cosmos.spark.sql.connector.gremlin

2. 更新配置参数

新版SDK的参数名有变更,需调整配置字典:

  • 将Masterkey改为key(对应参数名spark.cosmos.accountKey)
  • 将Collection改为container(对应参数名spark.cosmos.container)
  • 新增标签参数:写入顶点时指定spark.cosmos.gremlin.vertexLabel,写入边时指定spark.cosmos.gremlin.edgeLabel
  • 替换Upsert为spark.cosmos.write.upsertEnabled

修改后的代码示例

cosmosDbConfig = {
  "spark.cosmos.accountEndpoint": "https://xxxxxxxx.gremlin.documents.azure.com:443/",
  "spark.cosmos.accountKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "spark.cosmos.database": "sample-database",
  "spark.cosmos.container": "sample-graph",
  "spark.cosmos.gremlin.vertexLabel": "your-vertex-label", # 替换为实际顶点标签
  "spark.cosmos.write.upsertEnabled": "true"
}

cosmosDbFormat = "com.azure.cosmos.spark.sql.connector.gremlin"

cosmosDbVertices.write.format(cosmosDbFormat).mode("append").options(**cosmosDbConfig).save()

额外排查点

  • 确认集群已在安装库后重启,确保新库生效
  • 检查集群是否存在旧版com.microsoft.azure.cosmosdb.spark库,如有需删除避免版本冲突
  • 写入边时,需将配置中的spark.cosmos.gremlin.vertexLabel替换为spark.cosmos.gremlin.edgeLabel,并确保DataFrame包含边的sourceId、targetId必填字段

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 08:00:30