STRUCT
更新时间:2025-08-21
STRUCT<field_name:field_type [COMMENT 'comment_string'], ... > 表示由多个 Field 组成的结构体,也可被理解为多个列的集合。
- 不能作为 Key 使用,目前 STRUCT 仅支持在 Duplicate 模型的表中使用。
-
一个 Struct 中的 Field 的名字和数量固定,总是为 Nullable,一个 Field 通常由下面部分组成。
-field_name: Field 的标识符,不可重复
-field_type: Field 的类型
-COMMENT: Field 的注释,可选 (暂不支持)
当前可支持的类型有:
Plain Text
1BOOLEAN, TINYINT, SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLE, DECIMAL, DECIMALV3,
2DATE, DATEV2, DATETIME, DATETIMEV2, CHAR, VARCHAR, STRING
CSV 格式导入
第 1 步:准备数据
创建如下的 csv 文件:test_struct.csv 其中分隔符使用 | 而不是逗号,以便和 struct 中的逗号区分。
Plain Text
11|{10, 3.14, "Emily"}
22|{4, 1.5, null}
33|{7, null, "Benjamin"}
44|{}
55|null
第 2 步:在数据库中建表
Plain Text
1CREATE TABLE struct_test (
2 id INT NOT NULL,
3 c_struct STRUCT<f1:INT,f2:FLOAT,f3:STRING> NULL
4)
5DUPLICATE KEY(id)
6DISTRIBUTED BY HASH(id) BUCKETS 1
7PROPERTIES (
8 "replication_allocation" = "tag.location.default: 1"
9);
第 3 步:导入数据
Plain Text
1curl --location-trusted \
2 -u "root":"" \
3 -H "column_separator:|" \
4 -H "columns: id, c_struct" \
5 -T "test_struct.csv" \
6 http://localhost:8040/api/testdb/struct_test/_stream_load
第 4 步:检查导入数据
Plain Text
1mysql> SELECT * FROM struct_test;
2+------+--------------------------------------+
3| id | c_struct |
4+------+--------------------------------------+
5| 1 | {"f1":10, "f2":3.14, "f3":"Emily"} |
6| 2 | {"f1":4, "f2":1.5, "f3":null} |
7| 3 | {"f1":7, "f2":null, "f3":"Benjamin"} |
8| 4 | {"f1":null, "f2":null, "f3":null} |
9| 5 | NULL |
10+------+--------------------------------------+
115 rows in set (0.01 sec)
JSON 格式导入
第 1 步:准备数据
创建如下的 JSON 文件,test_struct.json
Plain Text
1[
2 {"id":1, "c_struct":{"f1":10, "f2":3.14, "f3":"Emily"}},
3 {"id":2, "c_struct":{"f1":4, "f2":1.5, "f3":null}},
4 {"id":3, "c_struct":{"f1":7, "f2":null, "f3":"Benjamin"}},
5 {"id":4, "c_struct":{}},
6 {"id":5, "c_struct":null}
7]
第 2 步:在数据库中建表
Plain Text
1CREATE TABLE struct_test (
2 id INT NOT NULL,
3 c_struct STRUCT<f1:INT,f2:FLOAT,f3:STRING> NULL
4)
5DUPLICATE KEY(id)
6DISTRIBUTED BY HASH(id) BUCKETS 1
7PROPERTIES (
8 "replication_allocation" = "tag.location.default: 1"
9);
第 3 步:导入数据
Plain Text
1curl --location-trusted \
2 -u "root":"" \
3 -H "format:json" \
4 -H "columns: id, c_struct" \
5 -H "strip_outer_array:true" \
6 -T "test_struct.json" \
7 http://localhost:8040/api/testdb/struct_test/_stream_load
第 4 步:检查导入数据
Plain Text
1mysql> SELECT * FROM struct_test;
2+------+--------------------------------------+
3| id | c_struct |
4+------+--------------------------------------+
5| 1 | {"f1":10, "f2":3.14, "f3":"Emily"} |
6| 2 | {"f1":4, "f2":1.5, "f3":null} |
7| 3 | {"f1":7, "f2":null, "f3":"Benjamin"} |
8| 4 | {"f1":null, "f2":null, "f3":null} |
9| 5 | NULL |
10+------+--------------------------------------+
115 rows in set (0.00 sec)
