将数据从Hive导入到MySQL实验报告

将数据从Hive导入到MySQL实验报告

实验要求

用户行为日志user_log.csv,日志中的字段定义如下:

  1. user_id | 买家id
  2. item_id | 商品id
  3. cat_id | 商品类别id
  4. merchant_id | 卖家id
  5. brand_id | 品牌id
  6. month | 交易时间:月
  7. day | 交易事件:日
  8. action | 行为,取值范围{0,1,2,3},0表示点击,1表示加入购物车,2表示购买,3表示关注商品
  9. age_range | 买家年龄分段:1表示年龄<18,2表示年龄在[18,24],3表示年龄在[25,29],4表示年龄在[30,34],5表示年龄在[35,39],6表示年龄在[40,49],7和8表示年龄>=50,0和NULL则表示未知
  10. gender | 性别:0表示女性,1表示男性,2和NULL表示未知
  11. province| 收货地址省份

Hive操作

(1)在hive中创建临时表inner_user_log ,字段包括上面11个字段,使用逗号进行分隔符

(2)在hive中将user_log表中数据插入到临时表中

(3)查询hive中user_log中倒数二十行的数据

(4)在mysql中创建dbtaobao11的数据库作为存放库

(5)查看mysql数据库的编码格式除了filesystem其他均为utf8

(6)在mysql的dbtaobao11数据库中创建表user_log,设置编码为utf8,使用反引号 如:`user_log`

(7)使用SQOOP进行导入操作 将hive_user_log表中数据导入mysql的user_log中

./bin/sqoop export #表示数据从 hive 复制到 mysql 中
--connect jdbc:mysql://localhost:3306/dbtaobao11
--username root #mysql登陆用户名
--password Admin123. #登录密码
--table user_log #mysql 中的表,即将被导入的表名称
--export-dir '/user/hive/warehouse/dbtaobao.db/inner_user_log' #hive 中被导出的文件
--fields-terminated-by ',' #Hive 中被导出的文件字段的分隔符

(8)查看mysql的user_log的前10行

实验步骤

(1)在hive中创建临时表inner_user_log ,字段包括上面11个字段,使用逗号进行分隔符

create table dbtaobao.inner_user_log(user_id int,item_id int,cat_id int,merchant_id int,brand_id int,month string,day string,action int,age_range int,gender int,province string) comment 'Welcome to Alex dblab, now create dbtaobao.inner_user_log!' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' stored as textfile;

(2)在hive中将user_log表中数据插入到临时表中

insert overwrite table inner_user_log select * from user_log;

(3)查询hive中user_log中倒数二十行的数据

select * from inner_user_log order by user_id limit 20;

image-20220705162226977

(4)在mysql中创建dbtaobao11的数据库作为存放库

create database dbtaobao11;
show databases;
image-20220705152856528

(5)查看mysql数据库的编码格式除了filesystem其他均为utf8

show variables like "char%";
image-20220705153015379

(6)在mysql的dbtaobao11数据库中创建表user_log,设置编码为utf8,使用反引号 如:`user_log`

CREATE TABLE `dbtaobao11`.`user_log`(`user_id` varchar(20),`item_id` varchar(20),`cat_id` varchar(20),`merchant_id` varchar(20),`brand_id` varchar(20),`month` varchar(6),`day` varchar(6),`action` varchar(6),`age_range` varchar(20),`gender` varchar(6),`province` varchar(10)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
use dbtaobao11;
show tables;
image-20220705153215354

(7)使用SQOOP进行导入操作 将hive_user_log表中数据导入mysql的user_log中

bin/sqoop export --connect jdbc:mysql://192.168.85.100:3306/dbtaobao11 --username root --password Admin123. --table user_log --export-dir '/user/hive/warehouse/dbtaobao.db/inner_user_log' --fields-terminated-by ','
image-20220705155209972

(8)查看mysql的user_log的前10行

select * from user_log limit 10;

image-20220706091629420

  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值