Skip to content

Commit 6da08c9

Browse files
committed
fix: Restore 'access-mysql-and-redis' tutorial and update its title format
1 parent 47d14f3 commit 6da08c9

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed

docs/cn/tutorials/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ description: 查找覆盖连接、摄取、迁移、开发与运维 Databend 的
1919
- [使用 Bend Ingest 导入 Kafka 数据](/tutorials/ingest-and-stream/kafka-bend-ingest-kafka)
2020
- [使用 Kafka Connect 导入 Kafka 数据](/tutorials/ingest-and-stream/kafka-databend-kafka-connect)
2121
- [使用 Vector 同步 JSON 日志](/tutorials/ingest-and-stream/automating-json-log-loading-with-vector)
22+
- [挂载 MySQL / Redis 外部字典](/tutorials/ingest-and-stream/access-mysql-and-redis)
2223
- [查询 Stage 文件元数据](/tutorials/ingest-and-stream/query-metadata)
2324

2425
## 数据迁移
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
title: 挂载 MySQL / Redis 外部字典
3+
sidebar_label: 挂载 MySQL / Redis 字典
4+
---
5+
6+
本教程将演示如何在 Databend 中通过 Dictionary 访问 MySQL 与 Redis 数据。你将学习如何为外部数据源创建 Dictionary,并像查询本地表一样无缝读取这些数据。
7+
8+
## 开始之前
9+
10+
请在本地安装 [Docker](https://www.docker.com/),用于启动 Databend、MySQL 与 Redis 容器。同时需要一个连接 MySQL 的 SQL 客户端,推荐使用 [BendSQL](/guides/sql-clients/bendsql/) 连接 Databend。
11+
12+
## 步骤 1:搭建环境
13+
14+
本步骤会在本地通过 Docker 启动 Databend、MySQL 与 Redis。
15+
16+
1. 创建名为 `mynetwork` 的 Docker 网络,供各容器互通:
17+
18+
```bash
19+
docker network create mynetwork
20+
```
21+
22+
2. 在该网络内启动名为 `mysql` 的 MySQL 容器:
23+
24+
```bash
25+
docker run -d \
26+
--name=mysql \
27+
--network=mynetwork \
28+
-e MYSQL_ROOT_PASSWORD=admin \
29+
-p 3306:3306 \
30+
mysql:latest
31+
```
32+
33+
3. 启动名为 `databend` 的 Databend 容器:
34+
35+
```bash
36+
docker run -d \
37+
--name=databend \
38+
--network=mynetwork \
39+
-p 3307:3307 \
40+
-p 8000:8000 \
41+
-p 8124:8124 \
42+
-p 8900:8900 \
43+
datafuselabs/databend:nightly
44+
```
45+
46+
4. 启动名为 `redis` 的 Redis 容器:
47+
48+
```bash
49+
docker run -d \
50+
--name=redis \
51+
--network=mynetwork \
52+
-p 6379:6379 \
53+
redis:latest
54+
```
55+
56+
5. 检查 `mynetwork`,确认三个容器都在同一网络:
57+
58+
```bash
59+
docker network inspect mynetwork
60+
```
61+
62+
输出示例:
63+
64+
```bash
65+
[
66+
{
67+
"Name": "mynetwork",
68+
...
69+
"Containers": {
70+
"14d50cc4d075158a6d5fa4e6c8b7db60960f8ba1f64d6bceff0692c7e99f37b5": {
71+
"Name": "redis",
72+
...
73+
},
74+
"276bc1023f0ea999afc41e063f1f3fe7404cb6fbaaf421005d5c05be343ce5e5": {
75+
"Name": "databend",
76+
...
77+
},
78+
"95c21de94d27edc5e6fa8e335e0fd5bff12557fa30889786de9f483b8d111dbc": {
79+
"Name": "mysql",
80+
...
81+
}
82+
},
83+
...
84+
}
85+
]
86+
```
87+
88+
## 步骤 2:准备示例数据
89+
90+
本步骤将在 Databend、MySQL 与 Redis 中写入示例数据。
91+
92+
1. 在 Databend 中创建 `users_databend` 表并插入示例数据:
93+
94+
```sql
95+
CREATE TABLE users_databend (
96+
id INT,
97+
name VARCHAR(100) NOT NULL
98+
);
99+
100+
INSERT INTO users_databend (id, name) VALUES
101+
(1, 'Alice'),
102+
(2, 'Bob'),
103+
(3, 'Charlie');
104+
```
105+
106+
2. 在 MySQL 中创建 `dict` 数据库与 `users` 表,并插入示例数据:
107+
108+
```sql
109+
CREATE DATABASE dict;
110+
USE dict;
111+
112+
CREATE TABLE users (
113+
id INT AUTO_INCREMENT PRIMARY KEY,
114+
name VARCHAR(100) NOT NULL,
115+
email VARCHAR(100) NOT NULL
116+
);
117+
118+
INSERT INTO users (name, email) VALUES
119+
('Alice', '[email protected]'),
120+
('Bob', '[email protected]'),
121+
('Charlie', '[email protected]');
122+
```
123+
124+
3. 通过 Docker Desktop 或运行 `docker ps` 找到 Redis 容器 ID:
125+
126+
![alt text](../../../../static/img/documents/tutorials/redis-container-id.png)
127+
128+
4. 使用容器 ID 进入 Redis CLI(将 `14d50cc4d075` 替换为实际 ID):
129+
130+
```bash
131+
docker exec -it 14d50cc4d075 redis-cli
132+
```
133+
134+
5. 在 Redis CLI 中插入示例数据:
135+
136+
```bash
137+
SET user:1 '{"notifications": "enabled", "theme": "dark"}'
138+
SET user:2 '{"notifications": "disabled", "theme": "light"}'
139+
SET user:3 '{"notifications": "enabled", "theme": "dark"}'
140+
```
141+
142+
## 步骤 3:创建 Dictionary
143+
144+
本步骤将在 Databend 中为 MySQL 与 Redis 创建 Dictionary,并通过查询提取外部数据。
145+
146+
1. 在 Databend 中创建名为 `mysql_users` 的 Dictionary 指向 MySQL:
147+
148+
```sql
149+
CREATE DICTIONARY mysql_users
150+
(
151+
id INT,
152+
name STRING,
153+
email STRING
154+
)
155+
PRIMARY KEY id
156+
SOURCE(MySQL(
157+
host='mysql'
158+
port=3306
159+
username='root'
160+
password='admin'
161+
db='dict'
162+
table='users'
163+
));
164+
```
165+
166+
2. 创建名为 `redis_user_preferences` 的 Dictionary 指向 Redis:
167+
168+
```sql
169+
CREATE DICTIONARY redis_user_preferences
170+
(
171+
user_id STRING,
172+
preferences STRING
173+
)
174+
PRIMARY KEY user_id
175+
SOURCE(Redis(
176+
host='redis'
177+
port=6379
178+
));
179+
```
180+
181+
3. 查询两个 Dictionary:
182+
183+
```sql
184+
SELECT
185+
u.id,
186+
u.name,
187+
DICT_GET(mysql_users, 'email', u.id) AS email,
188+
DICT_GET(redis_user_preferences, 'preferences', CONCAT('user:', TO_STRING(u.id))) AS user_preferences
189+
FROM
190+
users_databend AS u;
191+
```
192+
193+
该查询会返回用户的 ID、姓名,同时通过 MySQL Dictionary 获取 email,通过 Redis Dictionary 获取偏好设置。
194+
195+
```sql title='Result:'
196+
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
197+
│ id │ name │ dict_get(default.mysql_users, 'email', u.id) │ dict_get(default.redis_user_preferences, 'preferences', CONCAT('user:', TO_STRING(u.id))) │
198+
│ Nullable(Int32) │ String │ Nullable(String) │ Nullable(String) │
199+
├─────────────────┼─────────┼──────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────┤
200+
1 │ Alice │ alice@example.com │ {"notifications": "enabled", "theme": "dark"} │
201+
2 │ Bob │ bob@example.com │ {"notifications": "disabled", "theme": "light"} │
202+
3 │ Charlie │ charlie@example.com │ {"notifications": "enabled", "theme": "dark"} │
203+
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
204+
```

0 commit comments

Comments
 (0)