Hive系列文章
Hadoop完全分布式搭建(腾讯云服务器+阿里云服务器)
Hive系列 (一):Hive搭建
Hive系列 (二):Hive基础知识
Hive系列 (三):Hive开窗函数详解
Hive系列 (四):自定义函数UDF UDTF UDAF
Hive系列 (五):Hive数据类型
Hive系列 (六):Hive数据类型转换
Hive系列 (七):Hive常用函数
Hive系列 (八):Hive中的explode 与 lateral view
Hive系列 (九):Hive数据存储
Hive系列 (十):Hive调优
数据类型转换
同其他语言一样,Hive也包含隐式转换和显式转化。
- Hive会对numeric类型的数据进行隐式转换。
- 任何整数类型都可以隐式地转换成一个范围更大的类型。
TINYINT,SMALLINT,INT,BIGINT,FLOAT和STRING
都可以隐式 地转换成DOUBLE;BOOLEAN类型不能转换为其他任何数据类型。- 可以使用CAST操作显示进行数据类型转换,例如
CAST(‘1’ AS INT)
将把字符串’1’ 转换成整数1;如果强制类型转换失败,如执行CAST(‘X’ AS INT)
,表达式返回空值NULL
Cast显示转换
可以用CAST来显式的将一个类型的数据转换成另一个数据类型,其语法为cast(value AS TYPE)
0: jdbc:hive2://master:10000> select stu_id,chinese from myhive.stu_scores where stu_id = 10001; OK +---------+----------+ | stu_id | math | +---------+----------+ | 10001 | 70.0 | +---------+----------+ 1 row selected (4.558 seconds) 0: jdbc:hive2://master:10000> select stu_id,cast(math as float) as math from myhive.stu_scores where stu_id = 10001; OK +---------+-------+ | stu_id | math | +---------+-------+ | 10001 | 70.0 | +---------+-------+ 1 row selected (4.105 seconds) 0: jdbc:hive2://master:10000> # 这样math将会显示的转换成float。如果math是不能转换成float,这时候cast将会返回NULL
注意事项:
- 如果将浮点型的数据转换成int类型的,内部操作是通过round()或者floor()函数来实现的,而不是通过cast实现。
- 对于
binary
类型的数据,只能将binary
类型的数据转换成string
类型。 - 只有
binary
类型数据是一个数字类型(a number),这时候你可以利用嵌套的cast操作select(cast(cast(a as string) as double)) from tb1
数据类型转换表
boolean | tinyint | smallint | int | bigint | float | double | decimal | string | varchar | timestamp | date | binary | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
boolean | true | false | false | false | false | false | false | false | false | false | false | false | false |
tinyint | false | true | true | true | true | true | true | true | true | true | false | false | false |
smallint | false | false | true | true | true | true | true | true | true | true | false | false | false |
int | false | false | false | true | true | true | true | true | true | true | false | false | false |
bigint | false | false | false | false | true | true | true | true | true | true | false | false | false |
float | false | false | false | false | false | true | true | true | true | true | false | false | false |
double | false | false | false | false | false | false | true | true | true | true | false | false | false |
decimal | false | false | false | false | false | false | false | true | true | true | false | false | false |
string | false | false | false | false | false | false | true | true | true | true | false | false | false |
varchar | false | false | false | false | false | false | true | true | true | true | false | false | false |
timestamp | false | false | false | false | false | false | false | false | true | true | true | false | false |
date | false | false | false | false | false | false | false | false | true | true | false | true | false |
binary | false | false | false | false | false | false | false | false | false | false | false | false | true |
日期类型转换说明
有效的转换 | 结果 |
---|---|
cast(date as date) | 返回date类型 |
cast(timestamp as date) | timestamp中的年/月/日的值是依赖与当地的时区,结果返回date类型 |
cast(string as date) | 如果string是YYYY-MM-DD格式的,则相应的年/月/日的date类型的数据将会返回;但如果string不是YYYY-MM-DD格式的,结果则会返回NULL。 |
cast(date as timestamp) | 基于当地的时区,生成一个对应date的年/月/日的时间戳值 |
cast(date as string) | date所代表的年/月/日时间将会转换成YYYY-MM-DD的字符串 |
转换示例
日期与字符串转换
# cast(string as date) # 不是YYYY-MM-DD格式返回null 0: jdbc:hive2://master:10000> select cast('' as date); OK +-------+ | _c0 | +-------+ | NULL | +-------+ 1 row selected (2.135 seconds) 0: jdbc:hive2://master:10000> select cast('2021-08-25' as date); OK +-------------+ | _c0 | +-------------+ | 2021-08-25 | +-------------+ 1 row selected (2.167 seconds) # 日期转字符串 0: jdbc:hive2://master:10000> select cast(current_date() as string); OK +-------------+ | _c0 | +-------------+ | 2021-08-25 | +-------------+ 1 row selected (2.291 seconds) 0: jdbc:hive2://master:10000>
时间戳与日期转换
# cast(timestamp as date) # 显示当前时间戳current_timestamp() 0: jdbc:hive2://master:10000> select current_timestamp(); OK +--------------------------+ | _c0 | +--------------------------+ | 2021-08-25 16:35:27.249 | +--------------------------+ 1 row selected (2.287 seconds) # 时间戳转日期 0: jdbc:hive2://master:10000> select cast(current_timestamp() as date); OK +-------------+ | _c0 | +-------------+ | 2021-08-25 | +-------------+ 1 row selected (2.399 seconds) # 日期转时间戳 0: jdbc:hive2://master:10000> select cast(current_date() as timestamp); OK +------------------------+ | _c0 | +------------------------+ | 2021-08-25 00:00:00.0 | +------------------------+ 1 row selected (2.325 seconds) 0: jdbc:hive2://master:10000>
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233134.html原文链接:https://javaforall.net