DynamoDB:专业 NoSQL 架构与查询设计 - Openclaw Skills

作者:互联网

2026-03-30

AI教程

什么是 DynamoDB?

DynamoDB 技能为开发者提供了一个全面的框架,使用专业级模式设计和操作 Amazon DynamoDB。它专注于解决 Serverless 生态系统中的常见 NoSQL 挑战,如热分区、低效扫描以及复杂的数据关系。

通过在 Openclaw Skills 中使用此技能,用户可以确保其数据库架构在性能和成本上都得到优化。它涵盖了从基础键设计到单表设计和事务完整性等高级概念,是云原生应用开发的必备工具。

下载入口:https://github.com/openclaw/skills/tree/main/skills/ivangdavila/dynamodb

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install dynamodb

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 dynamodb。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

DynamoDB 应用场景

  • 设计在任何规模下都能保持稳定毫秒级延迟的可扩展后端。
  • 使用单表设计模式将关系型数据模型迁移到 NoSQL。
  • 通过优化读/写容量单位 (RCU/WCU) 来降低 AWS 运营成本。
  • 使用生存时间 (TTL) 属性实现自动化的数据保留策略。
  • 通过条件写入和事务确保分布式系统中的数据一致性。
DynamoDB 工作原理
  1. 定义访问模式,以确定用于数据分布的最佳分区键 (PK) 和用于范围查询的排序键 (SK)。
  2. 评估是否需要全局二级索引 (GSI) 作为替代搜索路径,以避免高昂的全表扫描。
  3. 使用 FilterExpressions 实现查询操作,在保持高性能的同时精炼结果。
  4. 将条件逻辑应用于写入操作,以处理乐观锁并防止数据覆盖。
  5. 通过使用 LastEvaluatedKey 的递归分页逻辑管理大型数据集,以获取完整结果集。

DynamoDB 配置指南

要使用此技能,请确保已安装 AWS CLI 并配置了访问 DynamoDB 资源的适当权限。

# 验证 AWS CLI 安装
aws --version

# 配置您的 AWS 环境
aws configure

确保您的 IAM 策略拥有目标表的必要 dynamodb:GetItemdynamodb:PutItemdynamodb:Querydynamodb:Scan 权限。

DynamoDB 数据架构与分类体系

该技能根据 DynamoDB 的严格结构限制和 Openclaw Skills 工作流的最佳实践管理数据:

属性 规格 开发者备注
项目大小 最大 400KB 将较大的 Blob 存储在 S3 中并引用 URI
读取限制 每次请求 1MB 针对大型数据集必须使用分页逻辑
分区限制 3000 RCU / 1000 WCU 使用高基数键以避免吞吐量受限
TTL Unix 时间戳 (秒) 后台进程;项目在过期后可能会短暂存在
name: DynamoDB
description: Design DynamoDB tables and write efficient queries avoiding common NoSQL pitfalls.
metadata: {"clawdbot":{"emoji":"?","requires":{"anyBins":["aws"]},"os":["linux","darwin","win32"]}}

Key Design

  • Partition key determines data distribution—high-cardinality keys spread load evenly
  • Hot partition = one key gets all traffic—use composite keys or add random suffix
  • Sort key enables range queries within partition—design for access patterns
  • Can't change keys after creation—model all access patterns before creating table

Query vs Scan

  • Query uses partition key + optional sort key—O(items in partition), always prefer
  • Scan reads entire table—expensive, slow, avoids indexes; almost never correct
  • "I need to filter by X" usually means missing GSI—add index, don't scan
  • FilterExpression applies AFTER read—still consumes full read capacity

Global Secondary Indexes

  • GSI = different partition/sort key—enables alternate access patterns
  • GSI is eventually consistent—writes propagate with slight delay
  • GSI consumes separate capacity—provision or pay for each GSI independently
  • Sparse index trick: only items with attribute appear in GSI

Single-Table Design

  • One table for multiple entity types—prefix partition key: USER#123, ORDER#456
  • Overloaded sort key: METADATA, ORDER#2024-01-15, ITEM#abc
  • Query returns mixed types—filter client-side or use begins_with
  • Not always right—start with access patterns, not doctrine

Pagination

  • Results capped at 1MB per request—must handle pagination
  • LastEvaluatedKey in response means more pages—pass as ExclusiveStartKey
  • Loop until LastEvaluatedKey is absent—common mistake: assume one call gets all
  • Limit limits evaluated items, not returned—still need pagination logic

Consistency

  • Reads are eventually consistent by default—may return stale data
  • ConsistentRead: true for strong consistency—costs 2x read capacity
  • GSI reads always eventually consistent—no strong consistency option
  • Write-then-read needs consistent read or retry—eventual consistency bites here

Conditional Writes

  • ConditionExpression for optimistic locking—fails if condition false
  • Prevent overwrites: attribute_not_exists(pk)
  • Version check: version = :expected then increment
  • ConditionCheckFailedException = retry with fresh data, don't just fail

Batch Operations

  • BatchWriteItem is NOT atomic—partial success possible, check UnprocessedItems
  • Retry unprocessed with exponential backoff—built into AWS SDK
  • Max 25 items per batch, 16MB total—split larger batches
  • No conditional writes in batch—use TransactWriteItems for atomicity

Transactions

  • TransactWriteItems for atomic multi-item writes—all or nothing
  • Max 100 items per transaction, 4MB total
  • TransactGetItems for consistent multi-read—snapshot isolation
  • 2x cost of normal operations—use only when atomicity required

TTL

  • Enable TTL on timestamp attribute—DynamoDB deletes expired items automatically
  • Deletion is background process—items may persist hours after expiration
  • TTL value is Unix epoch seconds—milliseconds silently fails
  • Filter attribute_exists(ttl) AND ttl > :now for queries if needed

Capacity

  • On-demand: pay per request, auto-scales—good for unpredictable traffic
  • Provisioned: set RCU/WCU, cheaper at scale—needs capacity planning
  • Provisioned with auto-scaling for predictable patterns—set min/max/target
  • ProvisionedThroughputExceededException = throttled—back off and retry

Limits

  • Item size max 400KB—store large objects in S3, reference in DynamoDB
  • Partition throughput: 3000 RCU, 1000 WCU—spread across partitions
  • Query/Scan returns max 1MB—pagination required for more
  • Attribute name max 64KB total per item—don't use long attribute names