集群

创建一个集群

请参考 Kubernetes 以获得关于创建 Kubernetes 集群的信息。

分布式读/写

SQL

可以按照步骤使用 SQL 来进行分布式插入和查询:

  1. 使用 MySQL cli 连接到 Frontend。

    shell

    1. mysql -h 127.0.0.1 -P 4002
  2. 通过 CREATE 语句创建一个分布式表。

    SQL

    1. CREATE TABLE dist_table(
    2. ts TIMESTAMP DEFAULT current_timestamp(),
    3. n INT,
    4. row_id INT,
    5. PRIMARY KEY(n),
    6. TIME INDEX (ts)
    7. )
    8. PARTITION BY RANGE COLUMNS (n) (
    9. PARTITION r0 VALUES LESS THAN (5),
    10. PARTITION r1 VALUES LESS THAN (9),
    11. PARTITION r2 VALUES LESS THAN (MAXVALUE),
    12. )
    13. engine=mito;

    结果如下:

    shell

    1. mysql> CREATE TABLE dist_table(
    2. -> ts TIMESTAMP DEFAULT current_timestamp(),
    3. -> n INT,
    4. -> row_id INT,
    5. -> TIME INDEX (ts)
    6. -> TIME INDEX (ts)
    7. -> )
    8. -> PARTITION BY RANGE COLUMNS (n) (
    9. -> PARTITION r0 VALUES LESS THAN (5),
    10. -> PARTITION r1 VALUES LESS THAN (9),
    11. -> PARTITION r2 VALUES LESS THAN (MAXVALUE),
    12. -> )
    13. -> engine=mito;
    14. Query OK, 3 rows affected (0.09 sec)

    dist_table 被分布在 Datanode 中。你可以参考 “Table Sharding” 了解更多细节。

  3. 通过 INSERT 语句输入一些数据。

    SQL

    1. INSERT INTO dist_table(n, row_id) VALUES (1, 1);
    2. INSERT INTO dist_table(n, row_id) VALUES (2, 2);
    3. INSERT INTO dist_table(n, row_id) VALUES (3, 3);
    4. INSERT INTO dist_table(n, row_id) VALUES (4, 4);
    5. INSERT INTO dist_table(n, row_id) VALUES (5, 5);
    6. INSERT INTO dist_table(n, row_id) VALUES (6, 6);
    7. INSERT INTO dist_table(n, row_id) VALUES (7, 7);
    8. INSERT INTO dist_table(n, row_id) VALUES (8, 8);
    9. INSERT INTO dist_table(n, row_id) VALUES (9, 9);
    10. INSERT INTO dist_table(n, row_id) VALUES (10, 10);
    11. INSERT INTO dist_table(n, row_id) VALUES (11, 11);
    12. INSERT INTO dist_table(n, row_id) VALUES (12, 12);
  4. 通过 SELECT 语句执行查询:

    sql

    1. SELECT * FROM dist_table ORDER BY n LIMIT 5;

    sql

    1. +---------------------+------+--------+
    2. | ts | n | row_id |
    3. +---------------------+------+--------+
    4. | 2022-11-14 12:02:32 | 1 | 1 |
    5. | 2022-11-14 12:02:32 | 2 | 2 |
    6. | 2022-11-14 12:02:32 | 3 | 3 |
    7. | 2022-11-14 12:02:32 | 4 | 4 |
    8. | 2022-11-14 12:02:32 | 5 | 5 |
    9. +---------------------+------+--------+
    10. 5 rows in set (0.081 sec)

    sql

    1. SELECT MAX(n) FROM dist_table;

    sql

    1. +-------------------+
    2. | MAX(dist_table.n) |
    3. +-------------------+
    4. | 12 |
    5. +-------------------+
    6. 1 row in set (0.057 sec)

    sql

    1. SELECT MIN(n) FROM dist_table;

    sql

    1. +-------------------+
    2. | MIN(dist_table.n) |
    3. +-------------------+
    4. | 1 |
    5. +-------------------+
    6. 1 row in set (0.079 sec)

    sql

    1. SELECT * FROM dist_table WHERE n > 2 AND n < 10 ORDER BY row_id;

    sql

    1. +---------------------+------+--------+
    2. | ts | n | row_id |
    3. +---------------------+------+--------+
    4. | 2022-11-14 12:02:32 | 3 | 3 |
    5. | 2022-11-14 12:02:32 | 4 | 4 |
    6. | 2022-11-14 12:02:32 | 5 | 5 |
    7. | 2022-11-14 12:02:32 | 6 | 6 |
    8. | 2022-11-14 12:02:32 | 7 | 7 |
    9. | 2022-11-14 12:02:32 | 8 | 8 |
    10. | 2022-11-14 12:02:32 | 9 | 9 |
    11. +---------------------+------+--------+
    12. 7 rows in set (0.02 sec)

    sql

    1. SELECT * FROM dist_table WHERE row_id = 10;

    sql

    1. +---------------------+------+--------+
    2. | ts | n | row_id |
    3. +---------------------+------+--------+
    4. | 2022-11-14 12:02:32 | 10 | 10 |
    5. +---------------------+------+--------+
    6. 1 row in set (0.03 sec)