Hive专题四---Hive数据类型

版权声明:本文为博主原创文章,转载请标明出处:

欢迎访问:https://blog.csdn.net/qq_21439395

交流QQ: 824203453

欢迎访问博主个人主页:http://www.oldsheep.cn 

 

Hive数据类型

基本数据类型

Hive数据类型

Java数据类型

长度

例子

TINYINT

byte

1byte有符号整数

20

SMALINT

short

2byte有符号整数

20

INT

int

4byte有符号整数

20

BIGINT

long

8byte有符号整数

20

FLOAT

float

单精度浮点数

3.14159

DOUBLE

double

双精度浮点数

3.14159

 

 

 

 

STRING

string

字符系列。可以指定字符集。可以使用单引号或者双引号。

‘now is the time’ “for all good men”

VARCHAR

 

字符串1-65535长度,超长截断

 

CHAR 

 

字符串,最大长度255

 

 

 

 

 

BOOLEAN

boolean

布尔类型,true或者false

TRUE  FALSE

BINARY

 

字节数组

 

TIMESTAMP

 

时间类型 包含年月日时分秒毫秒

 

DATE

 

日期,只包含年月日

 

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

 

示例:

create table t_test(a string ,b int,c bigint,d float,e double,f tinyint,g smallint)

日期类型示例:


1,zhangsan,1985-06-31

2,lisi,1986-07-10

3,wangwu,1985-08-09


那么,就可以建一个表来对数据进行映射

create table t_customer(id int,name string,birthday date)

row format delimited fields terminated by ',';

然后导入数据

load data local inpath '/root/customer.dat' into table t_customer;

然后,就可以正确查询

boolean类型实例:

1,zs,28,true

2,ls,30,false

3,ww,32,false

4,lulu,18,true

create table t_p(id int,name string,age int,is_married boolean)
select * from t_p where is_married;

 

集合数据类型

数据类型

描述

语法示例

STRUCT

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

struct()

MAP

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

map()

ARRAY

数组是一组具有相同类型和名称的变量的集合。这些变量称为数组的元素,每个数组元素都有一个编号,编号从零开始。例如,数组值为[‘John’, ‘Doe’],那么第2个元素可以通过数组名[1]进行引用。

Array()

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

 

array数组类型

arrays: ARRAY<data_type> )

示例:array类型的应用

假如有如下数据需要用hive的表去映射:

战狼2,吴京:吴刚:余男,2017-08-16 

三生三世十里桃花,刘亦菲:痒痒,2017-08-20

羞羞的铁拳,沈腾:玛丽:艾伦,2017-12-20

设想:如果主演信息用一个数组来映射比较方便

建表:
create table t_movie(moive_name string,actors array<string>,first_show date)
row format delimited fields terminated by ','
collection items terminated by ':';

导入数据:
load data local inpath '/root/hivedata/movie.dat' into table t_movie;

查询:
select * from t_movie;
select moive_name,actors[0] from t_movie;
select moive_name,actors from t_movie where array_contains(actors,'吴刚');
select moive_name,size(actors) from t_movie;

 

map类型

maps: MAP<primitive_type, data_type> 

 

  1. 假如有以下数据:

1,zhangsan,father:xiaoming#mother:xiaohuang#brother:xiaoxu,28

2,lisi,father:mayun#mother:huangyi#brother:guanyu,22

3,wangwu,father:wangjianlin#mother:ruhua#sister:jingtian,29

4,mayun,father:mayongzhen#mother:angelababy,26

可以用一个map类型来对上述数据中的家庭成员进行描述


建表语句:
create table t_person(id int,name string,fm map<string,string>,age int)
row format delimited fields terminated by ','
collection items terminated by '#'
map keys terminated by ':';

加载数据:
load data local inpath '/root/hivedata/map.dat' into table t_person;

查询
select * from t_person;

## 取map字段的指定key的值
select id,name,fm['father'] as father from t_person;

## 取map字段的所有key
select id,name,map_keys(fm) as relation from t_person;

## 取map字段的所有value
select id,name,map_values(fm) from t_person;
select id,name,map_values(fm)[0] from t_person;

struct类型

struct: STRUCT<col_name : data_type, ...>

  1. 假如有如下数据:

1,zhangsan,18:male:beijing

2,lisi,28:female:shanghai

 

其中的用户信息包含:年龄:整数,性别:字符串,地址:字符串

设想用一个字段来描述整个用户信息,可以采用struct

建表:
create table t_ps(id int,name string,info struct<age:int,sex:string,addr:string>)
row format delimited fields terminated by ','
collection items terminated by ':';

加载数据:
load data local inpath '/root/hivedata/struct.dat' into table t_ps;

查询
select * from t_ps;
select id,name,info.age from t_ps;

 

案例实操

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

{

    "name": "songsong",

    "friends": ["bingbing" , "lili"] ,       //列表Array,

    "children": {                      //键值Map,

        "xiao song": 18 ,

        "xiaoxiao song": 19

    }

    "address": {                      //结构Struct,

        "street": "hui long guan" ,

        "city": "beijing"

    }

}

2)基于上述数据结构,我们在Hive里创建对应的表,并导入数据。

创建本地测试文件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

注意,MAP,STRUCT和ARRAY里的元素间关系都可以用同一个字符表示,这里用“_”。

3)Hive上创建测试表test

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 '_'

map keys terminated by ':'

lines terminated by '\n';

字段解释:

row format delimited fields terminated by ','  -- 列分隔符

collection items terminated by '_'        --MAP STRUCT 和 ARRAY 的分隔符(数据分割符号)

map keys terminated by ':'                          -- MAP中的key与value的分隔符

lines terminated by '\n';                              -- 行分隔符

4)导入文本数据到测试表

hive (default)> load data local inpath '/opt/module/datas/test.txt' into table test;

5)访问三种集合列里的数据,以下分别是ARRAY,MAP,STRUCT的访问方式

hive (default)> select friends[1],children['xiao song'],address.city from test where name="songsong";

OK

_c0     _c1     city

lili    18      beijing

Time taken: 0.076 seconds, Fetched: 1 row(s)

类型转化

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

1)隐式类型转换规则如下。

(1)任何整数类型都可以隐式地转换为一个范围更广的类型,如TINYINT可以转换成INT,INT可以转换成BIGINT。

(2)所有整数类型、FLOAT和STRING类型都可以隐式地转换成DOUBLE。

(3)TINYINT、SMALLINT、INT都可以转换为FLOAT。

(4)BOOLEAN类型不可以转换为任何其它的类型。

2)可以使用CAST操作显示进行数据类型转换,例如CAST('1' AS INT)将把字符串'1' 转换成整数1;如果强制类型转换失败,如执行CAST('X' AS INT),表达式返回空值 NULL。

 

版权声明:本文为博主原创文章,转载请标明出处

交流QQ: 824203453

欢迎访问博主个人主页:http://www.oldsheep.cn 

猜你喜欢

转载自blog.csdn.net/qq_21439395/article/details/88918170