apache pig-仅从时间戳获取日期

cx6n0qe3  于 2021-06-21  发布在  Pig
关注(0)|答案(1)|浏览(328)

我有以下代码:

Data = load '/user/cloudera/' using PigStorage('\t') 
as
(   ID:chararray, 
    Time_Interval:chararray, 
    Code:chararray); 

transf = foreach Source_Data generate  (int) ID, 
                                   ToString( ToDate((long) Time_Interval), 'yyyy-MM-dd hh:ss:mm') as TimeStamp,
                        (int) Code; 

SPLIT transf INTO       Src25 IF (ToString(TimeStamp, 'yyyy-MM-dd')=='2016-07-25'),
                        Src26 IF (ToString(TimeStamp, 'yyyy-MM-dd')=='2016-07-26');

STORE Src25 INTO '/user/cloudera/2016-07-25' using PigStorage('\t');
STORE Src26 INTO '/user/cloudera/2016-07-26' using PigStorage('\t');

我想按日期和规则分割文件,我把分割语句它给我错误。。。
如何转换date中的timestamp(用于transf语句)以进行比较?
非常感谢!

67up9zun

67up9zun1#

从todate获取datetime对象后,对datetime对象使用getyear()、getmonth()、getday(),并使用concat仅构造日期。

transf = foreach Source_Data generate  
                   (int) ID, 
                   ToString( ToDate((long) Time_Interval), 'yyyy-MM-dd hh:ss:mm') as TimeStamp,
                   (int) Code;

transf_new = foreach transf generate
                     ID,
                     TimeStamp,
                     CONCAT(CONCAT(CONCAT(GetYear(TimeStamp),'-')),(CONCAT(GetMonth(TimeStamp),'-')),GetDay(TimeStamp)) AS Day,-- Note:Brackets might be slightly off but it should be like 'yyyy-MM-dd' format
                     Code;

-- Now use the new Day column to split the data
SPLIT transf_new INTO       Src25 IF (Day =='2016-07-25'),
                            Src26 IF (Day =='2016-07-26');

相关问题