【问题标题】:I want to add an extra column in my existing hive table so that I can have a current time stamp for that day我想在现有的配置单元表中添加一个额外的列,以便我可以拥有当天的当前时间戳
【发布时间】:2019-07-03 04:02:15
【问题描述】:

需要在我现有的配置单元表中添加额外的日期列,以便它获取 当天系统中的当前日期

hive (hivejobs)>
select * from my_current_Table;
OK
name   age
Aditya  2
Aditya  7

我想在此处添加一个日期列,以便在添加列后立即获取当天的当前系统日期。我认为解决方法是将两个表与具有当前系统日期的另一个表连接起来。

以下是我的代码和思考过程。

alter table my_current_Table add columns( todays_date current_date());

这给了我一个错误,我无法找到实际的方法

请帮忙。

预期输出

hive (hivejobs)> 
select * from my_current_Table;

OK
name   age  todays_date
Aditya  2   2019-02-08 13:21:50
Aditya  7   2019-02-08 13:21:50

【问题讨论】:

  • 使用now()有问题吗?

标签: sql hadoop hive timestamp hiveddl


【解决方案1】:

您不能在 Hive 表中定义计算列。

如果您想始终查询今天的日期,请在 select 中使用current_timestamp 函数:

select t.*, 
       date_format(current_timestamp,'yyyyMMdd HH:mm:ss') as todays_date 
  from my_current_Table t;

如果要添加列并存储插入记录的日期,请将列添加到表中并从自身重新加载,例如:

alter table my_current_Table  add columns(load_date string);

insert overwrite table my_current_Table 
select t.*, 
           date_format(current_timestamp,'yyyyMMdd HH:mm:ss') as load_date
      from my_current_Table t;

【讨论】:

    【解决方案2】:

    以下是相同的步骤,

    1. 创建临时表

    创建表 my_current_Table_temp(name string, age int);

    1. 使用文件或现有表将数据插入 my_current_Table_temp 表:

    姓名|年龄

    阿迪亚| 2

    阿迪亚| 7

    1. 创建一个包含最终数据的表:

    create table my_current_Table(name string, age tinyint, todays_date 字符串);

    1. 从临时表中插入数据,并添加具有默认值的列作为当前日期和时间:

    插入覆盖表 my_current_Table 选择名称、年龄、 FROM_UNIXTIME(UNIX_TIMESTAMP(), 'dd/MM/YYYY HH:mm') 从 我的_current_Table_temp;

    【讨论】:

      猜你喜欢
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      相关资源
      最近更新 更多