【问题标题】:How to insert date,boolean filed value in apache hive?如何在 apache hive 中插入日期、布尔字段值?
【发布时间】:2019-01-17 18:58:26
【问题描述】:

这是我的示例数据集。

#cust_id,  #cust_name, #odr_date,#shipdt,#Courer,#recvd_dt,#returned or not,#returned dt,#reson of return
GGYZ333519YS,Allison,01-01-2017,03-01-2017,Fedx,06-01-2017,**no**,null,null
GGYZ333519YS,Allison,08-01-2017,10-01-2017,Delhivery,13-01-2017,**yes**,15-01-2017,Damaged Item

并创建表结构。

create table order
( 
cust_id string,
cust_name string,
order_date date,
ship_date date,
courier_name string,
received_date date,
is_returned boolean,
returned_date date,
reason string
)
row format delimited
fields terminated by ','
lines terminated by '\n'
stored as textfile;

使用 load 命令将数据加载到订单表中。获取日期字段和布尔字段列的 NULL。任何想法?如何解决这个问题呢 。

【问题讨论】:

    标签: hive bigdata hiveql


    【解决方案1】:

    日期应采用兼容的格式 'yyyy-MM-dd' 才能正确插入 DATE。 BOOLEAN 应该是 (TRUE, FALSE) 之一。

    解决方案是将列定义为 STRING 并在选择期间对其进行转换,或者在加载到表中之前转换输入数据。

    如果列定义为 STRING,则可以在选择期间转换数据:

    select
          from_unixtime(unix_timestamp( returned_date ,'dd-MM-yyyy'), 'dd-MMM-yyyy') as returned_date,
          case when is_returned like '%no%' then false
               when is_returned like '%yes%' then true
               --else will be null by default
           end as is_returned 
    

    【讨论】:

    • 感谢您的意见。我已经加载了数据。我正在使用 select 语句获取 NULL 值。 select from_unixtime(unix_timestamp( returned_date ,'MM-dd-yyyy'), 'dd-MMM-yyyy') from order;.
    • @LearnHadoop 使用字符串而不是 DATE 和 BOOLEAN 重新创建表
    • 如何使用插入语句将字符串格式日期插入日期字段。在 oracle 中,我们有 to_date 函数。在蜂巢中?
    • @LearnHadoop 相同的 to_date,如果字符串的时间戳比日期长。如果日期字符串已经采用正确的格式“yyyy-MM-dd”,则无需转换,您可以将其直接插入到日期中。使用我的答案中的解决方案来转换日期格式。字符串应在 yyyy-MM-dd 中插入 Date
    猜你喜欢
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2012-05-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    相关资源
    最近更新 更多