MAP
The MAP
function is used to create a key-value mapping table, where each key corresponds to a value. This data structure allows for convenient storage and retrieval of data associated with specific keys.
Syntax
MAP <keyType, valueType> (key1, value1, key2, value2, ...)
Parameters
keyType
: The type of the key, which can be any basic data type such as integer, string, etc.
valueType
: The type of the value, which can be any basic data type such as integer, string, etc.
(key1, value1, key2, value2, ...)
: One or more key-value pairs, separated by commas.
Example
- Map color names to corresponding numbers:
CREATE TABLE MAP_TABLE(COL MAP<STRING,INT>);
SELECT MAP ('red', 1, 'green', 2);
+---------------------------+
| MAP('red', 1, 'green', 2) |
+---------------------------+
| {"red":1,"green":2} |
+---------------------------+
- Map employee names to their corresponding departments:
SELECT MAP ('Alice', 'HR', 'Bob', 'Sales', 'Charlie', 'IT');
+-----------------------------------------------------+
| MAP('Alice', 'HR', 'Bob', 'Sales', 'Charlie', 'IT') |
+-----------------------------------------------------+
| {"Alice":"HR","Bob":"Sales","Charlie":"IT"} |
+-----------------------------------------------------+
3. Map the exam scores to the corresponding grades:
SELECT MAP (60, 'Pass', 80, 'Good', 90, 'Excellent');
+-----------------------------------+
| MAP(60, 'Pass', 80, 'Good', 90, 'Excellent') |
+-----------------------------------+
| {60:"Pass",80:"Good",90:"Excellent"} |
+-----------------------------------+