Hive微博案例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/a805814077/article/details/102635788

数据下载链接:
https://pan.baidu.com/s/1OGyO2jFj393-Dcq3eosbjA&shfl=sharepset
提取码:jtdi

数据案例(取其中两个文件即可):

[{"beCommentWeiboId":"","beForwardWeiboId":"","catchTime":"1387157643","commentCount":"682","content":"喂!2014。。。2014!喂。。。","createTime":"1387086483","info1":"","info2":"","info3":"","mlevel":"","musicurl":[],"pic_list":["http://ww1.sinaimg.cn/square/47119b17jw1ebkc9b07x9j218g0xcair.jpg","http://ww4.sinaimg.cn/square/47119b17jw1ebkc9ebakij218g0xc113.jpg","http://ww2.sinaimg.cn/square/47119b17jw1ebkc9hml7dj218g0xcgt6.jpg","http://ww3.sinaimg.cn/square/47119b17jw1ebkc9kyakyj218g0xcqb3.jpg"],"praiseCount":"1122","reportCount":"671","source":"iPhone客户端","userId":"1192336151","videourl":[],"weiboId":"3655768039404271","weiboUrl":"http://weibo.com/1192336151/AnoMrDstN"}]

字段描述:

总共19个字段
beCommentWeiboId  是否评论
beForwardWeiboId 是否是转发微博
catchTime 抓取时间
commentCount 评论次数
content	内容
createTime 创建时间
info1 信息字段1
info2信息字段2
info3信息字段3
mlevel   no sure
musicurl	音乐链接
pic_list	照片列表(可以有多个)
praiseCount	点赞人数
reportCount	转发人数
source	数据来源
userId	用户id
videourl	视频链接	
weiboId	微博id
weiboUrl	微博网址

功能需求:

  • 建表的时候,建外部表
  • 数据的存储目录: hdfs://hadoop01:9000/data/weibo

1.创建Hive表weibo_json(json string),表只有一个字段,导入所有数据,并验证查询前5条数据

创建json表

create external table if not exists weibo_json(json string) location "/data/weibo";

加载数据

load data local inpath "/home/hadoop/hive_data/weibo.json" into table weibo_json;

检查数据

select json 
from weibo_json limit 5;

2.解析完weibo_json当中的json格式数据到拥有19个字段的weibo表中,写出必要的SQL语句

创建weibo表

create table if not exists weibo(
 beCommentWeiboId string,
 beForwardWeiboId string,
 catchTime string,
 commentCount int,
 content string,
 createTime string,
 info1 string, 
 info2 string, 
 info3 string,
 mlevel string, 
 musicurl string, 
 pic_list string, 
 praiseCount int,
 reportCount int, 
 source string, 
 userId string, 
 videourl string,
 weiboId string, 
 weiboUrl string 
) row format delimited fields terminated by '\t';

插入数据

insert into table weibo 
select 
get_json_object(json,'$[0].beCommentWeiboId') beCommentWeiboId,
get_json_object(json,'$[0].beForwardWeiboId') beForwardWeiboId,
get_json_object(json,'$[0].catchTime') catchTime,
get_json_object(json,'$[0].commentCount') commentCount,
get_json_object(json,'$[0].content') content, 
get_json_object(json,'$[0].createTime') createTime,
get_json_object(json,'$[0].info1') info1, 
get_json_object(json,'$[0].info2') info2,
get_json_object(json,'$[0].info3') info3,
get_json_object(json,'$[0].mlevel') mlevel,
get_json_object(json,'$[0].musicurl') musicurl,
get_json_object(json,'$[0].pic_list') pic_list,
get_json_object(json,'$[0].praiseCount') praiseCount,
get_json_object(json,'$[0].reportCount') reportCount,
get_json_object(json,'$[0].source') source,
get_json_object(json,'$[0].userId') userId,
get_json_object(json,'$[0].videourl') videourl,
get_json_object(json,'$[0].weiboId') weiboId,
get_json_object(json,'$[0].weiboUrl') weiboUrl
from weibo_json;

3.统计微博总量和独立用户数

涉及字段

  • weiboId 微博id
  • userId 用户id

独立用户数就是对useId进行去重

select count(weiboId)c1,count(distinct userId)c2 
from weibo;

4.统计用户所有微博被转发的次数之和,输出top5用户,并给出次数

涉及字段

  • reportCount 转发人数
  • userId 用户id

这里使用sum函数来求次数之和,并根据结果进行排序,倒序取前五个

select 
sum(reportCount) sums 
from weibo 
group by userId order by sums desc limit 5;

5.统计带图片的微博数

涉及字段

  • weiboId 微博id
  • pic_list 照片列表

带图片的微博就是pic_list字段中带http的,用instr来判断是否包含

select	count(weiboId) sum 
from weibo 
where instr(pic_list,'http')>0;

6.统计使用iphone发微博的独立用户数

涉及字段

  • userId 用户id
  • source 数据来源

与第五题类似

select count(distinct userId)c1 
from weibo 
where instr(source,'iPhone客户端')>0;

7.将微博的点赞人数和转发人数相加求和,并将相加之和降序排列,取前10条记录,输出userid和总次数

涉及字段

  • userId 用户id
  • praiseCount 点赞人数
  • reportCount 转发人数

先按userId排序再按和的结果倒序

select userid,count(praiseCount)+count(reportCount) sums 
from weibo 
group by userId 
order by sums desc limit 10;

8.统计微博中评论次数小于1000的用户ID与数据来源信息,将其放入视图,然后统计视图中数据来源是”ipad客户端”的用户数目

涉及字段

  • userId 用户id
  • commentCount 评论次数
  • source 数据来源

按题目要求使用视图,后面与第五题类似

create view weibo_view 
as select userId,source 
from weibo 
where commentCount<1000;


select count(userId) c 
from weibo_view 
where instr(source,'iPad客户端')>0;

9.使用自定义函数统计微博内容中出现”iphone”次数最多的用户,最终结果输出用户id和次数(注意:该次数是”iphone”的出现次数,不是出现”iphone”的微博数目)

涉及字段

  • userId 用户id
  • content 内容

自定义类

public class MyUDF extends UDF {
	public static int evaluate(String s) {
        int count=0;//计数器
        int index=0;//下标
        //返回出现iphone字段第一次出现的下标,如果为-1说明没有iphone字段了
        while((index=s.toLowerCase.indexOf("iphone",index))!=-1){
           count++;
           index+="iphone".length();//下次从e的后一个字段开始循环
        }
        return count;
    }
			
	/*public static void main(String[] args) {
		String s = "iphone1212iphone421iphone";
		System.out.println(evaluate(s));
	}*/

将这个类打成jar包并添加到linux上

add jar /home/hadoop/hive_data/test.jar;

在hive中创建一个临时函数,注意这里为全类名

create temporary function mysum as "udf.MyUDF";

直接使用即可

select userId,mysum(content)c 
from weibo 
order by c desc limit 1;

10.求每天发微博次数最多的那个家伙的ID和发微博的条数

涉及字段

  • userId 用户id
  • createTime 创建时间
  • weiboId 微博id

分析题目可以知道,题目是要求我们先按useId分组,然后再按天分组

由于要排序所以我们这里要将时间转成bigint类型

select 
userId,from_unixtime(cast(createTime as bigint),"yyyy-MM-dd") day,count(weiboId) totalCount 
from weibo 
group by userId,from_unixtime(cast(createTime as bigint),"yyyy-MM-dd") 
order by totalCount desc limit 1;

11.求出所有被多次引用(同一张照片出现在多条微博中,超过1条就算多条)的照片的数目

涉及字段

  • weiboId 微博id
  • pic_list 照片列表

在这里插入图片描述

我们可以发现pic_list字段中,有的为[]空,有的用","分割表示多条,所以这里我们要使用炸裂函数将多个字段炸裂开来

字段解释

(substr(pic_list,2,length(pic_list)-2) 
 表示切割pic_list,从第2个下标(h)开始切,这条url的最后一个字段,也就是拿出url
(split(substr(pic_list,2,length(pic_list)-2),",")
 表示以,分割多条url
explode(split(substr(pic_list,2,length(pic_list)-2),","))
将数组中的多个字段炸裂开来成为单个字段

创建一个临时表来存储weiboId(关联键),url

create table pic_temp as 
select 
weiboId,ps.pic_url pic_url 
from weibo lateral view explode(split(substr(pic_list,2,length(pic_list)-2),",")) ps as pic_url where length(pic_list)>2;

统计url的个数,并找出被多次引用的照片

select 
count(*) totalCount 
from 
(select 
pic_url,count(distinct weiboId) totalCount 
from pic_temp 
group by pic_url 
having totalCount>1) a;

猜你喜欢

转载自blog.csdn.net/a805814077/article/details/102635788