HIVE中數據類型分爲數值類型、字符串類型、日期類型、複合類型以及其餘類型。下面分別進行介紹。數據結構
數值類型相似於JAVA中的基本數據類型spa
hive> select * from ts; OK 1 2018-08-21 11:12:13.1 2 2018-08-21 11:12:13.123 3 2018-08-21 11:12:13.123456 4 2018-08-21 11:12:13.123456789 5 2018-08-21 11:12:13 6 NULL
若是load數據中的精度超出,則爲null, 但使用cast由string向timestamp轉換隻是會截取到最大精度,而不會爲null。好比:code
6,2019-09-09 09:09:09.1234567891
1,2018-08-21 11:12:13.1,2018-08-21,2018-08-21 11:12:13.1 4,2018-08-21 11:12:13.123456789,2018-08-21,2018-08-21 11:12:13.123456789 5,2018-08-21 11:12:13,2018-08-21,2018-08-21 11:12:13 6,2019-09-09 09:09:09.1234567891,2018-08-21,2019-09-09 09:09:09.1234567891
hive> desc ts; OK id int ts timestamp date date dstring string Time taken: 0.062 seconds, Fetched: 4 row(s)
hive> select * from ts; OK 1 2018-08-21 11:12:13.1 2018-08-21 2018-08-21 11:12:13.1 4 2018-08-21 11:12:13.123456789 2018-08-21 2018-08-21 11:12:13.123456789 5 2018-08-21 11:12:13 2018-08-21 2018-08-21 11:12:13 6 NULL 2018-08-21 2019-09-09 09:09:09.1234567891 Time taken: 0.054 seconds, Fetched: 4 row(s)
select cast(ts as String) from ts; 2018-08-21 11:12:13.1 2018-08-21 11:12:13.123456789 2018-08-21 11:12:13 NULL
select cast(ts as date) from ts; 2018-08-21 2018-08-21 2018-08-21 NULL
select cast(date as string) from ts; 2018-08-21 2018-08-21 2018-08-21 2018-08-21
select cast(date as timestamp) from ts; 2018-08-21 00:00:00 2018-08-21 00:00:00 2018-08-21 00:00:00 2018-08-21 00:00:00
select cast(dstring as timestamp) from ts; 2018-08-21 11:12:13.1 2018-08-21 11:12:13.123456789 2018-08-21 11:12:13 2019-09-09 09:09:09.123456789
select cast(dstring as date) from ts; 2018-08-21 2018-08-21 2018-08-21 2019-09-09
Array:ARRAY<data_type>
Map:MAP<primitive_type, data_type>
Struct: STRUCT<col_name : data_type [COMMENT col_comment], ...>
Union:UNIONTYPE<data_type, data_type, ...>orm
create table uniontab(arr array<string>, mp MAP<INT, string>, stru struct<name:string, age:int>, uni uniontype<int, double, string>) row format delimited fields terminated by ',' collection items terminated by '#' map keys terminated by ':' lines terminated by '\n'; hive> desc uniontab; OK arr array<string> mp map<int,string> stru struct<name:string,age:int> uni uniontype<int,double,string> Time taken: 0.073 seconds, Fetched: 4 row(s)
插入union時使用create_union,其中第一個位置,指定使用的類型的index(從0開始),後面跟一個完整的union類型的數據結構ip
insert into uniontab select array('d','e','f'), map(1,'zjx',2,'wly'), named_struct('name', 'wly', 'age', 17), create_union(2, 10, 10.0, '10.1') from ts limit 1; select * from uniontab; ["a","b","c"] {1:"zjx",2:"wly"} {"name":"zjx","age":18} {0:10} ["d","e","f"] {1:"zjx",2:"wly"} {"name":"wly","age":17} {1:10.0} ["d","e","f"] {1:"zjx",2:"wly"} {"name":"wly","age":17} {2:"10.1"}
struct類型至關於JAVA中的BEAN,能夠使用 「.」 來操做其中的屬性ci
select stru.age from uniontab; 18 17 17 select mp[1] from uniontab; zjx zjx zjx select uni from uniontab; {0:10} {1:10.0} {2:"10.1"}