Hive复杂数据类型与一些聚合函数

ARRAY

数组类型,取值:列名[index],取长度size(列名)

MAP

键-值对元组集合,使用数组表示法可以访问数据。例如,如果某个列的数据类型是MAP,其中键->值对是’first’->’John’和’last’->’Doe’,那么可以通过字段名[‘last’]获取最后一个元素(注意:要加引号)

STRUCT

和java中的javaBean类似,都可以通过“点”符号访问元素内容。例如,如果某个列的数据类型是STRUCT{first STRING, last STRING},那么第1个元素可以通过字段.first来引用。

 

示例:

假设某表有如下一行,我们用JSON格式来表示其数据结构。在Hive下访问的格式为

{
    "name": "songsong",
    "friends": ["bingbing" , "lili"] ,       //列表Array, 
    "children": {                      //键值Map,
        "xiao song": 18 ,
        "xiaoxiao song": 14
    }
    "address": {                      //结构Struct,
        "street": "hui long guan" ,
        "city": "beijing" 
    }
}

转换为本地数据 test.txt

songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing

创建测试表导入数据:

create table test(
name string,
friends array<string>,
children map<string, int>,
address struct<street:string, city:string>
)
row format delimited fields terminated by ','
collection items terminated by '_'    --ARRAY ,MAP,STRUCT 的分隔符(数据分割符号)
map keys terminated by ':'        -- MAP中的key与value的分隔符
lines terminated by '\n';        -- 行分隔符
load data local inpath '/opt2/pojo.txt' into table test;

字段解释:

注意:MAP,STRUCT和ARRAY里的元素间关系都可以用同一个字符表示,这里用“_”。
row format delimited fields terminated by ','  -- 列分隔符
collection items terminated by '_'      --ARRAY ,MAP,STRUCT 的分隔符(数据分割符号)
map keys terminated by ':'                -- MAP中的key与value的分隔符
lines terminated by '\n';                    -- 行分隔符

测试:

select friends[1], children['xiao yang'],addredd.street from test;

 

聚合函数

CONCAT(string A/col, string B/col…):拼接;


CONCAT_WS(separator, str1, str2,...):用第一个字符拼接;


COLLECT_SET(col):接收一列,去重后拼接成一个数组返回一行


COLLECT_LIST(col): 不去重的COLLECT_SET

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值