Hive 字符串、时间、数字类型转换:实战指南
在 Hive 环境下,可以使用 Hive 内置的函数来进行字符串转时间、时间转字符串、字符串转数字和数字转字符串的操作。
- 字符串转时间: 可以使用 Hive 内置的 to_unix_timestamp 函数将字符串转换为时间戳,然后再使用 from_unixtime 函数将时间戳转换为时间类型。
SELECT from_unixtime(to_unix_timestamp('2023-08-22 23:23:59', 'yyyy-MM-dd HH:mm:ss')) AS time;
这将返回'2023-08-22 23:23:59' 的时间类型。
- 时间转字符串: 可以使用 Hive 内置的 from_unixtime 函数将时间类型转换为字符串,可以指定输出的时间格式。
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;
这将返回'2023-08-22 23:23:59' 的字符串。
- 字符串转数字: 可以使用 Hive 内置的 cast 函数将字符串转换为数字类型。
SELECT CAST('001' AS INT) AS number;
这将返回'001' 的整数类型。
- 数字转字符串: 可以使用 Hive 内置的 cast 函数将数字类型转换为字符串。
SELECT CAST(1 AS STRING) AS number_string;
这将返回 1 的字符串类型。
原文地址: https://www.cveoy.top/t/topic/qE59 著作权归作者所有。请勿转载和采集!