Hive 数据类型转换:字符串、时间、数字互相转换详解
在 Hive 环境下,可以使用 Hive 的内置函数来实现字符串和时间、数字之间的转换。
- 字符串转时间: 使用 Hive 的内置函数'to_unix_timestamp'将字符串转换为 Unix 时间戳,然后使用'from_unixtime'将 Unix 时间戳转换为时间类型。
示例代码:
SELECT from_unixtime(to_unix_timestamp('2023-08-22 23:23:59', 'yyyy-MM-dd HH:mm:ss')) AS time_value;
- 时间转字符串: 使用 Hive 的内置函数'from_unixtime'将时间类型转换为 Unix 时间戳,然后使用'to_unix_timestamp'将 Unix 时间戳转换为指定格式的字符串。
示例代码:
SELECT from_unixtime(unix_timestamp('2023-08-22 23:23:59', 'yyyy-MM-dd HH:mm:ss'), 'yyyy-MM-dd HH:mm:ss') AS time_string;
- 字符串转数字: 使用 Hive 的内置函数'cast'将字符串转换为数字类型。
示例代码:
SELECT cast('001' AS int) AS number_value;
- 数字转字符串: 使用 Hive 的内置函数'cast'将数字类型转换为字符串。
示例代码:
SELECT cast(123 AS string) AS number_string;
原文地址: https://www.cveoy.top/t/topic/qE6d 著作权归作者所有。请勿转载和采集!