在 Hive 环境下,可以使用 Hive 的内置函数来实现字符串和时间、数字之间的转换。

  1. 字符串转时间: 使用 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;
  1. 时间转字符串: 使用 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;
  1. 字符串转数字: 使用 Hive 的内置函数'cast'将字符串转换为数字类型。

示例代码:

SELECT cast('001' AS int) AS number_value;
  1. 数字转字符串: 使用 Hive 的内置函数'cast'将数字类型转换为字符串。

示例代码:

SELECT cast(123 AS string) AS number_string;
Hive 数据类型转换:字符串、时间、数字互相转换详解

原文地址: https://www.cveoy.top/t/topic/qE6d 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录