ARTICLE DETAIL

资讯详情

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

头歌实践教学平台:大数据存储2023(三~四)

头歌实践教学平台:大数据存储2023(三~四) 三、Hive综合应用案例 — 用户搜索日志分析第1关2018年点击量最高的10个网站域名任务描述本关任务分析2018年点击量最高的10个网站域名。编程要求在右侧编辑器补充代码分析出2018年点击量最高的10个网站域名。创建数据库mydb创建原始表db_search字段名 类型 注释id string 用户编号key string 搜索关键词ranking int 该URL在返回结果中的排名or_der int 点击顺序url string 域名time string 时间部分数据如下数据切分方式空格数据所在位置/root/data.txt测试说明平台会对你编写的代码进行测试预期输出bbs.union.daqi.com 1822www.qihoo.com 1714ent.sina.com.cn 937bbs.phoenixtv.com 857yule.sohu.com 827club.chinaren.com 827click.cpc.sogou.com 807blog.sohu.com 737news.sina.com.cn 649club.yule.sohu.com 627开始你的任务吧祝你成功---------- 禁止修改 ----------drop database if exists mydb cascade;---------- 禁止修改 -------------------- begin -------------创建mydb数据库create database if not exists mydb;---使用mydb数据库use mydb;---创建表db_searchcreate table if not exists db_search(id string,key string,ranking int,or_der int,url string,time string)row format delimitedfields terminated by stored as textfile;---导入数据/root/data.txtload data local inpath /root/data.txt into table db_search;--查询2018年点击量最多的10个网站域名select url,count(*) as cntfrom db_searchwhere time like 2018%group by urlorder by cnt desclimit 10;---------- end ----------第2关同一种搜索词哪个网站域名被用户访问最多任务描述本关任务分析同一种搜索词哪个网站域名被用户访问最多并根据访问次数降序取前十。编程要求在右侧编辑器补充代码分析同一种搜索词哪个网站域名被用户访问最多并根据访问次数降序取前十。创建数据库mydb创建原始表db_search字段名 类型 注释id string 用户编号key string 搜索关键词ranking int 该URL在返回结果中的排名or_der int 点击顺序url string 域名time string 时间部分数据如下数据切分方式空格数据所在位置/root/data.txt测试说明平台会对你编写的代码进行测试预期输出[陋俗] www.qihoo.com 5529[周恩来] bbs.phoenixtv.com 2050[女艺人] bbs.union.daqi.com 1662[北京的GAY吧] love.86gay.com 1217[张玉凤] bbs.union.daqi.com 1171[百度] www.baidu.com 1076[林彪] bbs.phoenixtv.com 885[明星] club.yule.sohu.com 864[《十景缎》] book.haahoo.com 638[富婆] bbs.enet.com.cn 591开始你的任务吧祝你成功---------- 禁止修改 ----------drop database if exists mydb cascade;---------- 禁止修改 -------------------- begin -------------创建mydb数据库create database if not exists mydb;---使用mydb数据库use mydb;---创建表db_searchcreate table if not exists db_search(id string,key string,ranking int,or_der int,url string,time string)row format delimitedfields terminated by stored as textfile;---导入数据load data local inpath /root/data.txt into table db_search;-- 1) 先统计每个关键词域名的访问次数with key_url_cnt as (selectkey,url,count(*) as cntfrom db_searchgroup by key, url),-- 2) 每个关键词内按次数排序取每组第1名ranked as (selectkey,url,cnt,row_number() over(partition by key order by cnt desc) as rnfrom key_url_cnt)-- 3) 只保留每组rn1再整体取前十selectconcat(, key, ) as key_tag,url,cntfrom rankedwhere rn 1order by cnt desclimit 10;---------- end ----------第3关每月最火的搜索词任务描述本关任务分析出每年每月哪个搜索词被搜索次数最多。编程要求根据提示在右侧编辑器补充代码分析出每年每月哪个搜索词被搜索次数最多。创建数据库mydb创建原始表db_search字段名 类型 注释id string 用户编号key string 搜索关键词ranking int 该URL在返回结果中的排名or_der int 点击顺序url string 域名time string 时间部分数据如下数据切分方式空格数据所在位置/root/data.txt测试说明平台会对你编写的代码进行测试预期输出2016-1 [陋俗] 712016-2 [陋俗] 2022016-3 [陋俗] 1942016-4 [陋俗] 1812016-5 [陋俗] 1972016-6 [陋俗] 2102016-7 [陋俗] 2022016-8 [陋俗] 2062016-9 [陋俗] 1522016-10 [陋俗] 1932016-11 [陋俗] 1762016-12 [陋俗] 1912017-1 [陋俗] 1952017-2 [陋俗] 1932017-3 [陋俗] 2022017-4 [陋俗] 2022017-5 [陋俗] 1892017-6 [陋俗] 2082017-7 [陋俗] 1862017-8 [陋俗] 2012017-9 [陋俗] 2022017-10 [陋俗] 2112017-11 [陋俗] 1922017-12 [陋俗] 1962018-1 [陋俗] 1922018-2 [陋俗] 1702018-3 [陋俗] 2212018-4 [陋俗] 1902018-5 [陋俗] 1922018-6 [陋俗] 1932018-7 [陋俗] 1932018-8 [陋俗] 1942018-9 [陋俗] 1752018-10 [陋俗] 1892018-11 [陋俗] 2072018-12 [陋俗] 2032019-1 [陋俗] 1762019-2 [陋俗] 1722019-3 [陋俗] 2112019-4 [陋俗] 1742019-5 [陋俗] 1882019-6 [陋俗] 1932019-7 [陋俗] 1952019-8 [陋俗] 2052019-9 [陋俗] 118开始你的任务吧祝你成功---------- 禁止修改 ----------drop database if exists mydb cascade;---------- 禁止修改 -------------------- begin -------------创建mydb数据库create database mydb;---使用mydb数据库use mydb;---创建表db_searchcreate table db_search(id int,key1 string,ranking int,or_der int,url string,time1 string)row format delimited fields terminated by ;---导入数据/root/data.txtload data local inpath /root/data.txt into table db_search;--分析每年每月哪个搜索词被搜索次数最多。select concat(t.y1,-,t.m),t.key1,t.cntfrom(select year(time1) y1,month(time1) m,key1,count(*) cnt,row_number() over (partition by year(time1),month(time1) order by count(*) desc) rkfrom db_search group by year(time1),month(time1),key1) twhere t.rk1;---------- end ----------四、HBase编程HBase计数器第1关HBase计数器任务描述本关任务编写一个 HBase 计数器。相关知识HBase 有一个高级功能计数器counter)。许多收集统计信息的应用有点击流或在线广告意见这些应用需要被收集到日志文件中用于后续的分析。用户可以使用计数器做实时统计从而放弃延时较高的批量处理操作。计数器简介与之前介绍的原子操作检查并修改check-and-modify)一样HBase 也有一种机制可以将列当作计数器。否则如果用户需要对一行数据加锁然后读取数据再对当前数据做加法最后写回 HBase 并释放该行锁从而其他写程序可以访问该行数据。这样做会引起大量的资源竞争问题尤其是当客户端进程崩溃之后尚未释放的锁需要等待超时恢复——这会在一个高负载的系统中引起灾难性的后果。客户端 API 提供了专门的方法来完成这种读取并修改read-and-modify)操作同时在单独一次客户端的调用过程中保证原子性。早期的 HBase 版本只会在每次计数器更新操作中使用一个 RPC 请求不过新版本的 HBase 中 CRUD 操作开始使用与此相同的机制让许多更新计数器的请求都可以在一次 RPC 中完成但是多个计数器必须在同一行。在Shell中创建并操作计数器计数器使用 incr 命令增量可以是正数也可以是负数但是必须是长整数 Longincr table,rowKey,column,[increment-value]计数器不需要初始化第一次使用计数器时计数器被自动设置为 0。比如我创建的表名为 test列族分别是 info、score 的表create test,info,score现在我想在 info 这个列族上操作 hits 计数器使它的值在原来基础上加 1(初始默认为 0)incr test,001,info:hits,1使用如下命令可以查看一个计数器的值get_counter table rowKey column比如我想读取‘info:hits’计数器的值get_counter test,001,info:hits值得注意的是在操作计数器的时候只有一种操作那就是加法操作如果你想让一个计数器的值减 1则 ‘increment-value’ 这个参数为 -1 即可具体规则如下值 作用比零大的值 按给定的值增加计数器的值零 得到计数器当前的值不增也不减比零小的值 按给定的值减少计数器的值单列增加在使用计数器时绝大多数情况下是通过 Java API 来使用现来介绍单计数器。单计数器顾名思义就是一次操作只能操作一个计数器用户需要自己设置列方法由 HTable 类提供方法签名如下incrementColumnValue(byte[] row,byte[] family, byte[] qualifier,long amount)incrementColumnValue(byte[] row,byte[] family, byte[] qualifier,long amount,boolean writeToWAL)incrementColumnValue(byte[] row,byte[] family, byte[] qualifier,long amount,Durability durability)。这三个函数都是直接对表中的某一行数据进行添加不过后两个函数定义了是否将数据写入到预写日志文件的模式这三个函数都返回进行增加后的计数器的值。Configuration config HBaseConfiguration.create();config.set(hbase.zookeeper.quorum, 127.0.0.1);// zookeeper地址config.set(hbase.zookeeper.property.clientPort, 2181);// zookeeper端口Connection connection ConnectionFactory.createConnection(config);TableName tableName TableName.valueOf(test);Table table connection.getTable(tableName);// 对计数器进行加3操作long cnt1 table.incrementColumnValue(Bytes.toBytes(002), Bytes.toBytes(info), Bytes.toBytes(hits), 3);// 对计数器进行减1操作long cnt2 table.incrementColumnValue(Bytes.toBytes(002), Bytes.toBytes(info), Bytes.toBytes(hits), -1);// 得到当前计数器的值不做增减操作long current table.incrementColumnValue(Bytes.toBytes(002), Bytes.toBytes(info), Bytes.toBytes(hits), 0);多列增加Htable 直接对计数器进行增加的话可能只能增加一行如果对一行中的多个计数器进行增加则需要多次发送 RPC 请求在新版本的 HBase API 结果中提供了对一行中的多个计数器进行增加的APIincrementColumnValue(Increment increment)// 创建Increment实例Increment increment new Increment(Bytes.toBytes(003));// 在info:hits计数器上增加1increment.addColumn(Bytes.toBytes(info), Bytes.toBytes(hits), 1);// 在score:hits计数器上减少10increment.addColumn(Bytes.toBytes(score), Bytes.toBytes(hits), -10);// 操作计数器Result result table.increment(increment);// 打印操作结果for (Cell cell : result.rawCells()) {System.out.println(Cell: cell Value: Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));}Increament 的使用方法和 Put、Get 等方法是相似的并且返回一个 result 对象将整行数据进行返回。Increment 对象也提供了很多方法进行设置1setWriteToWAL(boolean write)是否将该操作写入到预先日志 HLog中2setDurability(Durability d)设置读写日志写入的模式3setReturnResults(boolean returnResults)是否将计数器结果值进行返回。Increment 对象还提供了其他方法这里不再进行详细的解释了可以通过 API 文档进行详细的查看。编程要求根据提示在右侧编辑器的 Begin-End 中补充代码实现 HBase 计数器的功能具体要求如下在 HBase 表 student 上中 rowkey 为 003 的数据添加计数器该表列族为 stuinfogradesclasses在“info:hits”计数器上增加 10在“grades:hits”计数器上减 3在“classes:hits”计数器上增加 4最后用一个 result 对象将整行数据进行返回。测试说明注意点击测评之前请先开启Hadoopstart-dfs.sh和HBasestart-hbase.sh。开始你的任务吧祝你成功package step1;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;public class Task {public Result counter(Connection connection, TableName tableName) throws IOException {/********* Begin *********/// 获取表Table table connection.getTable(tableName);// 创建Increment实例rowkey为003Increment increment new Increment(Bytes.toBytes(003));// 在stuinfo:hits计数器上增加10increment.addColumn(Bytes.toBytes(stuinfo), Bytes.toBytes(hits), 10);// 在grades:hits计数器上减少3increment.addColumn(Bytes.toBytes(grades), Bytes.toBytes(hits), -3);// 在classes:hits计数器上增加4increment.addColumn(Bytes.toBytes(classes), Bytes.toBytes(hits), 4);// 执行自增操作获取结果Result result table.increment(increment);table.close();return result;/********* End *********/}}有任何问题都可以随时关注私信
返回列表