本关任务
- 基于EduCoder平台提供的初始数据集,统计成功订单线路中出行次数最多的五条线路。
编程要求
- 1、在 hive 数据库 trafficdata 中 数据库创建表 orderline 。使用 Hive SQL 来统计成功订单线路中出行次数最多的五条线路(若线路经纬度而线路名一样,取这条线路出现最多次的经纬度),并插入到 orderline表。
- 2、使用 Sqoop 工具将 Hive 中orderline 表数据存放至 MySQL 数据库 trafficdata 的 orderline 表中。
过关代码
- 开启Hadoop服务,并使得 Hive 连接 MySQL 初始化
start-all.sh
schematool -dbType mysql -initSchema
- 进入hive命令行,检查trafficdata数据库以及cancelorder表和createorder表是否存在(不存在则创建,为第一关任务)
- 进入trafficdata数据库
use trafficdata;
- 在hive数据库中创建orderline表:
create table orderline(departure string,deplongitude string,deplatitude string,destination string,destlongitude string,destlatitude string,num int) row format delimited fields terminated by '\t' stored as textfile;
- 筛选出题意所需数据,并存入tt表中
create table tt as select departure,deplongitude,deplatitude,destination,destlongitude,destlatitude,count from (SELECT t2.name,departure,deplongitude,deplatitude,destination,destlongitude,destlatitude,t1.num,t2.num count,Row_Number() OVER (partition by t1.name order by t1.num) rank from (SELECT (case when departure <= destination then CONCAT(departure,"%%%",destination) else CONCAT(destination,"%%%",departure) end) as name,departure,deplongitude,deplatitude,destination,destlongitude,destlatitude,count(*) as num FROM createorder group by (case when departure <= destination then CONCAT(departure,"%%%",destination) else CONCAT(destination,"%%%",departure) end),departure,deplongitude,deplatitude,destination,destlongitude,destlatitude) as t1 right join (SELECT name ,count(name) as num from(SELECT (case when departure <= destination then CONCAT(departure,"%%%",destination) else CONCAT(destination,"%%%",departure) end) as name FROM createorder) as a GROUP BY name ORDER BY num DESC LIMIT 5) as t2 on t1.name=t2.name )as t where rank=1 order by count desc;
- 将tt表中结果插入到orderline表中
insert into orderline select * from tt;
- 另开命令行进入mysql命令行
mysql -h 127.0.0.1 -uroot -p123123
- 在mysql数据库中创建orderline表
create table orderline(departure varchar(255),deplongitude varchar(255),deplatitude varchar(255),destination varchar(255),destlongitude varchar(255),destlatitude varchar(255),num int);
- 退出mysql
exit
- 最后一步,将hive数据库中的orderline表数据导出到mysql数据库的orderline中
sqoop export --connect jdbc:mysql://127.0.0.1:3306/trafficdata --username root --password 123123 --export-dir '/opt/hive/warehouse/trafficdata.db/orderline' --table orderline --fields-terminated-by '\t'
提交执行
过啦!!!
如有错误,欢迎大家指出纠正*