有没有更好的方法将时间戳(hh:mm:ss)转换为hive中的秒?

izkcnapc  于 2021-04-03  发布在  Hive
关注(0)|答案(1)|浏览(434)

我有一个hive字段,类型为字符串,其时间戳的格式如下:hh:mm:ss mm:ss ss 我需要将它们转换为以下格式。

Input: 
10:30:40
   30:40
      40
Output Expected:
    10:30:40 = (10*3600) + (30 * 60) + 40  = 37,840
       30:40 =             (30 * 60) + 40  =   1840
          40 =                         40  =     40

我试着做这样的事情

case 
    when duration  like '%:%:%' then 
            split(duration, ':')[0] * 3600 + 
            split(duration, ':')[1] * 60 + 
            split(duration, ':')[2] 
        when duration  like  '%:%' then 
            split(duration, ':')[0] * 60 + 
            split(duration, ':')[1] 
        else 
            duration 
        end

当我要处理数十亿条记录时,是否有更好的方法来完成同样的工作。

fd3cxomn

fd3cxomn1#

你的表达式在hive中执行时不会产生很多额外的负载。你可以使用unix_timestamp函数来简化查询,但它的运行速度不会更快。

with input as(--use your table instead of this
select stack(3, '10:30:40',
                '30:40',
                '40') as duration
)

select duration, case when duration like '%:%:%' then unix_timestamp(duration,'HH:mm:ss') 
                      when duration like '%:%'   then unix_timestamp(duration,'mm:ss') 
                      else duration
                  end as result
 from input

结果。

duration    result
10:30:40    37840
30:40       1840
40          40

或者更简单。

select duration, coalesce(unix_timestamp(duration,'HH:mm:ss'), unix_timestamp(duration,'mm:ss'), duration) as result

返回完全一样的。

相关问题