数据清洗和可视化课程案例

最近完成了一个功能型课堂项目,对数据进行清洗,并传入到hive进行二次清洗,再传到mysql保存,并进行可视化。

由于我可视化使用的是finebi(属于是偷了个懒),所以相对比较简单

所以我有很多时间把心思用在数据的清洗上

一、题目

Result文件数据说明:

Ip106.39.41.166,(城市)

Date10/Nov/2016:00:01:02 +0800,(日期)

Day10,(天数)

Traffic: 54 ,(流量)

Type: video,(类型:视频video或文章article

Id: 8701(视频或者文章的id

测试要求:

1、 数据清洗:按照进行数据清洗,并将清洗后的数据导入hive数据库中

两阶段数据清洗:

1)第一阶段:把需要的信息从原始日志中提取出来

ip:    199.30.25.88

time:  10/Nov/2016:00:01:03 +0800

traffic:  62

文章: article/11325

视频: video/3235

2)第二阶段:根据提取出来的信息做精细化操作

ip--->城市 cityIP

date--> time:2016-11-10 00:01:03

day: 10

traffic:62

type:article/video

id:11325

3hive数据库表结构:

create table data(  ip string,  time string , day string, traffic bigint,

type string, id   string )

2数据分析:在HIVE统计下列数据。

1统计最受欢迎的视频/文章的Top10访问次数 (video/article

2按照地市统计最受欢迎的Top10课程 (ip

3按照流量统计最受欢迎的Top10课程 (traffic

3、数据可视化:

将统计结果倒入MySql数据库中,通过图形化展示的方式展现出来。

数据格式清晰,只需将日期和ip进行清洗就可以

由于数据是1217行,相对较少,所以我选择直接使用内存(JAVA)进行数据清洗(这种方式不适合庞大数据量),后续我将实现动态存取。

 

二、java代码

清洗

复制代码
//分割拼接Ip
            String [] tinyIp=table1.getIp().split("\\.");
            String newIp = tinyIp[0]+"."+tinyIp[2];
            //分割拼接时间
            String [] Date0=table1.getDate().split(" ");
            String [] Date1=Date0[0].split(":",2);
            String [] _Date = Date1[0].split("/");

            String newDate =_Date[2]+"-"+"11-"+_Date[0]+" "+Date1[1];
复制代码

清洗之后的数据导出为csv中

 

在hive中依次输入

create table city(
ip string comment "城市"
, c_date string comment "日期"
, c_day string comment "天数"
, traffic bigint comment "流量"
, type string comment "类型"
,Id string comment "id"
)
--指定分隔符为制表符
row format delimited fields terminated by ',';


load data local inpath '/opt/software/data1.csv' overwrite into table city;

create table if not exists tb_ip
comment "ip"
as
select
ip,
type,
id,
count(*) as paixu from city group by ip,type,id
order by paixu desc;

create table if not exists tb_traffic
comment "traffic"
as
select
type,
id,
sum(traffic) as liuliang from city
group by type,id
order by liuliang desc
limit 10;


create table type10(
id String,
total double
)ROW format delimited fields terminated by ',' STORED AS TEXTFILE;

insert into type10 select id, count(*) as total from city group by id order by total desc limit 10;

最后进行可视化

 

 

 

 

posted @   枫浔  阅读(319)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示