关于 hive 特殊字段的应用举例

建表

create table temp.jc_t_test_map_array
(
    id     int,
    name   string,
    hobby  array<string>,             --   array中元素为string类型
    friend map<string,string>,          -- map中键和值均为string类型
    mark   struct<math:int,english:int> -- struct中元素为int类型
)
    row format delimited fields terminated by ',' -- 字段之间用','分隔
        collection items terminated by '_' -- 集合中的元素用'_'分隔
        map keys terminated by ':' -- map中键值对之间用':'分隔
        lines terminated by '\n'  ;-- 行之间用'\n'分隔

查看表结构.

desc temp.jc_t_test_map_array;
id    int
name    string
hobby    array<string>
friend    map<string,string>
mark    struct<math:int,english:int>

插入数据

insert into temp.jc_t_test_map_array
select 2,
       'xiaohua',
       array('basketball', 'read'),
       str_to_map('xiaoming:no,xiaohong:no'),
       named_struct('math', 90, 'english', 90);

数据展示

select id,
       name,
       hobby[0],           --查询第一个hobby
       friend['xiaohong'], --查询map键为xiaohong的value
       mark.math          --查询struct中math的值
from temp.jc_t_test_map_array;

 

 

 

 

posted @ 2022-01-29 18:22  晓枫的春天  阅读(74)  评论(0编辑  收藏  举报