案例实操基础版--加载数据+数据清洗(5W条数据)

我看到了这个

跟着实操一下!

1、加载数据(已经提供了csv文件)

建库建表--->这个比较简单,根据文件的字段名创建合适的表;

create table msg(
    msg_time string comment "消息发送时间",
    sender_name string comment "发送人昵称",
    sender_account string comment "发送人帐号",
    sender_sex string comment "发送人性别",
    sender_ip string comment "发送人ip地址",
    sender_os string comment "发送人操作系统",
    sender_phonetype string comment "发送人手机型号",
    sender_network string comment "发送人网络类型",
    sender_gps string comment "发送人的GPS定位",
    receiver_name string comment "接收人昵称",
    receiver_ip string comment "接收人IP",
    receiver_account string comment "接收人帐号",
    receiver_os string comment "接收人操作系统",
    receiver_phonetype string comment "接收人手机型号",
    receiver_network string comment "接收人网络类型",
    receiver_gps string comment "接收人的GPS定位",
    receiver_sex string comment "接收人性别",
    msg_type string comment "消息类型",
    distance string comment "双方距离",
    message string comment "消息内容"
    );

建表语句有点儿繁琐,注意格式~

desc msg;

然后将文件上传到hdfs中:

hadoop fs -mkdir -p /message/data
hadoop fs -put chat_data-5W.csv /message/data

然后将文件数据加载到表中(从hdfs上传加载):

load data inpath '/message/data/chat_data-5W.csv' into table msg;

(不过个人觉得本地上传更加方便一点~)

对了,记得进行加载数据的验证奥!

2、ETL数据清洗转换

视频中表示有些gps数据为空,数据不合理,需要进行清洗转换成合理的数据;

上面是问题一,那么问题二就是,要根据天、小时等数据字段进行统计,但是在表中并没有明确给出天数和小时标识;

问题三,经纬度不明确,需要获取经纬度构建可视化地图;

故,需要使用ETL进行数据清洗,从而得到我们想要的格式;

有的字段处sender_gps字段内容为空:

数据不合理;

新建一个结构相同的表,存储去除问题一的数据:

create table msg_etl(
    msg_time string comment "消息发送时间",
    sender_name string comment "发送人昵称",
    sender_account string comment "发送人帐号",
    sender_sex string comment "发送人性别",
    sender_ip string comment "发送人ip地址",
    sender_os string comment "发送人操作系统",
    sender_phonetype string comment "发送人手机型号",
    sender_network string comment "发送人网络类型",
    sender_gps string comment "发送人的GPS定位",
    receiver_name string comment "接收人昵称",
    receiver_ip string comment "接收人IP",
    receiver_account string comment "接收人帐号",
    receiver_os string comment "接收人操作系统",
    receiver_phonetype string comment "接收人手机型号",
    receiver_network string comment "接收人网络类型",
    receiver_gps string comment "接收人的GPS定位",
    receiver_sex string comment "接收人性别",
    msg_type string comment "消息类型",
    distance string comment "双方距离",
    message string comment "消息内容"
    );

然后执行语句:

insert overwrite table msg_etl select * from msg where LENGTH(sender_gps)>0;

执行完毕:

数据验证:

有数据!

数据清洗成功!

另外两个就是调用函数date函数、hour函数、还有split函数分割取值的事儿啦,比较简单,就不说了(当然,这个也简单);

posted @ 2023-09-27 18:06  yesyes1  阅读(21)  评论(0编辑  收藏  举报