Hive表-----数据清洗
创始人
2024-05-29 19:13:36
0

以下内容所需要的环境 :hive 、beeline、Zeppelin(可视化界面如何操作表格)

一、准备表格

1、上传csv表格至linux目录中

百度网盘自取:链接:https://pan.baidu.com/s/1xd5MdXiBDLBUtP07kpgl5Q?pwd=2ema 
提取码:2ema

2.、登录Zeppelin 

启动命令:zeppelin-daemon.sh start

2.1、创建一个新的New Note ,使用命令创建hdfs对应表格文件夹

2.2、将表格上传到hdfs对应文件夹内

 3、创建数据库,创建表格

小技巧:可以通过head命令查看文件表格的表头,便于创建表格元数据 

tblproperties (“skip.header.line.count”=“1”) 是设置在读取文件插入数据时跳过文件的第一行

tblproperties ("skip.footer.line.count"="2") t跳过行尾两行

[root@reagan180 storetransaction]# head -n 1 ./customer_details.csv 
customer_id,first_name,last_name,email,gender,address,country,language,job,credit_type,credit_noroot@reagan180 storetransaction]# head -n 1 ./transaction_details.csv 
transaction_id,customer_id,store_id,price,product,date,time

创建 ext_customer_details表

create external table if not exists ext_customer_details(
customer_id string,
first_name string,
last_name string,
email string,
gender string,
address string,
country string,
language string,
job string,
credit_type string,
credit_no string )
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
location '/shopping/data/customer'
tblproperties('skip.header.line.count'='1');

创建 ext_transaction_details表

create external table if not exists ext_transaction_details
(transaction_id string,customer_id    string,store_id       string,price          decimal(8, 2),product        string,purchase_date  string,purchase_time  string) row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'location '/shopping/data/transaction'tblproperties ('skip.header.line.count' = '1');

创建 ext_store_details表

create external table if not exists ext_store_details(
store_id string,
store_name string,
employee_number int
) row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'location '/shopping/data/store'tblproperties ('skip.header.line.count' = '1');

创建 ext_store_revie

create external table if not exists ext_store_review(transaction_id string,store_id string,review_score int
) row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'location '/shopping/data/review'tblproperties ('skip.header.line.count' = '1');

二、数据清洗

2.1 、敏感词一次加密和二次加密(可以采用试图创建另一个表)

drop view if exists vw_customer_details;
create view if not exists vw_customer_details as
select 
customer_id,first_name,unbase64(last_name) as last_name,
unbase64(email) as email, gender,unbase64(address) as address,
country,job,credit_type,
unbase64(concat(unbase64(credit_no),'hello')) as credit_no
from  ext_customer_details;

加密前:

 

加密后:

2.2、 对ext_transaction_details表中的重复数据生成新ID

with
basetb as (select  row_number()over(partition by transaction_id) as rn,transaction_id,customer_id,store_id,price,product,purchase_date,purchase_time,substr(purchase_date,0,7) purchase_month from ext_transaction_details),
basetb2 as (select if(rn=1,transaction_id,concat(transaction_id,'_fix',rn)) transaction_id ,customer_id,store_id,price,product,purchase_date,purchase_time,purchase_month from basetb)
select * from basetb2 where transaction_id like '%fix%' limit 100;

解析:主要依靠 窗口函数的排名函数并分组 和 if 语句

row_number()over(partition by transaction_id) as rnif(rn=1,transaction_id,concat(transaction_id,'_fix',rn))

 if 语句:如果排名为 1,为真则使用原来id,不为真则使用id+fix+排名;

 2.3、 过滤掉store_review中没有评分的数据

create view if not exists vm_store_review as
select * from ext_store_review where review_score <> '';

2.4 、可以把清洗好的数据放到另一个表或者用View表示

2.5、重新组织transaction数据按照日期YYYY-MM做分区

相关内容

热门资讯

股价走高触发强赎 7月将有两只... 近日,银行正股股价表现强势,多只银行转债触发强制赎回条款。7月1日是杭银转债最后一个交易日,其最后转...
300548,“改名”,股价历... 科技股和顺周期板块再现“跷跷板”走势。今天上午,顺周期板块走强,银行、有色金属、白酒、新能源等板块上...
明阳电路在昆山投资成立集成电路... 人民财讯7月2日电,企查查APP显示,近日,昆山华芯微测集成电路有限公司成立,法定代表人为窦旭才,经...
民生银行“易创E贷”获“202... 在近日举行的第七届数字普惠金融大会上,民生银行“易创E贷”产品凭借在服务科技型中小微企业的创新与实践...
机器人大军逼近,很快,亚马逊的...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! 亚马逊正迅速接近仓库...
广东省教育考试院通报:不存在中... 7月2日,广东省教育考试院发布通报称,7月1日下午广东省初中学业水平考试(以下简称“中考”)数学科目...
国际油价承压 中东油企拟放缓全... 财联社7月2日讯(编辑 秦嘉禾)在国际油价下行压力加剧背景下,中东两大国有能源巨头——沙特阿美(Sa...
光伏50ETF、光伏龙头ETF... 光伏设备板块走强,亿晶光电、欧晶科技涨停。银华光伏50ETF、汇添富光伏龙头ETF、浦银安盛光伏龙头...
世界银行发布重磅预测:黄金今年... 错过上半年,别再错过下半年!世界银行继续看好贵金属前景,黄金、白银、铂金或将继续延续强势……世界银行...
洪灝称高分红投资策略应继续有所... 【#洪灝称高分红投资策略应继续有所表现#】中国股市近期一个显著趋势是分红和回购活动显著增加。其中,许...