1. 程式人生 > >利用hive對微博資料統計分析案例

利用hive對微博資料統計分析案例

資料樣例:

欄位描述

總共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 微博網址

題目

  1. 組織資料
    (建立Hive表weibo_json(json string),表只有一個欄位,匯入所有資料,並驗證查詢前5條資料)
    (解析完weibo_json當中的json格式資料到擁有19個欄位的weibo表中,寫出必要的SQL語句)

  2. 統計微博總量 和 獨立使用者數

  3. 統計使用者所有微博被轉發的次數之和,輸出top5使用者,並給出次數

4.統計帶圖片的微博數

  1. 統計使用iphone發微博的獨立使用者數

  2. 將微博的點贊人數和轉發人數相加求和,並將相加之和降序排列,取前10條記錄,輸出userid和總次數

  3. 統計微博中評論次數小於1000的使用者ID與資料來源資訊,將其放入檢視,然後統計檢視中資料來源是”ipad客戶端”的使用者數目

  4. 統計微博內容中出現”iphone”次數最多的使用者,最終結果輸出使用者id和次數(注意:該次數是”iphone”的出現次數,不是出現”iphone”的微博數目)

  5. 求每天發微博次數最多的那個傢伙的ID和發微博的條數

  6. 求出所有被多次引用(同一張照片出現在多條微博中,超過1條就算多條)的照片的數目

解題

組織資料

// 建立庫:
create database weibo;
use weibo;

// 建立表:
create table weibo_json(json string);

// 匯入資料:
load data local inpath ‘/home/hadoop/weibojson.data.json’ into table weibo_json;

// 驗證:
select json from weibo_json limit 5;

// 建立19個欄位的weibo表:

create table 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;

統計使用者所有微博被轉發的次數之和,輸出top5使用者,並給出次數。注意:一個使用者可能發過多個微博

思路:
1. 以使用者id分組,求轉發和
2. 按照轉發量排序
select sum(reportCount) sumrep
from weibo
group by userId
order by sumrep desc limit 5;

結果
2721667
518676
477742
430532
415424

5、統計帶圖片的微博數(7分)
圖片欄位pic_list
select count(weiboId) total
from weibo
where instr(pic_list,’http’)>0
結果:

5278

統計使用iphone發微博的獨立使用者數

資料來源欄位:source
select count(distinct userId)
from weibo
where instr(lcase(source),’iphone’)>0;

或者使用
select count(distinct userId)
from weibo
where lcase(source) like ‘%iphone%’;

將微博的點贊人數和轉發人數相加求和,並將相加之和降序排列,取前10條記錄,輸出userid和總次數

思路:
以userid分組,統計

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

結果:
14328
620
516
472
428
340
308
226
210
188

統計微博中評論次數小於1000的使用者ID與資料來源資訊,將其放入檢視,然後統計檢視中資料來源是”ipad客戶端”的使用者數目

思路:
1. commentCount<1000
2. select userId,source

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

select count(userId)
from weibo8_view where source like ‘%皮皮%’;

統計微博內容中出現”iphone”次數最多的使用者,最終結果輸出使用者id和次數(注意:該次數是”iphone”的出現次數,不是出現”iphone”的微博數目)

思路

  1. 以iPhone為分隔符使用split切分來源之後轉換為陣列,統計陣列size

  2. 以userid分組最後統計使用者所有出現的次數

create view weibo9_view as
select userId,size(split(lcase(content),’iphone’))-1 total
from weibo where size(split(lcase(content),’iphone’))-1>0;

select userId, sum(total) total
from weibo9_view
group by userId order by total desc limit 1;

1640601392 3
也可以用一條實現
select userId,sum(size(split(lcase(content),’iphone’))-1) total
from weibo
group by userId
order by total desc limit 1;

求每天發微博次數最多的那個傢伙的ID和發微博的條數

求解步驟:

  1. 以每天和userId分組統計每天之中使用者傳送微博數
    create table weibo10 as
    select from_unixtime(cast(createTime as int), ‘yyyy-MM-dd’) dt,userId, count(weiboId) total
    from weibo
    group by from_unixtime(cast(createTime as int), ‘yyyy-MM-dd’),userId;

  2. 使用視窗函式生成以天數為分割槽,以傳送微博數排序的列
    create table weibo10_2 as
    select dt,userId,total,
    row_number() over (distribute by dt sort by tota) as index
    from weibo10;

  3. 查詢出每天傳送微博數排名第一的欄位
    select * from weibo10_2 where index<2;

求出所有被多次引用(同一張照片出現在多條微博中,超過1條就算多條)的照片的數目

思路: 以照片的url分組,統計這個分組下的weiboid數。

難點:
1. pic_list欄位屬於字串型別,但是被[]包括,要先去除這個括號,再把字串按照逗號切分成一個數組。
2. 要把照片列表中多個連結分裂之後,才能分組。

create table weibo11 as
select explode(split(substring(pic_list,2,length(pic_list)-2),’,’)) url
from weibo where pic_list!=’[]’;

select count(*) total
from weibo11
group by url having total >= 2 order by total;