网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

传统hive数据表怎么通过写SQL方式实现数据的更新

这篇文章主要为大家展示了“传统hive数据表怎么通过写SQL方式实现数据的更新”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“传统hive数据表怎么通过写SQL方式实现数据的更新”这篇文章吧。

创新互联专注于鸡泽企业网站建设,成都响应式网站建设公司,商城网站建设。鸡泽网站建设公司,为鸡泽等地区提供建站服务。全流程定制网站设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

1.这里新建两张表student10,student10_temp(临时表,可看成业务库),建表语句如下:

//学生信息表create table student10(id string,name string,age string) row format delimited fields terminated by',' stored as textfile;//学生信息表临时表,业务关系数据库的变化,一般先导入临时表create table student10_temp(id string,name string,age string) row format delimited fields terminated by',' stored as textfile;

2.数据准备,可理解成业务库更新前全量数据,文件test.txt,数据如下:

[root@salver158 ~]# cat /tmp/test.txt 1,name1,162,name2,173,name3,18

3.这里先将test.txt中的数据加载到student10_temp表中,执行命令:

hive> load data local inpath '/tmp/test.txt'  into table student10_temp;Loading data to table lujs.student10_tempTable lujs.student10_temp stats: [numFiles=1, numRows=0, totalSize=33, rawDataSize=0]OKTime taken: 1.275 seconds

    执行insert into将student10_temp,全量插入到student10表中:

hive> insert into table student10 select id,name,age from student10_temp;

    到这里两张表数据相同,这就相当于我们生产上,第一次进行了全量数据的导入;这时候我们更新了student10_temp表(这里模拟业务关系数据库中删除一条数据,插入了两条数据),这里直接加载文件test1.txt:

[root@salver158 tmp]# cat  /tmp/test1.txt 2,name2,173,name3,5104,name4,195,name5,20

    其实这个student10_temp一般代表的是关系型数据库的变化,然后可通过sqoop等数据抽取工具,全量抽取关系数据库已变化的数据到student10_temp临时表中。

hive> select * from student10_temp;OK2  name2  173  name3  5104  name4  195  name5  20

4.我们可以通过SQL进行数据比对,然后把非增量、非更新的数据覆盖到student10表中,执行完SQL后可保证student10和student10_temp数据不重复(这里表必须要有主键,不然没法关联)

    找出非增量、非更新数据,一般就是已删除数据,执行命令:

select b.id,b.name,b.age from student10 a left join student10_temp b on a.id=b.id  where b.id is nul

    将非增量、非更新数据覆盖到student10表,执行命令:

insert overwrite table student10 select b.id,b.name,b.age from student10 a left join student10_temp b on a.id=b.id  where b.id is null

    执行完后,表中只有非增量、非更新数据,一般就是业务库已删除数据,这里我们可以选择保留做个标识;

5.经过步骤4的比对去重之后,则可以把临时表数据加载进来,执行命令:

hive> insert into table student10  select  id,name,age from student10_temp;

    查看最终数据,包含非增量、非更新数据,以及更新和增量数据:

hive> select * from student10;OK1  name1  162  name2  173  name3  5104  name4  195  name5  20Time taken: 0.436 seconds, Fetched: 5 row(s)

    我这里只是简单的一种数据更新方案,中间可能问题,数据更新之前最后都要做好备份,以免丢失数据。

补充SQL:

    1.比对新增、更新数据SQL,这里可以执行步骤4之前查看,可在student10表中添加一个标识字段来标识,是已删除、更新、还是新增数据,这里就不在演示,有兴趣自己去研究下:

hive> select a.id,a.name,a.age,CASE WHEN b.id IS null THEN '新增' ELSE '更新'  END  FROM  student10_temp a LEFT JOIN  student10  b on a.id=b.id;Query ID = root_20200310194355_d5428597-77a7-40e6-8b56-6b636c3e137eTotal jobs = 1Launching Job 1 out of 1Status: Running (Executing on YARN cluster with App id application_1583204243863_0026)
--------------------------------------------------------------------------------        VERTICES      STATUS  TOTAL  COMPLETED  RUNNING  PENDING  FAILED  KILLED--------------------------------------------------------------------------------Map 1 ..........   SUCCEEDED      1          1        0        0       0       0Map 2 ..........   SUCCEEDED      1          1        0        0       0       0--------------------------------------------------------------------------------VERTICES: 02/02  [==========================>>] 100%  ELAPSED TIME: 6.59 s     --------------------------------------------------------------------------------OK2  name2  17  更新3  name3  510  更新4  name4  19  新增5  name5  20  新增

以上是“传统hive数据表怎么通过写SQL方式实现数据的更新”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


当前名称:传统hive数据表怎么通过写SQL方式实现数据的更新
本文路径:http://bjjierui.cn/article/igocos.html

其他资讯