You need to enable JavaScript to run this app. 如何使用Pyspark删除CosmosDB中的顶点
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Pyspark删除CosmosDB中的顶点

用PySpark删除Cosmos DB中图顶点的操作方法

可以通过Azure Cosmos DB OLTP Spark连接器原生提供的删除操作实现硬删除,直接移除无法被覆盖、带删除标记的UUID顶点,具体操作如下:

首先复用你原有的连接配置,先查询出需要删除的顶点的必要字段:

cfg = {
    "spark.cosmos.accountEndpoint" : "xx:443/",
    "spark.cosmos.accountKey" : "xx==",
    "spark.cosmos.database" : "graphdb",
    "spark.cosmos.container" : "graph"
}

# 仅查询待删除顶点的id和容器分区键字段即可,此处假设你的删除标记字段为is_deleted,可根据实际业务字段修改查询条件
df_to_delete = spark.read.format("cosmos.oltp").options(**cfg) \
    .option("spark.cosmos.read.customQuery", "select c.id, c.label FROM c where c.is_deleted = true and c.label = 'Email'") \
    .load()

再执行删除操作:

# 批量删除待清理的顶点
df_to_delete.write.format("cosmos.oltp") \
    .options(**cfg) \
    .mode("append") \
    .option("spark.cosmos.write.operation", "Delete") \
    .save()

注意事项

  • 待删除的DataFrame必须包含顶点的id字段和当前容器配置的分区键字段,两个字段缺一不可,否则会删除失败
  • 如果需要删除大量顶点,可在配置中添加"spark.cosmos.write.bulk.enabled": "true"开启批量写入优化,提升删除效率
  • 删除操作为物理硬删除,执行后顶点会直接从容器中移除,不会残留删除标记

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 17:30:01