大家好,我是程序员柏风,AI 最全的 Vibecoding 学习地图 http://coderoadmap.cn 作者。
综合练习
本课串联 键设计 → CRUD → GSI → Query → Streams 概念,设计 UserProgress 实验表。主库 cf_track 仍在 MySQL,DynamoDB 只承载 Serverless 实验数据。
学习目标
- 完成 UserProgress 表 PK/SK + GSI 设计与 CLI 实现
- 用 Query 实现用户进度列表与按课时反查
- 可选:Spring lab profile 暴露读写 API
核心内容
任务一:建表与键设计
需求:记录用户对课时的 view/complete,支持「某用户全部进度」「某课时全部用户」。
Table: CodeRoadMapLab-UserProgress
PK = USER#<userId>
SK = LESSON#<lessonSlug>
GSI ByLesson:
GSI1PK = LESSON#<lessonSlug>
GSI1SK = USER#<userId>
aws dynamodb create-table \
--table-name CodeRoadMapLab-UserProgress \
--attribute-definitions \
AttributeName=PK,AttributeType=S AttributeName=SK,AttributeType=S \
AttributeName=GSI1PK,AttributeType=S AttributeName=GSI1SK,AttributeType=S \
--key-schema AttributeName=PK,KeyType=HASH AttributeName=SK,KeyType=RANGE \
--global-secondary-indexes file://gsi-by-lesson.json \
--billing-mode PAY_PER_REQUEST
对照 MySQL:在 sql-essentials 写 user_progress(user_id, lesson_slug, action, ...) + 两个索引。
任务二:写入与查询
- PutItem 至少 20 条(3 user × 5 lesson,混合 view/complete)
- Query
PK=USER#u_1001,SK 降序 - Query GSI
GSI1PK=LESSON#ddb-crud,统计 complete 人数 - UpdateItem:
viewCount自增 +ConditionExpression防并发覆盖
任务三(可选):Spring + Streams
- ddb-spring:
UserProgressStore+/api/lab/progressGET/PUT - 启用 Stream,伪代码描述 complete 时异步更新「课时完成计数」表
- 不要修改 MySQL
TrackController或cf_track写入路径
验收清单
| 项 | 标准 |
|---|---|
| 无 Scan 主路径 | 用户列表、课时反查均 Query/GSI |
| 热点 | PK 按用户分散,无 GLOBAL# 单键 |
| 权威边界 | cf_track 只读对照,不写 DynamoDB 镜像 |
练习
- 完成任务一、二 CLI 脚本并保存为
lab-user-progress.sh - GSI Query 结果与手工计数一致
- 写 200 字:为何 progress 可实验放 DynamoDB、cf_track 必须留 MySQL
小结
UserProgress = PK/SK + GSI 最佳练手;MySQL 仍管 cf_track 权威数据。 恭喜完成 DynamoDB 入门 track!