新浪微薄数据分析系统开发实战
用户历史微博数据都是JSON格式的,有许多的键值对表示的对象信息,下面我们就对对象包含的字段做一个详细的说明:
beCommentWeibold:是否评论
beForwardWeibold:是否转发微博
catchTime:抓取时间
commentCount:评论次数
content:微博内容
info1:预留字段
info2:预留字段
info3:预留字段
mlevel:音乐评级
musicurl:音乐链接
pic_list:照片列表
praiseCount:点赞人数
reportCount:转发人数
source:数据来源
userId:用户id
videout:视频链接
weibold:微博id
weiboUrl:微博网址
下面是json数据的展示:
导入到Hive里面查询后是这样的:
步骤如下:
1.数据存储
1.构建数据仓库
2.创建原始数据表
3.加载数据到数据仓库
4.验证数据结果
2.统计分析
3.特殊需求
4.数据ETL
创建数据库
create database weibo;
使用数据库;
use weibo;
2.创建原始数据表
create external table weibo(json string) location '/weibo';
2.1/Hive里面上传数据文件
先创建一个目录文件存储json数据文件:
mkdir /hivedata/weibo100;
后面使用winscp工具上传,因为数据较大,使用rz会出错
3.加载数据到数据仓库
load data local inpath '/hivedata/weibo100' into table weibo;
或者
dfs -put /hivedata/weibo100 /weibo;
验证数据
查看前2条数据
select*from weibo limit 2;
查看数据总条数
select count(*) from weibo;
统计独立用户数:
select count(distinct(get_json_object(a.j,'$.userId')))from(
select substr(json,2,length(json)-2) as j from weibo) a ;
统计json文本字段
select get_json_object(a.j,'$.userId'),
get_json_object(a.j,'$.content')
from(
select substr(json,2,length(json)-2) as j from weibo) a limt 3;
统计用户所有微博被转发的总次数,并输出TOP-3用户
select b.id ,sum(b.cnt) as bsum from (
select get_json_object(a.j,'$.userId') as
id,get_json_object(a.j,'$.reportCount') as cnt
from (select substring(json,2,length(json)-2)as j
from weibo )a )b
group by b.id order by bsum desc limit 3;
统计被转发次数最多的TOP-3微博,输出用户id
select get_json_object(a.j,'$.userId') as id,cast(get_json_object(a.j,'$.reportCount') AS INT ) as cnt from (select substring(json,2,length(json)-2) as j from weibo) a order by cnt desc limit 3;
统计每个用户的发送微博总数,并存储到临时表
创建一个临时表,存储每个用户发布的微博总数,设计了2个字段,一个是用户userId,一个是用户微博总数wbcnt
create table weibo_uid_wbcnt(userid string,wbcnt int) row format delimited fields terminated by '\t' stored as textfile;
根据用户分组,同一个用户发布的微博都会进入同一个组,然后我们对组内的微博条数进行ocunt运算可以统计出每个用户发布的微博总数,再使用insert overwrite table命令将统计出来的结果重写到临时表中
insert overwrite table weibo_uid_wbcnt select get_json_object(a.j,'$.userId'),count(1) from(select substring(json,2,length(json)-2) as j from weibo) a group by get_json_object(a.j,'$.userId');
查看临时表weibo_uid_wbcnt的前20天数据进行验证
select*from weibo_uid_wbcnt limit 20;
统计带图片的微博数‘
select count(*) from(select substring(json,2,length(json)-2) as j from weibo) a where
get_json_object(a.j,'$.pic_list') like '%http%';
统计使用iPhone发微博的独立用户数
思路:我们首先根据微博数据来源字段source过滤出其中包含"iPhone"的微博数据,然后再通过关键字distinct对用户userId进行去重,就可以得到使用IPhone发微博的独立用户数,
select count(distinct get_json_object(a.j,'$.userId') )from(select substring(json,2,length(json)-2) as j from weibo) a where lower(get_json_object(a.j,'$.source')) like '%iphone%';
统计微博中评论次数小于1000的用户ID与数据来源信息,将其放入视图,然后统计视图中数据来源是’iPad’客户端的用户目录
create view weibo_view as select get_json_object(a.j,'$.userId') as uid,get_json_object(a.j,'$.source')
as source from (select substr(json,2,length(json)-2) as j from weibo) a where get_json_object(a.j,'$.commentCount')<1000;
查看视图前10条数据
select*from weibo_view limit 10;
统计视图中数据来源是’iPad’客户端的用户目录
select count(distinct uid) as cnt from weibo_view where source='iPad客户端';
Hive的UDF应用一:
需求:将微博的点赞人数与转发人数相加求和,并且相加之和降序排列,取前10条记录
我们根据需求分析知道这个我们要采用Hive的用户的自定义函数UDF来实现:
package Hive;
import org.apache.hadoop.hive.ql.exec.UDF;
//继承UDF
public class UdfTest extends UDF{
//重写evaluate()方法,num1代表点赞人数,num2代表转发人数
public Integer evaluate(Integer num1,Integer num2){
try{
return num2+num1;
}catch(Exception e){
return 0;
}
}
//测试方法是否正确
public static void main(String args[]){
UdfTest ss=new UdfTest();
ss.evaluate(10, 20);
System.out.println(ss.evaluate(10, 20));
}
}
第二步我们要将该代码在EclipseIDE中并且通过一个项目来管理,并且导出,打包
第一步:我们点击到我们java project下创建的包下,点击鼠标右键出现界面,
找到Export这个,鼠标左键点击
第一步我们应该切换到自己要放打包好的jar的目录下:
cd /hivedata
添加架包:
add jar /hivedata/udf.jar(自己命名的jar的名字)
创建临时函数
create temporary function wb as "Hive.UdfTest";
查看函数
show function
接下来我们就用这个函数来实现这个需求分析的:
select wb(cast(get_json_object(a.j,'$.praiseCount') as int),
cast(get_json_object(a.j,'$.praiseCount') as int)) as cnt from
(select substr(json,2,length(json)-2) as j from weibo) a order by cnt desc limit 10;
Hive的UDF应用二:
需求:微博内容content中包含某个词的个数,方法返回值是int类型的数值,
我们根据需求分析知道这个我们要采用Hive的用户的自定义函数UDF来实现:
package Hive;
import org.apache.hadoop.hive.ql.exec.UDF;
public class Wcount extends UDF{
//继承evaluate方法
public int evaluate(String content,String word){
//定义次型变量
int count=0;
//字符串判断
if(content!=null&&content.length()>0){
String[] arrary=content.split(word);
count=arrary.length-1;
}
return count;
}
//测试方法是否正确
public static void main(String args[]){
Wcount ss=new Wcount();
System.out.println(ss.evaluate("打包上是一个好的思想的思想,思想要正确", "思想"));
}
}
第二步我们要将该代码在EclipseIDE中并且通过一个项目来管理,并且导出,打包
第一步:我们点击到我们java project下创建的包下,点击鼠标右键出现界面,
找到Export这个,鼠标左键点击
第一步我们应该切换到自己要放打包好的jar的目录下:
cd /hivedata
添加架包:
add jar /hivedata/.jar(自己命名的jar的名字)
创建临时函数
create temporary function wcount as "Hive.Wcount";
查看函数
show function
接下来我们就用这个函数来实现这个需求分析的:
select b.id,max(b.cnt) as cn from(select get_json_object(a.j,'$.userId') as id,wcount(get_json_object(a.j,'$.content'),'iphone') as cnt from(select substr(json,2,length(json)-2) as j from weibo) a) b group by b.id order by cn desc limit 10;
数据ELT
登录数据库中执行一下命令
mysql -uhadoop -phadoop
show databases;
创建表的SQL语句如下:
use test;
create table test.weibo(userId varchar(255),wbcnt int);
show tables;
下面我们可以使用Sqoop大数据平台的ETL工具将Hive数据仓库处理完的结果加载到关系数据MySQL中,具体的执行脚本程序设计如下:
进入sqoop的安装目录
cd sqoop-1.4.5.bin_hadoop-2.0.4-alpha
bin/sqoop export --connect "jabc:mysql://master:3306/test" --username hadoop --password hadoop --table weibo --export-dir /user/hive/warehouse/user_weibo.db/weibo_uid_wbcnt/
--input-fields-terminated-by '\t';
select*from weibo limit 10;
在数据库查询:
select count(*) from weibo;