How to make dynamic insert in hive from a field?

随声附和 提交于 2020-01-25 00:28:09

问题


I have a column where I have several dates, as follows:

Sun Oct 22 05:35:03 2017
Mon Apr 16 14:33:43 2018
Fri Apr 13 10:41:43 2018

I've created a process to filter these dates and convert to YYYYMMDD, as below.

20171022
20180416
20180413

This result will be used to distribute the data in their respective partitions, which are daily.

I tried to do it this way but I did not succeed:

insert into table tab2
PARTITION (REFERENCE_DATE = from_unixtime (unix_timestamp ('Sun Oct 22 05:35:03 2017', 'E MMM dd HH: mm: ss yyyy'), 'yyyyMMdd'))
SELECT
from_unixtime (unix_timestamp ('Sun Oct 22 05:35:03 2017', 'E MMM dd HH: mm: ss yyyy'), 'yyyyMMdd') as reference_date
FROM tab1 LIMIT 100;

how to do this insert?


回答1:


Use dynamic partition:

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

insert into table tab2
PARTITION (REFERENCE_DATE)
SELECT
from_unixtime (unix_timestamp ('Sun Oct 22 05:35:03 2017', 'E MMM dd HH: mm: ss yyyy'), 'yyyyMMdd') as reference_date
FROM tab1 LIMIT 100;

Better use yyyy-MM-dd date format because this is native Hive date format:

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

insert into table tab2
PARTITION (REFERENCE_DATE)
SELECT
      from_unixtime (unix_timestamp('Sun Oct 22 05:35:03 2017', 'E MMM dd HH: mm: ss yyyy'), 'yyyy-MM-dd') as reference_date
FROM tab1 LIMIT 100;


来源:https://stackoverflow.com/questions/55445080/how-to-make-dynamic-insert-in-hive-from-a-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!