文章目录
- 将数据从Hive导入到MySQL实验报告
- 实验要求
- 实验步骤
- (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中
- (8)查看mysql的user_log的前10行
将数据从Hive导入到MySQL实验报告
实验要求
用户行为日志user_log.csv,日志中的字段定义如下:
- user_id | 买家id
- item_id | 商品id
- cat_id | 商品类别id
- merchant_id | 卖家id
- brand_id | 品牌id
- month | 交易时间:月
- day | 交易事件:日
- action | 行为,取值范围{0,1,2,3},0表示点击,1表示加入购物车,2表示购买,3表示关注商品
- age_range | 买家年龄分段:1表示年龄<18,2表示年龄在[18,24],3表示年龄在[25,29],4表示年龄在[30,34],5表示年龄在[35,39],6表示年龄在[40,49],7和8表示年龄>=50,0和NULL则表示未知
- gender | 性别:0表示女性,1表示男性,2和NULL表示未知
- 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;
(4)在mysql中创建dbtaobao11的数据库作为存放库
create database dbtaobao11;
show databases;
![image-20220705152856528](https://s2.loli.net/2022/07/05/WwujItF5dvp36AU.png)
(5)查看mysql数据库的编码格式除了filesystem其他均为utf8
show variables like "char%";
![image-20220705153015379](https://s2.loli.net/2022/07/05/4VK2LtkFP9U8G3S.png)
(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](https://s2.loli.net/2022/07/05/zTkC6OVSxIDsG5b.png)
(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](https://s2.loli.net/2022/07/05/JwAnWaGgfUk6i4u.png)
(8)查看mysql的user_log的前10行
select * from user_log limit 10;