每天一道大厂SQL题【Day11】微众银行真题实战(一)_微众银行大数据面试(1)

set spark.sql.shuffle.partitions=4;
create database webank_db;
use webank_db;
create or replace temporary view check_view (ds comment '日期分区',
sno comment '流水号', uid comment '用户id',
is_risk_apply comment '是否核额申请',
is_pass_rule comment '是否通过规则',
is_obtain_qutoa comment '是否授信成功', quota comment '授信金额',
update_time comment '更新时间')
as
values ('20201101', 's000', 'u000', 1, 1, 1, 700, '2020-11-01 08:12:12'),
('20201102',	's088',	'u088',	1,	1,	1,	888, '2020-11-02 08:12:12'),
('20201230',	's091',	'u091',	1,	1,	1,	789, '2020-12-30 08:12:12'),
('20201230',	's092',	'u092',	1,	0,	0,	0, '2020-12-30 08:12:12'),
('20201230',	's093',	'u093',	1,	1,	1,	700, '2020-12-30 08:12:12'),
('20201231',	's094',	'u094',	1,	1,	1,	789, '2020-12-31 08:12:12'),
('20201231',	's095',	'u095',	1,	1,	1,	600, '2020-12-31 08:12:12'),
('20201231',	's096',	'u096',	1,	1,	0,	0, '2020-12-31 08:12:12')
;
--创建核额流水表 
drop table if exists check_t;
create table check_t (
sno string comment '流水号', uid string,
is_risk_apply bigint, is_pass_rule bigint, is_obtain_qutoa bigint, quota decimal(30,6), update_time string
) partitioned by (ds string comment '日期分区');
--动态分区需要设置 
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict; insert overwrite table check_t partition (ds) select sno,
uid, is_risk_apply, is_pass_rule, is_obtain_qutoa, quota, update_time,
ds
from check_view;


img

-- 创 建 借 据 表
create table debt(
duebill_id	string comment '借据号',
uid	string, prod_type		string, putout_date string, putout_amt		decimal(30, 6),
balance	decimal(30, 6), is_buliang		int, overduedays int
)partitioned by (ds string comment '日期分区');
--资料提供了一个34899条借据数据的文件 
--下面补充如何将文件的数据导入到分区表中。需要一个中间普通表过度。drop table if exists webank\_db.debt\_temp;
create table webank_db.debt_temp(
duebill_id		string comment '借据号', uid	string,
prod_type	string,
putout_date string, putout_amt	decimal(30, 6),
balance decimal(30,6),
is_buliang	int, overduedays int,
ds string comment '日期分区'
) row format delimited fields terminated by '\t';
load data local inpath '/root/debt.txt' overwrite into table webank_db.debt_temp;

--动态分区需要设置 
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table webank_db.debt partition (ds)
select  from webank_db.debt_temp;

--技巧:如果查询debt表,由于分区数太多,导致查询很慢。 
-- 开发阶段,我们可以事先将表缓存起来,并且降低分区数比如为6,那么查缓存表大大提升了开发效率。 
-- 上线阶段,再用实际表替换缓存表。 
--首次缓存会耗时慢 
cache table cache_debt as select /+ coalesce(6) /  from
debt;
--第二次使用缓存会很快 
select count() from cache_debt;
select ds,count(1) from cache_debt group by ds;

先了解表数据的分布情况,有2年多,每天都有分区,共760多个分区。

img

img

随机观察2个借据的情况

img

img

思路分析

方案1
假设当天是20201231,昨日是20201230
预先将复用分数据集缓存起来,只用加载一次源表。后面多次union all起来。cache table仅Spark支持,hive不支持。

方案2

借用stack函数,性能与方案1一样 ,都只加载一次表。

答案获取

建议你先动脑思考,动手写一写再对照看下答案,如果实在不懂可以点击下方卡片,回复:大厂sql 即可。
参考答案适用HQL,SparkSQL,FlinkSQL,即大数据组件,其他SQL需自行修改。

加技术群讨论

点击下方卡片关注 联系我进群

或者直接私信我进群

微众银行源数据表附录:
  1. 核额流水表
字段名字段意义字段类型
ds日期分区,样例格式为20200101,每个分区有全量流水string
sno每个ds内主键,流水号string
uid户idstring
is_risk_apply是否核额申请(核额漏斗第一步)取值0和1bigint
is_pass_rule是否通过规则(核额漏斗第二步)取值0和1bigint
is_obtain_qutoa是否授信成功(核额漏斗第三步)取值0和1bigint
quota授信金额decimal(30,6)
update_time更新时间样例格式为2020-11-14 08:12:12string
  1. 借据表
字段名字段意义字段类型
ds日期分区,样例格式为20200101每个分区有全量借据strng
duebilid借据号(每个日期分区内的主键)strng
uid用户idstring
prod_type产品名称仅3个枚举值XX贷YY贷ZZ贷string
putout_date发放日期样例格式为2020-10-10 00:10:30bigint
putout_amt发放金额decimal(30,6)
balance借据余额decimal(30,6)
is_buliang状态-是否不良取值0和1bigint
overduedays逾期天数bigint

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值