暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

hive建表、表关联、insert inot问题

五六六七七 2021-07-08
1983

1、为什么创建的表,数据导入后为什么只在一个列内呢?

    DROP tabledev.dev_risk_hitstrategy_zidian;
    CREATE
    TABLE IF NOT EXISTS dev.dev_risk_hitstrategy_zidian
    (
    NUMBER INT,
    hitstrategy STRING,
    hitstrategy_name VARCHAR(255)
    )

    因为没有加分列符号,需要在建表语句后加上分列的语句,注意分列符号要写对,否则结果也会不正确。

      ROW FORMAT DELIMITED FIELDS TERMINATED BY ','


      2、从一个json串中获取所需的内容,但当关联表时,报错格式无法匹配

        SELECT
        aa.hitStrategy,
        bb.hitstrategy_name,
        from(
        SELECT
        json_extract(e, '$.hitStrategy') AShitStrategy
        FROM
        risk_prism_hit_log
        GROUPBY
        json_extract(e, '$.hitStrategy'),
        )aa
        leftjoin
        (
        SELECThitstrategy,hitstrategy_name
        fromdev_risk_hitstrategy_zidian
        )bb
        on aa.hitstrategy=bb.hitstrategy


        报错内容如下:

          Query 20200727_060529_02587_98ijp failed: line 32:18: '=' cannot be applied to json, varchar


          原因:json格式和varchar格式不同无法进行比较,需要进行格式转换,可采用将json转为varchar

            SELECT
            aa.hitStrategy,
            bb.hitstrategy_name,
            from(
            SELECT
            cast(json_extract(e,'$.hitStrategy')as varchar) AShitStrategy
            FROM
            risk_prism_hit_log
            GROUPBY
            json_extract(e, '$.hitStrategy'),
            )aa
            leftjoin
            (
            SELECThitstrategy,hitstrategy_name
            fromdev_risk_hitstrategy_zidian
            )bb
            on aa.hitstrategy=bb.hitstrategy


            3hive使用insert inot插入数据时,遇到中文时会显示为乱码

              insert into  dev.dev_risk_hitstrategy_zidian values(111,'EEE','赔付策略')

              Hive不能直接使用values,需要使用select,例如:

              1将cite表中的数据复制一份,然后插入到原表中去

                insert into table cite select * fromcite;


                2)用tt表查出来的数据覆盖掉cite表格中已经存在的数据

                  insert overwrite table cite select *from tt;


                  3)使用decode函数

                  报错的:

                    insert into table(create_time ='20190616') values ('1','strategy','逆向赔付');

                    正确执行的:

                      insert into table(create_time='20190616') select '1','strategy',decode(binary('逆向赔付'),'utf-8');


                      文章转载自五六六七七,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                      评论