大数据踩过的坑——Hive insert

我在对Hive表数据清洗后,使用了如下SQL将结果集插入到新表中:

    insert into db_name.table_name_1 (
        col_1,col2,col3
    )
    with temp_table_1 as (
       select id,col_2
       from db_name.table_name_2 where id = condatition
    ),
    temp_table_2 as (
       select id,col_3
       from db_name.table_name_3 where id = condatition
    )
    select a.id,a.col_2,b.col_3
    from temp_table_1 a
    left join temp_table_2 b on a.id= b.id

出现了如下报错信息:

  • Error while compiling statement: FAILED: ParseException line 15:0 cannot recognize input near 'with' 'temp_table_1' 'as' in statement

错误原因:

Hive是支持with语法的,但是当与insert搭配使用时,语法与标准SQL语法规则不一样,需要将with放在insert之前,如下所示:

    with temp_table_1 as (
    select id,col_2
    f rom db_name.table_name_2 where id = condatition
    ),
    temp_table_2 as (
            select id,col3
    from db_name.table_name_3 where id = condatition
    )
    insert into db_name.table_name_1 (
        col_1,col2,col3
    )

    select a.id,a.col_2,b.col_3
    from temp_table_1 a

    left join temp_table_2 b on a.id= b.id

但是目标表db_name.table_name_1包含col_1,col_2,col_3,col_4,col_5等多个字段。上述insert语句执行后,又报了如下错误:

  • Error while compiling statement: FAILED: NullPointerException null

错误原因:Hive SQL中的Insert不支持插入部分字段

解决方案:将字段补全,没有数据的字段插入空值,修改如下:

    with temp_table_1 as (
    select id,col_2
    f rom db_name.table_name_2 where id = condatition
    ),
    temp_table_2 as (
            select id,col3
    from db_name.table_name_3 where id = condatition
    )
    insert into db_name.table_name_1 (
        col_1,col_2,col_3,col_4,col_5
    )
    select a.id,a.col_2,b.col_3,null,null
    from temp_table_1 a

    left join temp_table_2 b on a.id= b.id


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值