MAP

name

SinceVersion 2.0.0

MAP

description

MAP<K, V>

由K, V类型元素组成的map,不能作为key列使用。目前支持在Duplicate,Unique 模型的表中使用。

K,V 支持的类型有:

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

example

建表示例如下:

  1. CREATE TABLE IF NOT EXISTS test.simple_map (
  2. `id` INT(11) NULL COMMENT "",
  3. `m` Map<STRING, INT> NULL COMMENT ""
  4. ) ENGINE=OLAP
  5. DUPLICATE KEY(`id`)
  6. DISTRIBUTED BY HASH(`id`) BUCKETS 1
  7. PROPERTIES (
  8. "replication_allocation" = "tag.location.default: 1",
  9. "storage_format" = "V2"
  10. );

插入数据示例:

  1. mysql> INSERT INTO simple_map VALUES(1, {'a': 100, 'b': 200});

stream_load示例: 更多详细 stream_load 用法见 STREAM TABLE

  1. # load the map data from json file
  2. curl --location-trusted -uroot: -T events.json -H "format: json" -H "read_json_by_line: true" http://fe_host:8030/api/test/simple_map/_stream_load
  3. # 返回结果
  4. {
  5. "TxnId": 106134,
  6. "Label": "5666e573-9a97-4dfc-ae61-2d6b61fdffd2",
  7. "Comment": "",
  8. "TwoPhaseCommit": "false",
  9. "Status": "Success",
  10. "Message": "OK",
  11. "NumberTotalRows": 10293125,
  12. "NumberLoadedRows": 10293125,
  13. "NumberFilteredRows": 0,
  14. "NumberUnselectedRows": 0,
  15. "LoadBytes": 2297411459,
  16. "LoadTimeMs": 66870,
  17. "BeginTxnTimeMs": 1,
  18. "StreamLoadPutTimeMs": 80,
  19. "ReadDataTimeMs": 6415,
  20. "WriteDataTimeMs": 10550,
  21. "CommitAndPublishTimeMs": 38
  22. }

查询数据示例:

  1. mysql> SELECT * FROM simple_map;
  2. +------+-----------------------------+
  3. | id | m |
  4. +------+-----------------------------+
  5. | 1 | {'a':100, 'b':200} |
  6. | 2 | {'b':100, 'c':200, 'd':300} |
  7. | 3 | {'a':10, 'd':200} |
  8. +------+-----------------------------+

查询 map 列示例:

  1. mysql> SELECT m FROM simple_map;
  2. +-----------------------------+
  3. | m |
  4. +-----------------------------+
  5. | {'a':100, 'b':200} |
  6. | {'b':100, 'c':200, 'd':300} |
  7. | {'a':10, 'd':200} |
  8. +-----------------------------+

map 取值示例:

  1. mysql> SELECT m['a'] FROM simple_map;
  2. +-----------------------------+
  3. | %element_extract%(`m`, 'a') |
  4. +-----------------------------+
  5. | 100 |
  6. | NULL |
  7. | 10 |
  8. +-----------------------------+

map 支持的functions示例:

  1. # map construct
  2. mysql> SELECT map('k11', 1000, 'k22', 2000)['k11'];
  3. +---------------------------------------------------------+
  4. | %element_extract%(map('k11', 1000, 'k22', 2000), 'k11') |
  5. +---------------------------------------------------------+
  6. | 1000 |
  7. +---------------------------------------------------------+
  8. mysql> SELECT map('k11', 1000, 'k22', 2000)['nokey'];
  9. +-----------------------------------------------------------+
  10. | %element_extract%(map('k11', 1000, 'k22', 2000), 'nokey') |
  11. +-----------------------------------------------------------+
  12. | NULL |
  13. +-----------------------------------------------------------+
  14. 1 row in set (0.06 sec)
  15. # map size
  16. mysql> SELECT map_size(map('k11', 1000, 'k22', 2000));
  17. +-----------------------------------------+
  18. | map_size(map('k11', 1000, 'k22', 2000)) |
  19. +-----------------------------------------+
  20. | 2 |
  21. +-----------------------------------------+
  22. mysql> SELECT id, m, map_size(m) FROM simple_map ORDER BY id;
  23. +------+-----------------------------+---------------+
  24. | id | m | map_size(`m`) |
  25. +------+-----------------------------+---------------+
  26. | 1 | {"a":100, "b":200} | 2 |
  27. | 2 | {"b":100, "c":200, "d":300} | 3 |
  28. | 2 | {"a":10, "d":200} | 2 |
  29. +------+-----------------------------+---------------+
  30. 3 rows in set (0.04 sec)
  31. # map_contains_key
  32. mysql> SELECT map_contains_key(map('k11', 1000, 'k22', 2000), 'k11');
  33. +--------------------------------------------------------+
  34. | map_contains_key(map('k11', 1000, 'k22', 2000), 'k11') |
  35. +--------------------------------------------------------+
  36. | 1 |
  37. +--------------------------------------------------------+
  38. 1 row in set (0.08 sec)
  39. mysql> SELECT id, m, map_contains_key(m, 'k1') FROM simple_map ORDER BY id;
  40. +------+-----------------------------+-----------------------------+
  41. | id | m | map_contains_key(`m`, 'k1') |
  42. +------+-----------------------------+-----------------------------+
  43. | 1 | {"a":100, "b":200} | 0 |
  44. | 2 | {"b":100, "c":200, "d":300} | 0 |
  45. | 2 | {"a":10, "d":200} | 0 |
  46. +------+-----------------------------+-----------------------------+
  47. 3 rows in set (0.10 sec)
  48. mysql> SELECT id, m, map_contains_key(m, 'a') FROM simple_map ORDER BY id;
  49. +------+-----------------------------+----------------------------+
  50. | id | m | map_contains_key(`m`, 'a') |
  51. +------+-----------------------------+----------------------------+
  52. | 1 | {"a":100, "b":200} | 1 |
  53. | 2 | {"b":100, "c":200, "d":300} | 0 |
  54. | 2 | {"a":10, "d":200} | 1 |
  55. +------+-----------------------------+----------------------------+
  56. 3 rows in set (0.17 sec)
  57. # map_contains_value
  58. mysql> SELECT map_contains_value(map('k11', 1000, 'k22', 2000), NULL);
  59. +---------------------------------------------------------+
  60. | map_contains_value(map('k11', 1000, 'k22', 2000), NULL) |
  61. +---------------------------------------------------------+
  62. | 0 |
  63. +---------------------------------------------------------+
  64. 1 row in set (0.04 sec)
  65. mysql> SELECT id, m, map_contains_value(m, '100') FROM simple_map ORDER BY id;
  66. +------+-----------------------------+------------------------------+
  67. | id | m | map_contains_value(`m`, 100) |
  68. +------+-----------------------------+------------------------------+
  69. | 1 | {"a":100, "b":200} | 1 |
  70. | 2 | {"b":100, "c":200, "d":300} | 1 |
  71. | 2 | {"a":10, "d":200} | 0 |
  72. +------+-----------------------------+------------------------------+
  73. 3 rows in set (0.11 sec)
  74. # map_keys
  75. mysql> SELECT map_keys(map('k11', 1000, 'k22', 2000));
  76. +-----------------------------------------+
  77. | map_keys(map('k11', 1000, 'k22', 2000)) |
  78. +-----------------------------------------+
  79. | ["k11", "k22"] |
  80. +-----------------------------------------+
  81. 1 row in set (0.04 sec)
  82. mysql> SELECT id, map_keys(m) FROM simple_map ORDER BY id;
  83. +------+-----------------+
  84. | id | map_keys(`m`) |
  85. +------+-----------------+
  86. | 1 | ["a", "b"] |
  87. | 2 | ["b", "c", "d"] |
  88. | 2 | ["a", "d"] |
  89. +------+-----------------+
  90. 3 rows in set (0.19 sec)
  91. # map_values
  92. mysql> SELECT map_values(map('k11', 1000, 'k22', 2000));
  93. +-------------------------------------------+
  94. | map_values(map('k11', 1000, 'k22', 2000)) |
  95. +-------------------------------------------+
  96. | [1000, 2000] |
  97. +-------------------------------------------+
  98. 1 row in set (0.03 sec)
  99. mysql> SELECT id, map_values(m) FROM simple_map ORDER BY id;
  100. +------+-----------------+
  101. | id | map_values(`m`) |
  102. +------+-----------------+
  103. | 1 | [100, 200] |
  104. | 2 | [100, 200, 300] |
  105. | 2 | [10, 200] |
  106. +------+-----------------+
  107. 3 rows in set (0.18 sec)

keywords

  1. MAP