第7关:查询成功订单线路中出行次数最多的五条线路

本关任务

  • 基于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'

提交执行

过啦!!!
在这里插入图片描述

如有错误,欢迎大家指出纠正*

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值