【个人笔记】hive的Complex Types 复杂类型

hive的data type很丰富,简单的就不说了,直接说常用的几个复杂类型的理解和使用吧。
根据官网所知道,hive的复杂类型有4种:

Complex Types:
1、arrays: ARRAY<data_type> (Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
2、maps: MAP<primitive_type, data_type> (Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
3、structs: STRUCT<col_name : data_type [COMMENT col_comment], …>
4、union: UNIONTYPE<data_type, data_type, …> (Note: Only available starting with Hive 0.7.0.)
我们日常常用的有前面三种,我们只需了解他们是定义、取值、构造即可。便能在hive中使用他们。

ARRAY
定义:array
取值:arr[0]
构造:array(val2,val2,val3,…),split(),COLLECT_SET()

map
定义:map<String,String>
取值:map[key]
构造L:map(key1, value1, key2, value2, …)

struct:
定义:struct<a:string,b:string>
取值:struct.id
构造:name_struct(name1,val1,name2,val2,name3,val3,…)

  • map和struct的使用有一点小小的区别:
  • map里面的类型是一样的,根据我们的定义即可知道
  • struct里面的类型可以不一样。

这里写一个简单的场景表的create语句:

CREATE EXTERNAL TABLE ods_log_inc
(
    `common`   STRUCT<ar :STRING,ba :STRING,ch :STRING,is_new :STRING,md :STRING,mid :STRING,os :STRING,uid :STRING,vc:STRING> ,
    `page`     STRUCT<during_time :STRING,item :STRING,item_type :STRING,last_page_id :STRING,page_id:STRING,source_type :STRING> ,
    `actions`  ARRAY<STRUCT<action_id:STRING,item:STRING,item_type:STRING,ts:BIGINT>> ,
    `displays` ARRAY<STRUCT<display_type :STRING,item :STRING,item_type :STRING,`order` :STRING,pos_id:STRING>> ,
    `start`    STRUCT<entry :STRING,loading_time :BIGINT,open_ad_id :BIGINT,open_ad_ms :BIGINT,open_ad_skip_ms:BIGINT> ,
    `err`      STRUCT<error_code:BIGINT,msg:STRING> ,
    `ts`       BIGINT  
) 
    PARTITIONED BY (`dt` STRING)
    ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.JsonSerDe'
    LOCATION '/warehouse/gmall/ods/ods_log_inc/';

猜你喜欢

转载自blog.csdn.net/m0_49303490/article/details/128276240
今日推荐