如何处理Hive中单位数的多个事件



我想格式化来自源文件的日期。我正在处理垃圾值,因为此列可能包含0(可能是0或00或000等)

选择CASTURE_DATE在('',',00','000')中选择案例,然后从_unixtime(unix_timestamp(capture_date,'yyyymmdd'),'yyyy-mm-dd')null否则为capte_dt test_table; p>而不是在列表中增加垃圾值,我想以通用的方式处理它,这意味着如果我们收到任何数字0,则应以null为单位。

任何解决方案?

似乎没有意义,因为在任何情况下它们都会产生无效的值(除非我们有7个零或更多)

hive> with test_table as (select stack(5,'','0','00','000','20170831') as capture_date)
    > select  from_unixtime(unix_timestamp(capture_date,'yyyyMMdd'),'yyyy-MM-dd') as capt_dt 
    > from    test_table
    > ;
OK
capt_dt
NULL
NULL
NULL
NULL
2017-08-31

如果7个或更多零是可选的 -

hive> with test_table as (select stack(5,'','0','00','000000000000','20170831') as capture_date)
    > select  from_unixtime(unix_timestamp(regexp_replace(capture_date,'^0+$',''),'yyyyMMdd'),'yyyy-MM-dd') as capt_dt 
    > from    test_table
    > ;
OK
capt_dt
NULL
NULL
NULL
NULL
2017-08-31

hive> with test_table as (select stack(5,'','0','00','000000000000','20170831') as capture_date)
    > select  case 
    >             when capture_date not rlike '^0+$' 
    >             then from_unixtime(unix_timestamp(capture_date,'yyyyMMdd'),'yyyy-MM-dd')  
    >         end as capt_dt
    >         
    > from    test_table
    > ;
OK
capt_dt
NULL
NULL
NULL
NULL
2017-08-31

最新更新