Primitive Types

 

Numeric Types

 

 type length postfix example
TINYINT 1-byte signed integer, -128 - 127 Y 100Y
SMALLINT 2-byte signed integer, -32768 - 32767 S 100S
INT|INTEGE 4-byte signed integer, -2147483648 - 2147483647 L 100L
BIGINT 8-byte signed integer,-9223372036854775808 to 9223372036854775807    
FLOAT 4-byte single precision floating point number    
DOUBLE 8-byte double precision floating point number    
DECIMAL

Introduced in Hive 0.11.0 with a precision of 38 digits

   
NUMERIC same as DECIMAL, starting with Hive 3.0.0    
DOUBLE PRECISION alias for DOUBLE, only available starting with Hive 2.2.0    
       

 

Data/Time Types

 

TIMESTAMP Only available starting with Hive 0.8.0 YYYY-MM-DD HH:MM:SS.fffffffff" (9 decimal place precision)
DATE Only available starting with Hive 0.12.0 YYYY-­MM-­DD.
INTERVAL Only available starting with Hive 1.2.0  
timestamp with local time zone    

timestamp with loca time zone类型语法

tmestamp[(fractional_seconds_precisions)] with local zone

timestamp with local time zone 和timesatamp with time zone的最大区别就是,前者在用户提交时间给数据库的时,该类型会转换成数据库的时区来保存数据,即数据库保存的时间是数据库本地时区,当别的用户访问数据库时oracle会自动将该时间转换成当前客户端的时间。

 

DB 的date类型:Date(不含微妙级时间),Timestamp(不含时区,含微妙时间),Timestamp with timezone(保持client的timezone),Timestamp with local time zone(把client的timezone转换成DB的timezone对应的时间)

String Types

 

STRING    
VARCHAR between 1 and 65535  
CHAR The maximum length is fixed at 255 定长

 

对于Hive的String类型相当于数据库的varchar类型,该类型是一个可变的字符串,不过它不能声明其中最多能存储多少个字符,理论上它可以存储2GB的字符数。

 

Primitive Types Demo

  •  
DROP TABLE IF EXISTS hive_primitive_data_types;CREATE TABLE hive_primitive_data_types(    c_integers_tinyint  TINYINT,    c_integers_smallint SMALLINT,    c_integers_int      INT,    c_integers_integer  INTEGER,    c_integers_bigint   BIGINT,
c_boolean BOOLEAN,
c_float FLOAT, c_double DOUBLE,
c_decimal DECIMAL,
c_string STRING, c_varchar VARCHAR(65535), c_char CHAR(10) COMMENT '总长度255,固定长度', c_timestamp TIMESTAMP,--c_timestamp_2 timestamp with local time zone, c_date DATE,
c_binary BINARY);

 

  •  
insert into table hive_primitive_data_types   select 1,1,1,1,1,'true',1.1,1.11,1.111,'1','1','1',cast(current_date() as timestamp),cast(current_date() as date),cast('1' as binary);
insert into table hive_primitive_data_types select 2,2,2,2,2,'true',2.2,2.22,2.222,'2','2','2',cast(current_timestamp() as timestamp),current_date(),cast('2' as binary);

 

Hive data types_类型转换

 

Complex Types

 

类型解释

 

ARRAY

ARRAY<data_type>

编号从零开

MAP MAP<primitive_type, data_type>  
STRUCT STRUCT<col_name : data_type [COMMENT col_comment], ...>  
UNIONTYPE UNIONTYPE<data_type, data_type, ...>  

 

Hive有四种复杂数据类型ARRAY、MAP 和 STRUCT。ARRAY和MAP与Java中的Array和Map类似,而STRUCT与C语言中的Struct类似,它封装了一个命名字段集合,复杂数据类型允许任意层次的嵌套。

 

  • uniontype可以理解为泛型

  • 同一时刻同一地点只有联合体中的一个元素生效

  • uniontype中的元素共享内存

  • 可以通过create_union内置函数创建uniontype:create_union(tag, val1, val2) tag是数字,0开始,必须小于后面参数的个数

  • 插入uniontype数据,通过这种方式只能插入只有一个元素的uniontype,包含多个会提示跟表中的字段类型不一致,这个是坑的地方

 

创建表

 

  •  
DROP TABLE IF EXISTS hive_complex_data_types;CREATE TABLE hive_complex_data_types(    c_array  ARRAY<STRING>,    c_map    MAP<STRING,STRING>,    c_struct STRUCT<province:STRING,city:STRING,area:STRING>,    c_union  UNIONTYPE<STRING,STRING>)    ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'        COLLECTION ITEMS TERMINATED BY '_'        MAP KEYS TERMINATED BY ':'        LINES TERMINATED BY '\n';

 

ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t':字段分割

COLLECTION ITEMS TERMINATED BY '_':集合分割

MAP KEYS TERMINATED BY ':':map的key,value分割

LINES TERMINATED BY '\n':行分割

导入数据

  •  
load data local inpath '/liuduo/data.txt' into table hive_complex_data_types;

 

  •  
cat data.txt
广州_深圳 城市:广州 广东_广州_白云 广州深圳北京_上海       城市:上海       上海_上海_徐汇  北京上海


查询数据

 

  •  
select c_array[0],c_array[1],       c_map['城市'],       c_struct.province,c_struct.city,c_struct.area,       c_unionfrom hive_complex_data_types;

 

Hive data types_string类_02

 

类型转换

 

Hive的原子数据类型是可以进行隐式转换的,类似于Java的类型转换,例如某表达式使用INT类型,TINYINT会自动转换为INT类型,但是Hive不会进行反向转化,例如,某表达式使用TINYINT类型,INT不会自动转换为TINYINT类型,它会返回错误,除非使用CAST操作。

 

隐式类型转换规则如下1)任何整数类型都可以隐式地转换为一个范围更广的类型,如TINYINT可以转换成INTINT可以转换成BIGINT2)所有整数类型、FLOATSTRING类型都可以隐式地转换成DOUBLE3TINYINTSMALLINTINT都可以转换为FLOAT(4)BOOLEAN类型不可以转换为任何其它的类型。

 

Hive data types_hive_03