跳到正文
Skillpedia
数据库48k · #3919

azure-cosmos-rust

它能做什么

这个工具可以让用 Rust 编写的程序与 Azure Cosmos DB NoSQL 数据库进行交互。你可以在其中创建、读取、更新和删除存储的信息。

适用场景

  • ✓当你的 Rust 程序需要保存或获取数据时。
  • ✓当你需要管理分布式数据库中的信息时。
  • ✓用于对 Cosmos DB 中的数据执行基本操作(创建、读取、更新、删除)。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/azure-cosmos-rust/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/azure-cosmos-rust/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“如何使用 Rust 在我的 Cosmos DB 容器中创建一个新条目?”

每周 5 个新技能,1 分钟读完。

工作原理

Rust 程序会使用一个专门的库(SDK)连接到 Azure Cosmos DB。它会定义数据应该如何组织,并向数据库发送命令,执行所需的操作。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Azure Cosmos DB SDK for Rust

Client library for Azure Cosmos DB NoSQL API — globally distributed, multi-model database.

The examples below require a reviewed compatible crate set and replacement of every resource placeholder. Do not use production data for demonstration writes.

Installation

cargo add azure_data_cosmos@=0.37.1 azure_identity@=1.0.0 azure_core@=1.1.0

Environment Variables

export COSMOS_ENDPOINT="https://<account>.documents.azure.com:443/"
export COSMOS_DATABASE="mydb"
export COSMOS_CONTAINER="mycontainer"

Authentication

use azure_identity::DeveloperToolsCredential;
use azure_data_cosmos::{AccountEndpoint, AccountReference, CosmosClient, RoutingStrategy};

let credential = DeveloperToolsCredential::new(None)?;
let endpoint: AccountEndpoint = "https://<account>.documents.azure.com/".parse()?;
let account = AccountReference::with_credential(endpoint, credential.clone());
let client = CosmosClient::builder()
    .build(account, RoutingStrategy::ProximityTo("East US".into()))
    .await?;

Client Hierarchy

Client Purpose Get From
CosmosClient Account-level operations Direct instantiation
DatabaseClient Database operations client.database_client()
ContainerClient Container/item operations database.container_client()

Core Workflow

Get Database and Container Clients

let database = client.database_client("myDatabase");
let container = database.container_client("myContainer", None).await?;

Create Item

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Item {
    pub id: String,
    pub partition_key: String,
    pub value: String,
}

let item = Item {
    id: "1".into(),
    partition_key: "partition1".into(),
    value: "hello".into(),
};

container.create_item("partition1", "1", item, None).await?;

Read Item

let response = container.read_item("partition1", "1", None).await?;
let item: Item = response.into_model()?;

Replace Item

let mut item: Item = container.read_item("partition1", "1", None).await?.into_model()?;
item.value = "updated".into();

container.replace_item("partition1", "1", item, None).await?;

Patch Item

// Explicit preview opt-in: cargo add azure_data_cosmos@=0.37.1 --features preview_patch
use azure_data_cosmos::models::{PatchInstructions, PatchOperation};

let patch = PatchInstructions::from(vec![
    PatchOperation::set("/value", serde_json::json!("newValue")),
]);
let patched: Item = container.patch_item("partition1", "1", patch, None)
    .await?.into_model()?;

Delete Item

container.delete_item("partition1", "1", None).await?;

The reviewed current API uses an async builder/container initialization and a separate explicit item ID. Preview PATCH is not a production-ready guarantee; review its selected strategy, conflict/retry behavior and request-unit cost. Official crate reference.

Key Auth (Optional)

Enable key-based authentication with feature flag:

cargo add azure_data_cosmos@=0.37.1 --features key_auth

Best Practices

  1. Always specify partition key — required for point reads and writes
  2. Use into_model()? — to deserialize responses into your types
  3. Derive Serialize and Deserialize — for all document types
  4. Use Entra ID auth — prefer DeveloperToolsCredential over key auth
  5. Reuse client instances — clients are thread-safe and reusable

The reviewed example set uses azure_data_cosmos 0.37.1 and compatible Azure core/credential crates. Record and commit Cargo.lock; source/API review is not a compiler or live-service test.

Reference Links

Resource Link
API Reference https://docs.rs/azure_data_cosmos
Source Code https://github.com/Azure/azure-sdk-for-rust/tree/main/sdk/cosmos/azure_data_cosmos
crates.io https://crates.io/crates/azure_data_cosmos

When to Use

Use when a Rust application needs Cosmos DB NoSQL item CRUD, partition-key point reads, container access or explicitly opted-in preview patching.

Limitations

…

常见问题

Cosmos DB 中的“容器”是什么?

你可以把容器想象成传统数据库中的表,用来存放条目(类似数据行)。

“CRUD”是什么意思?

CRUD 是 Create(创建)、Read(读取)、Update(更新)和 Delete(删除)的缩写,也就是最基本的数据操作。

使用这个工具需要掌握很多数据库知识吗?

了解数据库的基本工作原理会有所帮助,但这个库已经简化了许多复杂的交互过程。

详情

作者的原始描述

Azure Cosmos DB SDK for Rust (NoSQL API). Use for document CRUD, queries, containers, and globally distributed data.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#3919
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能