快速驱动数据工程
使用Cursor、uv和MCP操作真实的数据基础架构
该项目展示了 现代数据工程工作流程 使用以下方式管理数据库和基础架构 游标内的自然语言提示。您不用在CLI、SQL客户端和仪表板之间切换,而是使用 聊天操作 由...驱动 模型上下文协议(MCP).
在本项目结束时,您将:
- 使用提示设置PostgreSQL
- 通过对话方式管理模式和用户
- 加载和分析大型数据集
- 反思和可视化数据库结构
- 使用快速驱动开发构建dbt模型
- 运行数据质量测试并通过对话解决问题
- 在人工智能的帮助下记录数据模型
- 直接从Cursor聊天中执行分析
______________________________________________________________________
技术栈
| 工具 | 角色 |
|---|---|
| 光标 | AI原生IDE和ChatOps接口 |
| 紫外线 | 快速Python环境+MCP运行器 |
| Docker MCP | 通过提示进行基础设施控制 |
| Postgres MCP | SQL执行和模式自检 |
| dbt MCP | 转换、测试和文档 |
| PostgreSQL | 分析数据存储 |
| 二苯并噻吩 | 数据转换和建模框架 |
______________________________________________________________________
为什么这个项目很重要
传统的数据工程工作流程依赖于:
- CLI繁重的Docker命令
- 手动执行SQL
- 用于模式设计、分析和调试的单独工具
该项目引入了 提示原生数据工程模型:
- 基础设施即对话
- SQL作为对话
- 基于AI推理的模式设计
- 具有上下文感知的调试
这是 ChatOps在数据工程中的应用.
______________________________________________________________________
项目初始化(uv优先)
所有MCP服务器都在由管理的Python环境中运行 uv.
初始化项目
在项目文件夹中打开Cursor的集成终端并运行:
uv init这将创建:
pyproject.toml–依赖性和环境跟踪.gitignoreREADME.mdmain.py
______________________________________________________________________
安装并验证紫外线
检查是否 uv 已安装:
uv --version如果未安装:
curl -LsSf https://astral.sh/uv/install.sh | sh验证安装:
uv --versionuv 自动处理Python环境和依赖关系,无需手动设置virtualenv。______________________________________________________________________
配置游标MCP服务器
MCP服务器允许Cursor 执行真实的Docker和PostgreSQL操作.
MCP配置
将以下内容添加到Cursor的 工具和MCP 配置:
{
"mcpServers": {
"docker": {
"command": "uv",
"args": ["run", "--with", "docker-mcp", "docker-mcp", "--access-mode=unrestricted"]
},
"postgres": {
"command": "uv",
"args": ["run", "--with", "postgres-mcp", "postgres-mcp", "--access-mode=unrestricted"],
"env": {
"DATABASE_URI": "postgresql://app:app@localhost:5432/demo"
}
},
"dbt": {
"command": "uv",
"args": ["run", "--with", "dbt-postgres", "dbt-mcp"],
"env": {
"DBT_PROJECT_DIR": "./MCP_dbt"
}
}
}
}激活MCP
- 重新启动游标
- 打开 工具和MCP
- 确保 码头工人, Postgres,以及 二苯并噻吩 显示绿色指示器
______________________________________________________________________
通过Prompts进行数据工程
从这一点开始, 所有操作都发生在Cursor聊天中.
______________________________________________________________________
1️配置PostgreSQL(基础设施提示)
Using the Docker MCP, create a PostgreSQL 16 container named pg-local
on port 5432 with database demo and user/password app此提示:
- 提取PostgreSQL映像
- 创建并运行容器
- 初始化数据库
______________________________________________________________________
配置用户和架构
Using the Postgres MCP, create an application user named app,
grant it permissions on the demo database,
and create a schema called app owned by this user为什么这很重要
- 符合实际生产安全实践
- 将管理和应用程序访问分开
- 在妥协的情况下限制爆炸半径
______________________________________________________________________
加载大规模演示数据
该数据集模拟了一个生产电子商务系统:
- 2000名客户
- 1000个产品
- 10000个订单
- 30000+订单项目
加载数据:
cat demo_data.sql | docker exec -i pg-local psql -U app -d demo这模仿了 大量摄入,一项核心数据工程任务。
______________________________________________________________________
验证基础结构状态
Using the Docker MCP, list all running containers.
Then using the Postgres MCP, list all tables in the demo database.确认:
- 集装箱健康状况
- 数据库连接
- 模式正确性
______________________________________________________________________
模式反思与可视化
Using the Postgres MCP, read the database schema and
sample 3 rows from each table.
Create a Mermaid ER diagram with example values.
Render in chat only.替换:
- pgAdmin
- ER图工具
- 手册文档
______________________________________________________________________
dbt模型开发与测试
该项目包括 dbt项目 (MCP_dbt/)使用提示建立分析模型。
运行dbt模型
Using the DBT MCP, run the customer_analytics model
and summarize the results concisely in a table.这 customer_analytics 模型创建了一个 客户360视图 与:
- 寿命值计算
- 订单频率指标
- 客户细分(非活跃、偶尔、定期、VIP)
- 最喜欢的产品类别
- 客户生命周期指标
运行数据质量测试
Using the DBT MCP, run all tests for the customer_analytics model
and show me the results测试验证:
- NOT NULL约束
- 唯一约束
- 分段的可接受值
- 数据质量规则
使用AI辅助修复失败的测试
Fix the failing test(s) in my customer_analytics model. Update the model
file to resolve the issue and explain what you changed.光标将:
- 确定根本原因
- 更新SQL模型
- 重新运行测试以确认修复
- 解释所做的更改
使用AI的文档模型
Update my existing models/schema.yml file to add detailed documentation.
Keep the tests I already added, but enhance the descriptions for the
customer_analytics model and its columns.创建生产就绪文档,该文档:
- 解释业务逻辑
- 文档数据沿袭
- 明确栏目目的
- 帮助其他团队成员理解模型
______________________________________________________________________
示例分析提示
Show the top 10 customers by lifetime valueFind products with high order volume but low inventoryWhich countries generate the highest revenue per customer?Explain which indexes would improve order lookup performance光标将:
- 生成SQL
- 执行查询
- 解释结果
- 建议优化
______________________________________________________________________
这个项目展示了什么
- 快速驱动的基础设施配置
- 人工智能辅助模式和角色管理
- 会话式SQL分析
- 生产型数据库安全
- 通过提示进行dbt模型开发
- 人工智能辅助数据质量测试
- 带有解释的自动测试修复
- 数据模型的文档生成
- 数据工程工作流的ChatOps
______________________________________________________________________
数据库设置
PostgreSQL容器
该项目使用名为的PostgreSQL 16容器 pg-local 具有以下配置:
- 容器名称:
pg-local - 端口:5432(主机)→ 5432 (集装箱)
- 用户:
app - 密码:
app - 数据库:
demo - 模式:
app
数据库模式
这 app 架构包含以下表:
表格
- 客户
- customer_id (PK,整数) - email (varchar) - registration_date (日期) - country (varchar) - tier (varchar)
- 订单
- order_id (PK,整数) - customer_id (FK → 客户.customer_id) - order_date (日期) - status (varchar) - total_amount (数字) - region (varchar)
- 订单项目
- item_id (PK,整数) - order_id (FK → orders.order_id) - product_id (FK → products.product_id) - quantity (整数) - price (数字)
- 产品
- product_id (PK,整数) - name (varchar) - category (varchar) - price (数字) - stock_quantity (整数)
关系
- 客户→ 订单(一对多)
- 订单→ 订购项目(一对多)
- 产品→ 订购项目(一对多)
数据库优化
已创建以下索引以优化查询性能:
- idx_orders_customer_id -索引在
orders.customer_id(外键) - idx_order_items_order_id -索引在
order_items.order_id(外键) - idx_order_items_product_id -索引在
order_items.product_id(外键)
性能指标
- 缓存命中率: 95.51%
- 行数:
- 客户:2000 - 订单数量:10000 - 订单数量:30000 - 产品数量:1000
使用的提示
本节记录了用于设置和配置数据库的提示/命令:
1.创建PostgreSQL容器
Using the Docker MCP, create a PostgreSQL 16 container named
pg-local on port 5432 with user/password 'app' and database 'demo'2.配置数据库用户和架构
Using the Postgres MCP, configure the database user named 'app' with
password 'app', grant it all permissions on the demo database, and
create a schema called 'app' owned by this user3.列出容器和表格
Using the Docker MCP, list all running containers. Then using the
Postgres MCP, list all tables in the demo database.4.生成数据库架构图
Using the Postgres MCP, read the database schema and
sample 3 rows from each table. Create a Mermaid diagram
including the example values. Render the diagram in
the chat only, no files. Use a high-contrast style.5.数据库审计
Using PostgreSQL MCP, audit my database (app schema: customers, orders, order_items, products).
Run these checks:
1. EXPLAIN ANALYZE on a join query between orders and customers - show execution time and scan types
2. Missing indexes - check which tables lack indexes on foreign keys
3. Cache hit rate - query pg_stat_database
Present findings in a table format with columns: Issue | Impact | Priority
Then provide:
- Top 3 optimization recommendations with exact SQL to implement
Keep responses concise but include key metrics (execution times, cache hit %, row counts).6.创建推荐索引
Using the Postgres MCP, create ALL the indexes you recommended in your audit. Execute each CREATE INDEX statement and confirm when all indexes are created.7.运行dbt模型
Using the DBT MCP, run the customer_analytics model
and summarize the results concisely in a table.8.测试dbt模型
Using the DBT MCP, run all tests for the customer_analytics model
and show me the results9.修复失败的测试
Fix the failing test(s) in my customer_analytics model. Update the model
file to resolve the issue and explain what you changed.10.记录dbt模型
Update my existing models/schema.yml file to add detailed documentation.
Keep the tests I already added, but enhance the descriptions for the
customer_analytics model and its columns (customer_id, email,
lifetime_value, customer_segment, total_orders).
Make the descriptions clear and helpful for other data team members.用法
运行应用程序
python main.py连接到数据库
psql -h localhost -p 5432 -U app -d demo密码: app
使用MCP工具
本项目演示了MCP工具在以下方面的使用:
- Docker MCP:集装箱管理
- 创建和管理PostgreSQL容器 - 列出正在运行的容器 - 查看容器日志
- PostgreSQL MCP:数据库操作
- 执行SQL查询 - 分析查询性能(解释分析) - 数据库健康检查 - 索引建议 - 模式检查
- dbt MCP:数据转换和建模
- 运行dbt模型 - 执行数据质量测试 - 构建分析模型 - 生成文档 - 管理数据转换
数据库审核结果
对数据库进行了全面审计,确定:
发现的问题
| 问题 | 影响 | 优先级 |
|---|---|---|
| orders.customer_id缺少索引 | 对10000个订单进行顺序扫描 | 高 |
| order_items.order_id缺少索引 | 与30000个order_items连接缓慢 | 高 |
| order_items.product_id上缺少索引 | 产品查找的顺序扫描 | 中等 |
| 计划时间长(18.5ms) | 计划超过执行时间 | 中等 |
| 缓存命中率95.51% | 良好但可以提高 | 低 |
已应用优化
所有推荐的索引都是为了提高查询性能而创建的。
dbt项目结构
该项目包括一个位于 MCP_dbt/:
MCP_dbt/
├── models/
│ └── example/
│ ├── customer_analytics.sql # Customer 360 analytics model
│ ├── my_first_dbt_model.sql # Example model
│ ├── my_second_dbt_model.sql # Example model
│ └── schema.yml # Model documentation & tests
├── dbt_project.yml # dbt project configuration
└── profiles.yml # Database connection settings关键dbt型号
客户分析
一个全面的客户分析模型,提供:
- 终生价值:每位客户的总收入
- 订单指标:总订单、平均订单值、订单频率
- 分割:自动客户细分(非活跃、偶尔、定期、VIP)
- 产品偏好:每位客户最喜欢的产品类别
- 生命周期指标:注册后天数,客户寿命
应用的测试:
not_null关于临界柱unique关于customer_idaccepted_values论客户细分
最近的改进:
- 修复了无订单客户的NULL处理(lifetime_value默认为0)
- 通过详细的列描述增强文档
- 生产就绪模式文档
dbt文档命令
您可以从终端构建和查看此项目的dbt文档:
# From the project root
uv run dbt --project-dir MCP_dbt docs generate
uv run dbt --project-dir MCP_dbt docs serve --port 8080然后打开 http://localhost:8080 在浏览器中浏览:
- 模型文档(包括
customer_analytics) - 列级描述
- 测试和谱系
发展
Python版本
本项目使用Python 3.14(在 .python-version).
依赖项
看 pyproject.toml 对于项目依赖关系。该项目使用:
docker-mcp用于Docker操作postgres-mcp用于PostgreSQL操作dbt-postgres用于dbt转换
dbt配置
dbt项目连接到 demo 数据库使用 app 用户。连接详细信息在中配置 MCP_dbt/profiles.yml.
