分享

Hive常用函数

 爱吃鱼的俊懒猫 2022-05-18 发布于广东
  • map 结构

    map 构造函数
    语法:map(k1,v1,k2,v2,…)
    操作类型:map
    说明:使用给定的 key-value 对,构造一个 map 数据结构

    hive> select map('k1','v1','k2','v2') from iteblog;
    OK
    {"k2":"v2","k1":"v1"}
    
    hive> Create table iteblog as select map('100','tom','200','mary') as t from iteblog;
    hive> describe iteblog;
    t       map<string ,string>
    hive> select t from iteblog;
    {"100":"tom","200":"mary"}
    

    获取 map 中的元素M[key]
    语法:M[key]
    操作类型:所有基础类型。M为map类型,key为map中的key值
    说明:返回 map 结构 M 中 key 对应的 value,没有对应的 key 返回 NULL。
    比如:M是值为{'f’ -> 'foo’, 'b’ -> 'bar’, 'all’ -> 'foobar’}的map类型,那么M['all’]将会返回’foobar’

    hive> select map('k1','v1','k2','v2')['k1'] from iteblog;
    OK
    v1
    hive> select map('k1','v1','k2','v2')['k3'] from iteblog;
    OK
    NULL
    
    hive> Create table iteblog as select map('100','tom','200','mary') as t from iteblog;
    hive> select t['200'],t['100'] from iteblog;
    mary    tom
    

    Map类型长度函数: size(Map<k .V>)
    语法: size(Map<k .V>)
    返回值: int
    说明: 返回map类型的长度

    hive> select size(map('100','tom','101','mary')) from iteblog;
    2
    
  • struct 结构

    struct 构造函数
    语法:struct(val1,val2,val3,…)
    操作类型:struct
    说明:使用给定的表达式,构造一个 struct 数据结构

    hive> select struct(1,'aaa',FALSE) from iteblog;
    OK
    {"col1":1,"col2":"aaa","col3":false}
    
    hive> create table iteblog as select struct('tom','mary','tim') as t from iteblog;
    hive> describe iteblog;
    t       struct<col1:string ,col2:string,col3:string>
    hive> select t from iteblog;
    {"col1":"tom","col2":"mary","col3":"tim"}
    

    获取 struct 中的元素 S.x
    语法:S.x
    操作类型:所有类型。S为struct类型
    说明:返回 struct 结构 S 中名为 x 的元素。
    比如:对于结构体struct foobar {int foo, int bar},foobar.foo返回结构体中的foo字段

    hive> select named_struct('a',1,'b','aaa','c',FALSE).c from iteblog;
    OK
    false
    
    hive> create table iteblog as select struct('tom','mary','tim') as t from iteblog;
    hive> describe iteblog;
    t       struct<col1:string ,col2:string,col3:string>
    hive> select t.col1,t.col3 from iteblog;
    tom     tim
    
  • array 结构

    array 构造函数
    语法:array(val1,val2,val3,…)
    操作类型:array
    说明:使用给定的表达式,构造一个 array 数据结构

    hive> select array(1,2,3) from iteblog;
    OK
    [1,2,3]
    
    hive> create table iteblog as select array("tom","mary","tim") as t from iteblog;
    hive> describe iteblog;
    t       array<string>
    hive> select t from iteblog;
    ["tom","mary","tim"]
    

    获取 array 中的元素 A[n]
    语法:A[n]
    操作类型:所有基础类型
    说明:返回数组 A 中第 n 个索引的元素值,数组的起始下标为0。
    比如:A是值为['foo’, 'bar’]的数组类型,那么A[0]将返回’foo’,而A[1]将返回’bar’

    hive> select array('a','b','c')[1] from iteblog;
    OK
    b
    
    hive> create table iteblog as select array("tom","mary","tim") as t from iteblog;
    hive> select t[0],t[1],t[2] from iteblog;
    tom     mary    tim
    

    array类型长度函数: size(Array)
    语法: size(Array)
    返回值: int
    说明: 返回array类型的长度

    hive> select size(array('100','101','102','103')) from iteblog;
    4
    
  • named_struct 结构
    语法:named_struct(name1,val1,name2,val2,name3,val3,…)
    操作类型:struct
    说明:使用给定的表达式,构造一个指定列名的 struct 数据结构

    hive> select named_struct('a',1,'b','aaa','c',FALSE) from iteblog;
    OK
    {"a":1,"b":"aaa","c":false}
    
  • create_union
    语法:create_union (tag, val1, val2, …)
    操作类型:uniontype
    说明:使用给定的 tag 和表达式,构造一个 uniontype 数据结构。tag 表示使用第 tag 个
    表达式作为 uniontype 的 value

    hive> select create_union(0,'ss',array(1,2,3)) from iteblog;
    OK
    {0:"ss"}
    hive> select create_union(1,'ss',array(1,2,3)) from iteblog;
    OK
    {1:[1,2,3]}
    hive> select create_union(2,'ss',array(1,2,3),'aa') from iteblog;
    OK
    {2:'aa'}
    • 本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约

      类似文章 更多