Hive数据库系列--Hive数据类型/Hive字段类型/Hive类型转换

本章主要讲解hive的数据类、字段类型。官网文档地址见https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types

一、Hive数据类型

数据类型主要是指create table的时候表中字段的类型,如int、string、decimal等等。

create table test_user
(
    id   int  comment '主键',
    name string comment '姓名',
    score   struct<math:int,computer:int>
)
comment '测试用户表'
row format delimited fields terminated by ','
collection items terminated by '_'
lines terminated by '\n';

1.1、数值类型

Hive 数据类型 Java 数据类型 长度 范围 例子
TINYINT byte 1byte 有符号整数 -128 to 127 10
SMALINT short 2byte 有符号整数 -32,768 to 32,767 10
INT int 4byte 有符号整数 -2,147,483,648 to 2,147,483,647 10
BIGINT long 8byte 有符号整数 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 20
FLOAT float 单精度浮点数 3.1415
DOUBLE double 双精度浮点数 3.1419
DECIMAL BigDecimal 17字节 38位,存储小数 10.20

1.2、字符类型

Hive的支付类型也与关系型数据库的MySQL类似。 实际使用中,String使用得最多

数据类型 描述
String 使用时通常用单引号(‘’)或者双引号(””)引用,Hive在String中使用c样式的转义
varchar 变长字符串,最大长度为65535
char 定长字符串, 最大长度255

Hive 的 STRING 类型相当于mysql数据库的 varchar 类型,该类型是一个可变的字符串,不过它不限定最多能存储多少个字符,理论上它可以存储 2GB 的字符数。

1.3、日期时间类型

Timestamp精度高,Timestamp精度为9,可以满足对时间字段的要求。 如要使用日期时间计算,可以使用interval。

Hive 数据类型 Java 数据类型 描述
TIMESTAMP 持传统的UNIX时间戳,可选纳秒精度,精度为9
Date 以YYYY-­MM-­DD格式存储年月日
interval INTERVAL ‘1’ DAY 增加1天
INTERVAL ‘1-2’ YEAR TO MONTH 增加1年2个月

1.4、其他类型

布尔类型表示true或false。

数据类型 描述
Boolean true/false
BINARY 字节数组

1.5、集合数据类型

Hive中的列支持struct、map和array集合数据类型。

数据类型 描述 语法示例
STRUCT 和 c 语 言 中 的 struct 类 似 , 都 可 以 通过“点”符号访问元素内容。例如,如果某个列的 数 据 类 型 是 STRUCT{first STRING, lastSTRING},那么第1个元素可以通过字段.first来引用。 struct(‘tom’,15) struct<name:string,age:int>
MAP MAP是一组键-值对元组集合,可以通过key来访问元素。例如,如果某个列的数据类型是MAP,其中键->值对是’first’->‘John’和’last’->‘Doe’,那么可以通过字段名[‘last’]获取最后一个元素 map<string, int>
ARRAY ARRAY是由一系列具有相同数据类型的元素组成的集合,这些些素可以通过下标来访问。例如有一个ARRAY类型的变量fruits,它是由[‘apple’,‘orange’,‘mango’]组成,那么我们可以 通 过 fruits[1] 来 访 问 元 素 orange , 因 为ARRAY类型的下标是从0开始的。 Array(‘John’, ‘Doe’)

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

1.5.1、Struct举例

(1)假设有如下两条数据,为了便于理解,以JSON格式来表示它的数据结构:

[
{
    
    
	"stuid": 1,
	"stuname":'alan',
	"score":{
    
    
		"math":98,
		"computer":89
	}
},
{
    
    
	"stuid": 2,
	"stuname":'john',
	"score":{
    
    
		"math":95,
		"computer":97
	}
}
]

(2)在目录/root/data中创建本地测试文件struct.txt,保存下面的数据。

1,alan,98_89
2,john,95_97

在这里插入图片描述
(3)在Hive上创建测试表test_struct

create table test_struct
(
    stuid   int,
    stuname string,
    score   struct<math:int,computer:int>
)
    row format delimited fields terminated by ','
        collection items terminated by '_'
        lines terminated by '\n';

在这里插入图片描述
字段解释:

row format delimited fields terminated by ',' -- 列分隔符
collection items terminated by '_' -- MAP STRUCT和ARRAY的分隔符(数据分割符号)
lines terminated by '\n'; -- 行分隔符

(4)接下来,导入struct.txt中的文本数据到测试表test_struct

load data local inpath '/root/data/struct.txt' into table test_struct;

(5)访问表test_struct中的数据

select * from test_struct;

在这里插入图片描述
(6)访问结构中的数据

select stuname,score.math,score.computer from test_struct;

在这里插入图片描述

1.5.2、Array举例

(1)假设有如下两条数据,为了便于理解,以JSON格式来表示它的数据结构:

[
{
    
    
	"stuid": 1,
	"stuname":'alan',
	"hobbys":["music","sports"]
},
{
    
    
	"stuid": 2,
	"stuname":'john',
	"hobbys":["music","travel"]
}
]

(2)在目录/root/data中创建本地测试文件array.txt,保存下面的数据。

1,alan,music_sports
2,john,music_travel

在这里插入图片描述
(3)在Hive上创建测试表test_array

create table test_array
(
    stuid   int,
    stuname string,
    hobbys  array<string>
)
    row format delimited fields terminated by ','
        collection items terminated by '_'
        lines terminated by '\n';

(4)接下来,导入array.txt中的文本数据到测试表test_array

load data local inpath '/root/data/array.txt' into table test_array;

(5)访问表test_array中的数据

select * from test_array;

在这里插入图片描述
(6)访问数组中的数据

set hive.cli.print.header=true;
select stuname,hobbys[0] from test_array;

在这里插入图片描述

1.5.3、Map举例

(1)假设有如下两条数据,为了便于理解,以JSON格式来表示它的数据结构:

[
{
    
    
	"stuid": 1,
	"stuname":'alan',
	"score":{
    
    
		"math":98,
		"computer":89
	}
},
{
    
    
	"stuid": 2,
	"stuname":'john',
	"score":{
    
    
		"math":95,
		"computer":97
	}
}
]

(2)在目录/root/data中创建本地测试文件 map.txt,保存下面的数据。

1,alan,math:98_computer:89
2,john,math:95_computer:97

在这里插入图片描述

3)在Hive上创建测试表test_map

create table test_map
(
    stuid   int,
    stuname string,
    score   map<string,int>
)
    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)接下来,导入map.txt中的文本数据到测试表test_map

load data local inpath '/root/data/map.txt' into table test_map;

(5)访问表test_map中的数据

set hive.cli.print.header=true;
select * from test_map;

在这里插入图片描述
(6)访问map中的数据

select stuname,score['math'] as math,score['computer'] as computer from test_map;

在这里插入图片描述

二、数据类型转换

Hive 的原子数据类型是可以进行隐式转换的,类似于 Java 的类型转换。转换的原则是从数据范围小的类型向数据范围大的类型转换,或从数据精度低的类型向数据精度高的类型转换,以保证数据和精度不丢失。例如某表达式使用 BIGINT类型,INT 会自动转换为BIGINT 类型,但是 Hive 不会进行反向转换。例如,某表达式使用 INT 类型,BIGINT 不会自动转换为 INT 类型,它会返回错误,除非使用 CAST 操作。

2.1、隐式转换

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

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

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

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

2.2、显示转换

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

select '2'+3,cast('2' as int)+1;

在这里插入图片描述

三、字段类型的使用

3.1、DECIMAL(precision,scale)

Hive中的DECIMAL类型基于Java的BigDecimal,它用于在Java中表示不可变的任意精度十进制数。所有常规的数字操作(例如+、-、*、/)和相关的UDF(例如Floor、Ceil、Round等)都处理十进制类型。你可以像处理其他数值类型一样,将十进制类型转换为十进制类型或从十进制类型转换。decimal类型的持久性格式支持科学和非科学记数法。因此,无论数据集是否包含4.004E+3(科学计数法)或4004(非科学计数法)或两者的组合,DECIMAL都可以用于它。

从Hive 0.13开始,用户可以在使用DECIMAL(precision,scale)语法创建DECIMAL数据类型的表时指定scale和precision。 如果未指定小数位数,则默认为0(无小数位数)。如果未指定精度,则默认为10。

CREATE TABLE foo (
  a DECIMAL, -- Defaults to decimal(10,0)
  b DECIMAL(9, 7)
)

DECIMAL(precision,scale)说明:
precision-精度: 整数+scale的长度(即整数部分的长度最大不能超过precision-scale位)
scale-小数位: 小数部分的长度(若小数点后的长度小于scale则会自动补齐到scale位;若小数点后面的长度大于scale位则会截取scale位截取时会四舍五入)









参考文章:https://blog.csdn.net/W_chuanqi/article/details/131101265

猜你喜欢

转载自blog.csdn.net/weixin_49114503/article/details/134737578