免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Hive學(xué)習(xí)之路 (十一)Hive的5個面試題

目錄

 

正文

一、求單月訪問次數(shù)和總訪問次數(shù)

1、數(shù)據(jù)說明

數(shù)據(jù)字段說明

用戶名,月份,訪問次數(shù)

數(shù)據(jù)格式

A,2015-01,5A,2015-01,15B,2015-01,5A,2015-01,8B,2015-01,25A,2015-01,5A,2015-02,4A,2015-02,6B,2015-02,10B,2015-02,5A,2015-03,16A,2015-03,22B,2015-03,23B,2015-03,10B,2015-03,1

2、數(shù)據(jù)準備

(1)創(chuàng)建表

use myhive;create external table if not exists t_access(uname string comment '用戶名',umonth string comment '月份',ucount int comment '訪問次數(shù)') comment '用戶訪問表' row format delimited fields terminated by "," location "/hive/t_access"; 

(2)導(dǎo)入數(shù)據(jù)

load data local inpath "/home/hadoop/access.txt" into table t_access;

(3)驗證數(shù)據(jù)

select * from t_access;

3、結(jié)果需求

現(xiàn)要求出:
每個用戶截止到每月為止的最大單月訪問次數(shù)和累計到該月的總訪問次數(shù),結(jié)果數(shù)據(jù)格式如下

4、需求分析

此結(jié)果需要根據(jù)用戶+月份進行分組

(1)先求出當月訪問次數(shù)

--求當月訪問次數(shù)create table tmp_access(name string,mon string,num int); insert into table tmp_access select uname,umonth,sum(ucount) from t_access t group by t.uname,t.umonth;

select * from tmp_access;

(2)tmp_access進行自連接視圖

create view tmp_view as select a.name anme,a.mon amon,a.num anum,b.name bname,b.mon bmon,b.num bnum from tmp_access a join tmp_access b on a.name=b.name;select * from tmp_view;

(3)進行比較統(tǒng)計

select anme,amon,anum,max(bnum) as max_access,sum(bnum) as sum_access from tmp_view where amon>=bmon group by anme,amon,anum;

二、學(xué)生課程成績

 1、說明

use myhive;CREATE TABLE `course` (  `id` int,  `sid` int ,  `course` string,  `score` int ) ;
// 插入數(shù)據(jù)// 字段解釋:id, 學(xué)號, 課程, 成績INSERT INTO `course` VALUES (1, 1, 'yuwen', 43);INSERT INTO `course` VALUES (2, 1, 'shuxue', 55);INSERT INTO `course` VALUES (3, 2, 'yuwen', 77);INSERT INTO `course` VALUES (4, 2, 'shuxue', 88);INSERT INTO `course` VALUES (5, 3, 'yuwen', 98);INSERT INTO `course` VALUES (6, 3, 'shuxue', 65);

2、需求

求:所有數(shù)學(xué)課程成績 大于 語文課程成績的學(xué)生的學(xué)號

1、使用case...when...將不同的課程名稱轉(zhuǎn)換成不同的列

create view tmp_course_view asselect sid, case course when "shuxue" then score else 0 end  as shuxue,  case course when "yuwen" then score else 0 end  as yuwen from course;  select * from tmp_course_view;

 

2、以sid分組合并取各成績最大值

create view tmp_course_view1 asselect aa.sid, max(aa.shuxue) as shuxue, max(aa.yuwen) as yuwen from tmp_course_view aa group by sid;  select * from tmp_course_view1;

3、比較結(jié)果

select * from tmp_course_view1 where shuxue > yuwen;

三、求每一年最大氣溫的那一天  + 溫度

 1、說明

數(shù)據(jù)格式

2010012325

具體數(shù)據(jù)

View Code

數(shù)據(jù)解釋

2010012325表示在2010年01月23日的氣溫為25度

2、 需求

比如:2010012325表示在2010年01月23日的氣溫為25度?,F(xiàn)在要求使用hive,計算每一年出現(xiàn)過的最大氣溫的日期+溫度。
要計算出每一年的最大氣溫。我用
select substr(data,1,4),max(substr(data,9,2)) from table2 group by substr(data,1,4);
出來的是 年份 + 溫度 這兩列數(shù)據(jù)例如 2015 99

但是如果我是想select 的是:具體每一年最大氣溫的那一天 + 溫度 。例如 20150109 99
請問該怎么執(zhí)行hive語句。。
group by 只需要substr(data,1,4),
但是select substr(data,1,8),又不在group by 的范圍內(nèi)。
是我陷入了思維死角。一直想不出所以然。。求大神指點一下。
在select 如果所需要的。不在group by的條件里。這種情況如何去分析?

3、解析

(1)創(chuàng)建一個臨時表tmp_weather,將數(shù)據(jù)切分

create table tmp_weather as select substr(data,1,4) years,substr(data,5,2) months,substr(data,7,2) days,substr(data,9,2) temp from weather;
select * from tmp_weather;

(2)創(chuàng)建一個臨時表tmp_year_weather

create table tmp_year_weather as select substr(data,1,4) years,max(substr(data,9,2)) max_temp from weather group by substr(data,1,4);
select * from tmp_year_weather;

(3)將2個臨時表進行連接查詢

select * from tmp_year_weather a join tmp_weather b on a.years=b.years and a.max_temp=b.temp;

四、求學(xué)生選課情況

1、數(shù)據(jù)說明

(1)數(shù)據(jù)格式

id course 1,a 1,b 1,c 1,e 2,a 2,c 2,d 2,f 3,a 3,b 3,c 3,e

(2)字段含義

表示有id為1,2,3的學(xué)生選修了課程a,b,c,d,e,f中其中幾門。

2、數(shù)據(jù)準備

(1)建表t_course

create table t_course(id int,course string)row format delimited fields terminated by ",";

(2)導(dǎo)入數(shù)據(jù)

load data local inpath "/home/hadoop/course/course.txt" into table t_course;

3、需求

編寫Hive的HQL語句來實現(xiàn)以下結(jié)果:表中的1表示選修,表中的0表示未選修

id    a    b    c    d    e    f1     1    1    1    0    1    02     1    0    1    1    0    13     1    1    1    0    1    0

4、解析

第一步:

select collect_set(course) as courses from id_course;

第二步:

set hive.strict.checks.cartesian.product=false;create table id_courses as select t1.id as id,t1.course as id_courses,t2.course courses from ( select id as id,collect_set(course) as course from id_course group by id ) t1 join (select collect_set(course) as course from id_course) t2;

啟用嚴格模式:hive.mapred.mode = strict // Deprecated
hive.strict.checks.large.query = true
該設(shè)置會禁用:1. 不指定分頁的orderby
         2. 對分區(qū)表不指定分區(qū)進行查詢 
         3. 和數(shù)據(jù)量無關(guān),只是一個查詢模式

hive.strict.checks.type.safety = true
嚴格類型安全,該屬性不允許以下操作:1. bigint和string之間的比較
                  2. bigint和double之間的比較

hive.strict.checks.cartesian.product = true
該屬性不允許笛卡爾積操作

第三步:得出最終結(jié)果:
思路:
拿出course字段中的每一個元素在id_courses中進行判斷,看是否存在。

select id,case when array_contains(id_courses, courses[0]) then 1 else 0 end as a,case when array_contains(id_courses, courses[1]) then 1 else 0 end as b,case when array_contains(id_courses, courses[2]) then 1 else 0 end as c,case when array_contains(id_courses, courses[3]) then 1 else 0 end as d,case when array_contains(id_courses, courses[4]) then 1 else 0 end as e,case when array_contains(id_courses, courses[5]) then 1 else 0 end as f from id_courses;

五、求月銷售額和總銷售額

1、數(shù)據(jù)說明

(1)數(shù)據(jù)格式

a,01,150a,01,200b,01,1000b,01,800c,01,250c,01,220b,01,6000a,02,2000a,02,3000b,02,1000b,02,1500c,02,350c,02,280a,03,350a,03,250

(2)字段含義

店鋪,月份,金額

2、數(shù)據(jù)準備

(1)創(chuàng)建數(shù)據(jù)庫表t_store

use class;create table t_store(name string,months int,money int) row format delimited fields terminated by ",";

(2)導(dǎo)入數(shù)據(jù)

load data local inpath "/home/hadoop/store.txt" into table t_store;

3、需求

編寫Hive的HQL語句求出每個店鋪的當月銷售額和累計到當月的總銷售額

4、解析

(1)按照商店名稱和月份進行分組統(tǒng)計

create table tmp_store1 as select name,months,sum(money) as money from t_store group by name,months;select * from tmp_store1;

(2)對tmp_store1 表里面的數(shù)據(jù)進行自連接

create table tmp_store2 as select a.name aname,a.months amonths,a.money amoney,b.name bname,b.months bmonths,b.money bmoney from tmp_store1 a join tmp_store1 b on a.name=b.name order by aname,amonths;select * from tmp_store2;

(3)比較統(tǒng)計

select aname,amonths,amoney,sum(bmoney) as total from tmp_store2 where amonths >= bmonths group by aname,amonths,amoney;

 

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
zhouweifeng | 分割串問題
[GYCTF2020]Ezsqli 無列名注入
hive行列轉(zhuǎn)換面試題
hiveinsertinto語句和insertoverwrite語句
刪除數(shù)據(jù)庫中重復(fù)數(shù)據(jù)的幾個方法
hive bucket
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服