STRUCT

name

SinceVersion 2.0.0

STRUCT

description

STRUCT<field_name:field_type [COMMENT 'comment_string'], ... >

由多个 Field 组成的结构体,也可被理解为多个列的集合。不能作为 Key 使用,目前 STRUCT 仅支持在 Duplicate 模型的表中使用。

一个 Struct 中的 Field 的名字和数量固定,总是为 Nullable,一个 Field 通常由下面部分组成。

  • field_name: Field 的标识符,不可重复
  • field_type: Field 的类型
  • COMMENT: Field 的注释,可选 (暂不支持)

当前可支持的类型有:

  1. BOOLEAN, TINYINT, SMALLINT, INT, BIGINT, LARGEINT, FLOAT, DOUBLE, DECIMAL, DECIMALV3, DATE,
  2. DATEV2, DATETIME, DATETIMEV2, CHAR, VARCHAR, STRING

在将来的版本我们还将完善:

  1. TODO:支持嵌套 STRUCT 或其他的复杂类型

example

建表示例如下:

  1. mysql> CREATE TABLE `struct_test` (
  2. `id` int(11) NULL,
  3. `s_info` STRUCT<s_id:int(11), s_name:string, s_address:string> NULL
  4. ) ENGINE=OLAP
  5. DUPLICATE KEY(`id`)
  6. COMMENT 'OLAP'
  7. DISTRIBUTED BY HASH(`id`) BUCKETS 1
  8. PROPERTIES (
  9. "replication_allocation" = "tag.location.default: 1",
  10. "storage_format" = "V2",
  11. "light_schema_change" = "true",
  12. "disable_auto_compaction" = "false"
  13. );

插入数据示例:

Insert:

  1. INSERT INTO `struct_test` VALUES (1, {1, 'sn1', 'sa1'});
  2. INSERT INTO `struct_test` VALUES (2, struct(2, 'sn2', 'sa2'));
  3. INSERT INTO `struct_test` VALUES (3, named_struct('s_id', 3, 's_name', 'sn3', 's_address', 'sa3'));

Stream load:

test.csv:

  1. 1|{"s_id":1, "s_name":"sn1", "s_address":"sa1"}
  2. 2|{s_id:2, s_name:sn2, s_address:sa2}
  3. 3|{"s_address":"sa3", "s_name":"sn3", "s_id":3}

示例:

  1. curl --location-trusted -u root -T test.csv -H "label:test_label" http://host:port/api/test/struct_test/_stream_load

查询数据示例:

  1. mysql> select * from struct_test;
  2. +------+-------------------+
  3. | id | s_info |
  4. +------+-------------------+
  5. | 1 | {1, 'sn1', 'sa1'} |
  6. | 2 | {2, 'sn2', 'sa2'} |
  7. | 3 | {3, 'sn3', 'sa3'} |
  8. +------+-------------------+
  9. 3 rows in set (0.02 sec)

keywords

  1. STRUCT