问题
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