如何处理蜂巢中单个数字的多次出现

how to handle multiple occurrences for single digit in hive

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

select case when capture_date in ('','00','000') then NULL else from_unixtime(unix_timestamp(capture_date ,'yyyyMMdd'),'yyyy-MM-dd') 从 test_table;

结束为 capt_dt

我不想增加列表中的垃圾值,而是想以通用方式处理它,这意味着如果我们收到任意数量的 0,它应该填充为 NULL。

有什么解决办法吗?

似乎没有必要处理非法日期文字,因为它们在任何情况下都会产生 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