|
| 1 | +--- |
| 2 | +title: Accessing MySQL and Redis with Dictionaries |
| 3 | +--- |
| 4 | + |
| 5 | +In this tutorial, we’ll guide you through accessing MySQL and Redis data using dictionaries in Databend. You’ll learn how to create dictionaries that map to these external data sources, enabling seamless data querying and integration. |
| 6 | + |
| 7 | +## Before You Start |
| 8 | + |
| 9 | +Before you start, ensure that [Docker](https://www.docker.com/) is installed on your local machine. We need Docker to set up the necessary containers for Databend, MySQL, and Redis. You will also need a SQL client to connect to MySQL; we recommend using [BendSQL](/guides/sql-clients/bendsql/) to connect to Databend. |
| 10 | + |
| 11 | +## Step 1: Setting up Environment |
| 12 | + |
| 13 | +In this step, we’ll launch instances of Databend, MySQL, and Redis using Docker on your local machine. |
| 14 | + |
| 15 | +1. Create a Docker network named `mynetwork` to enable communication between your Databend, MySQL, and Redis containers: |
| 16 | + |
| 17 | +```bash |
| 18 | +docker network create mynetwork |
| 19 | +``` |
| 20 | + |
| 21 | +2. Run the following command to start a MySQL container named `mysql` within the `mynetwork` network: |
| 22 | + |
| 23 | +```bash |
| 24 | +docker run -d \ |
| 25 | + --name=mysql \ |
| 26 | + --network=mynetwork \ |
| 27 | + -e MYSQL_ROOT_PASSWORD=admin \ |
| 28 | + -p 3306:3306 \ |
| 29 | + mysql:latest |
| 30 | +``` |
| 31 | + |
| 32 | +3. Run the following command to start a Databend container named `databend` within the `mynetwork` network: |
| 33 | + |
| 34 | +```bash |
| 35 | +docker run -d \ |
| 36 | + --name=databend \ |
| 37 | + --network=mynetwork \ |
| 38 | + -p 3307:3307 \ |
| 39 | + -p 8000:8000 \ |
| 40 | + -p 8124:8124 \ |
| 41 | + -p 8900:8900 \ |
| 42 | + datafuselabs/databend:nightly |
| 43 | +``` |
| 44 | + |
| 45 | +4. Run the following command to start a Redis container named `redis` within the `mynetwork` network: |
| 46 | + |
| 47 | +```bash |
| 48 | +docker run -d \ |
| 49 | + --name=redis \ |
| 50 | + --network=mynetwork \ |
| 51 | + -p 6379:6379 \ |
| 52 | + redis:latest |
| 53 | +``` |
| 54 | + |
| 55 | +5. Verify that the Databend, MySQL, and Redis containers are connected to the same network by inspecting the `mynetwork` Docker network: |
| 56 | + |
| 57 | +```bash |
| 58 | +docker network inspect mynetwork |
| 59 | + |
| 60 | +[ |
| 61 | + { |
| 62 | + "Name": "mynetwork", |
| 63 | + "Id": "ba8984e9ca07f49dd6493fd7c8be9831bda91c44595fc54305fc6bc241a77485", |
| 64 | + "Created": "2024-09-23T21:24:34.59324771Z", |
| 65 | + "Scope": "local", |
| 66 | + "Driver": "bridge", |
| 67 | + "EnableIPv6": false, |
| 68 | + "IPAM": { |
| 69 | + "Driver": "default", |
| 70 | + "Options": {}, |
| 71 | + "Config": [ |
| 72 | + { |
| 73 | + "Subnet": "172.18.0.0/16", |
| 74 | + "Gateway": "172.18.0.1" |
| 75 | + } |
| 76 | + ] |
| 77 | + }, |
| 78 | + "Internal": false, |
| 79 | + "Attachable": false, |
| 80 | + "Ingress": false, |
| 81 | + "ConfigFrom": { |
| 82 | + "Network": "" |
| 83 | + }, |
| 84 | + "ConfigOnly": false, |
| 85 | + "Containers": { |
| 86 | + "14d50cc4d075158a6d5fa4e6c8b7db60960f8ba1f64d6bceff0692c7e99f37b5": { |
| 87 | + "Name": "redis", |
| 88 | + "EndpointID": "e1d1015fea745bbbb34c6a9fb11010b6960a139914b7cc2c6a20fbca4f3b77d8", |
| 89 | + "MacAddress": "02:42:ac:12:00:04", |
| 90 | + "IPv4Address": "172.18.0.4/16", |
| 91 | + "IPv6Address": "" |
| 92 | + }, |
| 93 | + "276bc1023f0ea999afc41e063f1f3fe7404cb6fbaaf421005d5c05be343ce5e5": { |
| 94 | + "Name": "databend", |
| 95 | + "EndpointID": "ac915b9df2fef69c5743bf16b8f07e0bb8c481ca7122b171d63fb9dc2239f873", |
| 96 | + "MacAddress": "02:42:ac:12:00:03", |
| 97 | + "IPv4Address": "172.18.0.3/16", |
| 98 | + "IPv6Address": "" |
| 99 | + }, |
| 100 | + "95c21de94d27edc5e6fa8e335e0fd5bff12557fa30889786de9f483b8d111dbc": { |
| 101 | + "Name": "mysql", |
| 102 | + "EndpointID": "44fdf40de8c3d4c8fec39eb03ef1219c9cf1548e9320891694a9758dd0540ce3", |
| 103 | + "MacAddress": "02:42:ac:12:00:02", |
| 104 | + "IPv4Address": "172.18.0.2/16", |
| 105 | + "IPv6Address": "" |
| 106 | + } |
| 107 | + }, |
| 108 | + "Options": {}, |
| 109 | + "Labels": {} |
| 110 | + } |
| 111 | +] |
| 112 | +``` |
| 113 | + |
| 114 | +## Step 2: Populating Sample Data |
| 115 | + |
| 116 | +In this step, we’ll add sample data to MySQL and Redis, and Databend. |
| 117 | + |
| 118 | +1. In Databend, create a table named `users_databend` and insert sample user data: |
| 119 | + |
| 120 | +```sql |
| 121 | +CREATE TABLE users_databend ( |
| 122 | + id INT, |
| 123 | + name VARCHAR(100) NOT NULL |
| 124 | +); |
| 125 | + |
| 126 | +INSERT INTO users_databend (id, name) VALUES |
| 127 | +(1, 'Alice'), |
| 128 | +(2, 'Bob'), |
| 129 | +(3, 'Charlie'); |
| 130 | +``` |
| 131 | + |
| 132 | +2. In MySQL, create a database named `dict`, create a `users` table, and insert sample data: |
| 133 | + |
| 134 | +```sql |
| 135 | +CREATE DATABASE dict; |
| 136 | +USE dict; |
| 137 | + |
| 138 | +CREATE TABLE users ( |
| 139 | + id INT AUTO_INCREMENT PRIMARY KEY, |
| 140 | + name VARCHAR(100) NOT NULL, |
| 141 | + email VARCHAR(100) NOT NULL |
| 142 | +); |
| 143 | + |
| 144 | +INSERT INTO users (name, email) VALUES |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +``` |
| 149 | + |
| 150 | +3. Find your Redis container ID on Docker Desktop or by running `docker ps` in the terminal: |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +4. Access the Redis CLI using your Redis container ID (replace `14d50cc4d075` with your actual container ID): |
| 155 | + |
| 156 | +```bash |
| 157 | +docker exec -it 14d50cc4d075 redis-cli |
| 158 | +``` |
| 159 | + |
| 160 | +5. Insert sample user data into Redis by running the following commands in the Redis CLI: |
| 161 | + |
| 162 | +```bash |
| 163 | +SET user:1 '{"notifications": "enabled", "theme": "dark"}' |
| 164 | +SET user:2 '{"notifications": "disabled", "theme": "light"}' |
| 165 | +SET user:3 '{"notifications": "enabled", "theme": "dark"}' |
| 166 | +``` |
| 167 | + |
| 168 | +## Step 3: Creating Dictionaries |
| 169 | + |
| 170 | +In this step, we'll create dictionaries for MySQL and Redis in Databend and then query data from these external sources. |
| 171 | + |
| 172 | +1. In Databend, create a dictionary named `mysql_users` in Databend that connects to the MySQL instance: |
| 173 | + |
| 174 | +```sql |
| 175 | +CREATE DICTIONARY mysql_users |
| 176 | +( |
| 177 | + id INT, |
| 178 | + name STRING, |
| 179 | + email STRING |
| 180 | +) |
| 181 | +PRIMARY KEY id |
| 182 | +SOURCE(MySQL( |
| 183 | + host='mysql' |
| 184 | + port=3306 |
| 185 | + username='root' |
| 186 | + password='admin' |
| 187 | + db='dict' |
| 188 | + table='users' |
| 189 | +)); |
| 190 | +``` |
| 191 | + |
| 192 | +2. Create a dictionary named `mysql_users` in Databend that connects to the Redis instance: |
| 193 | + |
| 194 | +```sql |
| 195 | +CREATE DICTIONARY redis_user_preferences |
| 196 | +( |
| 197 | + user_id STRING, |
| 198 | + preferences STRING |
| 199 | +) |
| 200 | +PRIMARY KEY user_id |
| 201 | +SOURCE(Redis( |
| 202 | + host='redis' |
| 203 | + port=6379 |
| 204 | +)); |
| 205 | +``` |
| 206 | + |
| 207 | +3. Query data from the MySQL and Redis dictionaries we created earlier. |
| 208 | + |
| 209 | +```sql |
| 210 | +SELECT |
| 211 | + u.id, |
| 212 | + u.name, |
| 213 | + DICT_GET(mysql_users, 'email', u.id) AS email, |
| 214 | + DICT_GET(redis_user_preferences, 'preferences', CONCAT('user:', TO_STRING(u.id))) AS user_preferences |
| 215 | +FROM |
| 216 | + users_databend AS u; |
| 217 | +``` |
| 218 | + |
| 219 | +The query above retrieves user information, including their ID and name from the `users_databend` table, along with their email from the MySQL dictionary and user preferences from the Redis dictionary. |
| 220 | + |
| 221 | +```sql title='Result:' |
| 222 | +┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ |
| 223 | +│ id │ name │ dict_get(default.mysql_users, 'email', u.id) │ dict_get(default.redis_user_preferences, 'preferences', CONCAT('user:', TO_STRING(u.id))) │ |
| 224 | +│ Nullable(Int32) │ String │ Nullable(String) │ Nullable(String) │ |
| 225 | +├─────────────────┼─────────┼──────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────┤ |
| 226 | +│ 1 │ Alice │ alice@example.com │ {"notifications": "enabled", "theme": "dark"} │ |
| 227 | +│ 2 │ Bob │ bob@example.com │ {"notifications": "disabled", "theme": "light"} │ |
| 228 | +│ 3 │ Charlie │ charlie@example.com │ {"notifications": "enabled", "theme": "dark"} │ |
| 229 | +└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ |
| 230 | +``` |
0 commit comments