十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

头歌实践教学平台:大数据存储2023(十一)

头歌实践教学平台:大数据存储2023(十一) 十一、Hive基本查询操作一第1关where操作任务描述本关任务使用where和like求出编程要求中所给需求。相关知识where将不满足条件的行过滤在SQL语句中执行顺序优先于group by。having对where的一个补充过滤成组后的数据执行顺序后于group by。likelike 操作符用于在WHERE子句中搜索列中的指定模式。%代表任意多个字符。假设存在student表name agebob 22cindy 27herry 26可以使用where过滤查询出成绩大于25岁的学生名字。select name from student where age25;输出cindyherry编程要求在右侧编辑器中补充SQL查询出工作职责涉及hive的并且工资大于8000的公司名称以及工作经验。其中库名db1表名table1student表结构INFO TYPEeduLevel_name Stringcompany_name StringjobName Stringsalary intcity_code intresponsibility StringworkingExp String本地部分文件内容本科,北京联通支付有限公司,大数据开发工程师,10000,530,熟练使用hive等,1-3年专科,北京联科数创科技有限公司,大数据分析师,8000,530,熟练使用MySQL等数据库,1-3年本科,湖南智湘赢播网络技术有限公司,大数据开发工程师,16000,749,熟练使用spark等,3-5年测试说明平台会对你编写的代码进行测试预期输出1-3年 北京联通支付有限公司1-3年 深圳市德科信息技术有限公司广州分公司开始你的任务吧祝你成功----------禁止修改----------create database if not exists db1;use db1;create table if not exists table1(eduLevel_name string comment 学历,company_name string comment 公司名,jobName string comment 职位名称,salary int comment 薪资,city_code int comment 城市编码,responsibility string comment 岗位职责,workingExp string comment 工作经验)row format delimited fields terminated by ,lines terminated by \nstored as textfile;truncate table table1;load data local inpath /root/aaa.txt into table table1;----------禁止修改--------------------Begin----------SELECT workingExp, company_nameFROM table1WHERE responsibility LIKE %hive% AND salary 8000;----------End----------第2关group by操作任务描述本关任务实现不同工作年限的平均工资需求。相关知识group bygroup by表示按照某些字段的值进行分组有相同的值放到一起需要注意的是select后面的非聚合列必须出现在group by中假设存在st表city salary job长沙 7000 大数据开发北京 10000 大数据开发广州 11000 大数据开发长沙 7000 大数据开发可以使用group by求出不同省份的平均工资select city,avg(salary)from st group by city;输出长沙 7000北京 10000广州 11000编程要求在右侧编辑器中补充SQL计算不同工作年限以及其平均工资并且过滤出平均工资大于10000的。其中库名db1表名table1table1表结构INFO TYPEeduLevel_name Stringcompany_name StringjobName Stringsalary intcity_code intresponsibility StringworkingExp String本地部分文件内容本科,北京联通支付有限公司,大数据开发工程师,10000,530,熟练使用hive等,1-3年专科,北京联科数创科技有限公司,大数据分析师,8000,530,熟练使用MySQL等数据库,1-3年本科,湖南智湘赢播网络技术有限公司,大数据开发工程师,16000,749,熟练使用spark等,3-5年测试说明平台会对你编写的代码进行测试预期输出17000.0 3-5年20000.0 5-10年开始你的任务吧祝你成功----------禁止修改----------create database if not exists db1;use db1;create table if not exists table1(eduLevel_name string comment 学历,company_name string comment 公司名,jobName string comment 职位名称,salary int comment 薪资,city_code int comment 城市编码,responsibility string comment 岗位职责,workingExp string comment 工作经验)row format delimited fields terminated by ,lines terminated by \nstored as textfile;truncate table table1;load data local inpath /root/t1.txt into table table1;----------禁止修改--------------------Begin----------SELECT AVG(salary) AS avg_salary, workingExpFROM table1GROUP BY workingExpHAVING AVG(salary) 10000;----------End----------第3关join操作任务描述本关任务通过关联求出每个城市名的平均工资。相关知识Hive只支持等值连接即ON子句中只能使用等号连接假设存在表a:id name1 bob2 lily3 herry表b:cid score1 802 905 60内连接(JOIN)内连接指的是把符合两边连接条件的数据查询出来select a.name,b.score from a join b on a.idb.cid;输出结果bob 80lily 90左外连接LEFT OUTER JOIN左表全部查询出来右表不符合连接条件的显示为空select a.name,b.score from a left outer join b on a.idb.cid;输出结果bob 80lily 90herry null右外连接RIGHT OUTER JOIN右表全部查询出来左表不符合连接条件的显示为空select a.name,b.score from a right outer join b on a.idb.cid;输出结果bob 80lily 90null 60全外连接FULL OUTER JOIN左右表符合连接条件和不符合连接条件的都查出来不符合的显示空select a.name,b.score from a full outer join b on a.idb.cid;输出结果bob 80lily 90herry nullnull 60左半开连接LEFT SEMI JOIN)查询出满足连接条件的左边表记录需要注意的是select和where语句中都不能使用右表的字段。Hive不支持右半开连接select a.name from a LEFT SEMI JOIN b on a.idb.cid;输出结果boblily编程要求在右侧编辑器中补充SQL求出表table2中所有城市名的平均工资。其中库名db1表名table1表名table2表table1结构INFO TYPEeduLevel_name Stringcompany_name StringjobName Stringsalary intcity_code intresponsibility StringworkingExp Stringtable1本地部分文件内容本科,北京联通支付有限公司,大数据开发工程师,10000,530,熟练使用hive等,1-3年专科,北京联科数创科技有限公司,大数据分析师,8000,530,熟练使用MySQL等数据库,1-3年本科,湖南智湘赢播网络技术有限公司,大数据开发工程师,16000,749,熟练使用spark等,3-5年表table2结构INFO TYPEcity_code intcity_name Stringtable2本地部分文件内容538,上海653,杭州749,长沙763,广州测试说明平台会对你编写的代码进行测试预期输出8000.0 上海9000.0 北京NULL 天津12000.0 广州7500.0 杭州10000.0 深圳12000.0 长沙开始你的任务吧祝你成功----------禁止修改----------create database if not exists db1;use db1;create table if not exists table1(eduLevel_name string comment 学历,company_name string comment 公司名,jobName string comment 职位名称,salary int comment 薪资,city_code int comment 城市编码,responsibility string comment 岗位职责,workingExp string comment 工作经验)row format delimited fields terminated by ,lines terminated by \nstored as textfile;truncate table table1;load data local inpath /root/t2.txt into table table1;create table if not exists table2(city_code int comment 城市编码,city_name string comment 城市名)row format delimited fields terminated by ,lines terminated by \nstored as textfile;truncate table table2;load data local inpath /root/t22.txt into table table2;----------禁止修改--------------------Begin----------SELECT AVG(t1.salary) AS avg_salary, t2.city_nameFROM table2 t2LEFT JOIN table1 t1 ON t2.city_code t1.city_codeGROUP BY t2.city_nameORDER BY t2.city_name;----------End----------有任何问题都可以随时关注私信
返回列表