sql - 将两列合并为一列并格式化内容以在 Hive 中形成准确的日期时间格式?

标签 sql regex hadoop hive datetime-format

这些是 2 列(月,年)。我想从它们中创建一个具有准确日期时间格式('YYYY-MM-DD HH:MM:SS')的列,并将其添加为表中的新列。

 Month     year
 12/ 3   2013 at 8:40pm
 12/ 3   2013 at 8:39pm
 12/ 3   2013 at 8:39pm
 12/ 3   2013 at 8:38pm
 12/ 3   2013 at 8:37pm

最好的 hive 查询可能是什么?我无法为 形成准确的正则表达式。

最佳答案

我假设 12 是 month,而 3 是 day,因为您没有指定。此外,您说您想要 HH:MM:SS 但您的示例中没有秒,所以我不知道您将如何将它们放在那里。我还在您的示例中将 8:37pm 更改为 8:37am 以尝试这两种情况。

查询:

  select concat_ws(' ', concat_ws('-', yr, month, day)
                      , concat_ws(':', hour, minutes)) date_time
  from (
    select yr
      , case when length(month) < 2 then concat('0', month) else month end as month
      , case when length(day) < 2 then concat('0', day) else day end as day
      , case when instr(minutes, 'pm') > 0 then cast(hour+12 as int)
             when instr(minutes, 'am') > 0 and length(hour) < 2 then concat('0', hour)
             else hour end as hour
      , substr(minutes, 1, 2) minutes
    from (
    select ltrim(split(Month, '\\/')[1]) day
      , split(Month, '\\/')[0] month
      , split(year, ' ')[0] yr
      , split(split(year, ' ')[2], '\\:')[0] hour
      , split(split(year, ' ')[2], '\\:')[1] minutes
    from test.sample_data ) x ) y

输出:

date_time

2013-12-03 20:40
2013-12-03 20:39
2013-12-03 20:39
2013-12-03 20:38
2013-12-03 08:37

关于sql - 将两列合并为一列并格式化内容以在 Hive 中形成准确的日期时间格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30873875/

相关文章:

python - 显示 sqlite 数据库查询的结果

MySQLIntegrityConstraintViolationException : Column 'adno' in where clause is ambiguous

sql - Insert 失败,但 identity value 增长了,这是否违反了 Atomicity 规则?

java - 正则表达式仅删除特殊字符而不删除其他语言字母

python - 确定 Python 对象是正则表达式还是字符串

C#-运行Hadoop_job-报错(C#)

sql - 在 SQL 和 ESRI 定义查询中选择比两个日期/时间之一更新的记录

regex - 如何在正则表达式中允许\b 替换 sed 命令

hadoop - 如何在Spark而不是RAM的磁盘上进行计算?

MongoDB hadoop 连接器无法查询 mongo hive 表