Hive实战——修改一条数据

背景:

测试环境经常需要修改大数据里面的一条数据用来满足测试,比如修改一条数据的手机号用来发短信

思路:

1.

insert overwrite table table1

select id,修改后的内容 as cols from table1 where id = 你修改行的id  ----------先弄出你要修改的那个增量行

union all  --------最后合并起来就得到所有的行

select * from table1 where id !=你想修改的内容的所在id   ----------然后弄出排除旧行所剩余的所有行

 

实战:

  • 代码:
    -- 准备数据
    create table user_info as
     values
            (1,'zs',15089943551),
            (2,'ls',15089943551),
            (3,'ww',15089943551),
            (4,'zl',15089943551),
            (5,'qq',15089943551);
    select * from user_info;
    
    -- SQL实现
    insert overwrite table user_info
    select col1,col2,13744238881 as col3 from user_info where col2='zs'
    union all
    select * from user_info where col2!='zs';

 

方法二:

用select 字段值 字段插入一条数据到表里,比如:

 

 

 

代码:

insert into table `wolf`.`zx_userinfo`
select '18016278480' device_id,
'' name,
'' ip,
'' user_agent,
'' ua_name,
'' ua_major,
'' resolution,
'' language,
'' net_type,
'' country,
'' plugin,
'' continent_code,
'' region,
'' city,
0 lat,
0 lgt,
'18016278480' as mo,
'' user_id,
'' username,
'' email,
1 gender,
'' address,
18 age,
current_date birthday,
'' company,
'' credit_card_number,
'' credit_card_provider,
'' credit_card_security_code,
'' job,
'' edu,
'' version
;

这里注意,必填的字段一定要填写,其他的可以为空的懒得填写就空就行,另外就是字段值的类型要和表字段类型一致,比如age是int ,手机号是字符串,字符串就得加单引号,birthday 的值这里利用函数current_date取当前时间,这个函数可以加括号也可以不加,加上是current_date()

 

posted @ 2022-08-11 11:01  fanghui778  阅读(5304)  评论(0)    收藏  举报