【头歌】Hive基本查询操作(一) 答案

本专栏已收集头歌大数据所有答案 以供参考

第1关:where操作

答案 复制点击评测

----------禁止修改----------
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 '\n'
stored as textfile;
truncate table table1;
load data local inpath '/root/aaa.txt' into table table1;
----------禁止修改----------

----------Begin----------
select workingExp,company_name from table1 where responsibility like '%hive%' and salary > '8000';
----------End----------

第2关:group by操作

答案复制点击评测

----------禁止修改----------
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 '\n'
stored as textfile;
truncate table table1;
load data local inpath '/root/t1.txt' into table table1;
----------禁止修改----------

----------Begin----------
select avg(salary) as avgsalary,workingExp from table1 group by workingExp having avgsalary > 10000;
----------End----------


第3关:join操作


答案 复制点击测评

----------禁止修改----------
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 '\n'
stored 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 '\n'
stored as textfile;
truncate table table2;
load data local inpath '/root/t22.txt' into table table2;
----------禁止修改----------

----------Begin----------
select avg(table1.salary),table2.city_name from table1 right outer join table2 on table1.city_code=table2.city_code group by table2.city_name ;
----------End----------

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive是一个基于Hadoop的数据仓库工具,可以将结构化数据映射为一张数据库表,然后通过类SQL的命令进行查询和分析。本文将介绍Hive基本查询操作。 1. 创建表 在Hive中,首先需要创建表并定义表的结构,例如: ``` CREATE TABLE IF NOT EXISTS employee ( id INT, name STRING, age INT, salary FLOAT, address STRING ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' STORED AS TEXTFILE; ``` 上述代码创建了一个名为employee的表,包含id、name、age、salary和address五个字段,数据类型分别为整数、字符串、整数、浮点数和字符串。 2. 加载数据 在创建好表之后,需要将数据加载到表中,例如: ``` LOAD DATA LOCAL INPATH '/path/to/data.txt' OVERWRITE INTO TABLE employee; ``` 上述代码将本地路径为/path/to/data.txt的数据文件加载到名为employee的表中。 3. 查询数据 Hive支持类SQL的查询语句,例如: ``` SELECT name, age FROM employee WHERE salary > 5000; ``` 上述代码查询了表employee中salary大于5000的记录,并返回这些记录的name和age字段。 4. 聚合数据 Hive还支持聚合查询,例如: ``` SELECT COUNT(*) FROM employee WHERE age > 30; ``` 上述代码查询了表employee中年龄大于30岁的记录数。 5. 分组数据 Hive还支持分组查询,例如: ``` SELECT address, AVG(salary) FROM employee GROUP BY address; ``` 上述代码查询了表employee中每个地址的平均工资。 6. 排序数据 Hive支持对查询结果进行排序,例如: ``` SELECT * FROM employee ORDER BY salary DESC; ``` 上述代码查询了表employee中所有记录,并按照工资从高到低进行排序。 以上就是Hive基本查询操作,可以帮助你快速了解和使用Hive进行数据分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值