本篇主题内容是将应用 Sqoop 将 Hive 中的表数据导出到关系型数据库 MySQL 中,方便后续进行数据可视化处理,使抽象的数据转化为图形化表示,便于非技术人员的决策和分析。
首先下载一个SQLyog
链接:https://pan.baidu.com/s/1QXbINcwTPqw59OvgaEb-Ug
提取码:zxc1
SQLyog证书密钥
名称:any
证书秘钥:dd987f34-f358-4894-bd0f-21f3f04be9c1
首先启动MySQL服务
/etc/init.d/mysqld start
下载安装之后打开SQLyog,并填写如下信息:
这样连接好之后,可以在xshell里面进行安装Mysql的节点上访问mysql,也可以选择在SQLSQLyog工具在MySQL中创建JobData数据库。接下来以在xshell里面进行安装Mysql的节点上访问mysql创建JobData数据库为例!
一. 应用Sqoop工具将Hive分析的结果数据导入到Mysql中。
(一) 在安装Mysql的节点上访问mysql
[root@zyc220 bin]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
(二) Mysql中创建数据库 JobData
mysql> create database JobData character set utf8 collate utf8_general_ci;
此处之前在hive仓库建立了JobData,可以直接打开JobData;但是里面没有内容
mysql> use JobData;
(三) Mysql中创建职位所在城市分布表 t_city_count
CREATE TABLE t_city_count(
city varchar(30) DEFAULT NULL,
count int(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(四) Mysql中创建薪资分布表 t_salary_dist
CREATE TABLE t_salary_dist(
salary varchar(30) DEFAULT NULL,
count int(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(五) Mysql中创建福利标签统计表 t_company_count
CREATE TABLE t_company_count(
company varchar(30) DEFAULT NULL,
count int(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(六) Mysql中创建技能标签统计表 t_kill_count
CREATE TABLE t_kill_count(
kills varchar(30) DEFAULT NULL,
count int(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(七) 完成表的创建后查询一下并退出Mysql
mysql> use JobData;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_JobData |
+-------------------+
| t_city_count |
| t_company_count |
| t_salary_dist |
| t_skill_count |
+-------------------+
4 rows in set (0.00 sec)
mysql> exit;
Bye
二. Sqoop 数据迁移
首先先切换到sqoop安装目录中的bin目录下
[root@zyc220 bin]# cd /export/servers/sqoop-1.4.6/bin/
之后进行迁移HDFS上的数据到mysql对应的表中
(一)将职位分布统计结果到城市分布表 t_city_count
./sqoop export \
-Dsqoop.export.records.per.statement=50 \
--batch \
--num-mappers 1 \
--connect "jdbc:mysql://localhost:3306/JobData?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useServerPrepStmts=false&rewriteBatchedStatements=true" \
--username root \
--password 123456 \
--table t_city_count \
--columns "city,count" \
--fields-terminated-by ',' \
--export-dir "/user/hive/warehouse/jobdata.db/t_ods_city"
(二)将职位薪资分布结果数据迁移到MySQL的t_salary_dist表
./sqoop export \
-Dsqoop.export.records.per.statement=50 \
--batch \
--num-mappers 1 \
--connect "jdbc:mysql://localhost:3306/JobData?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useServerPrepStmts=false&rewriteBatchedStatements=true" \
--username root \
--password 123456 \
--table t_salary_dist \
--columns "salary,count" \
--fields-terminated-by ',' \
--export-dir "/user/hive/warehouse/jobdata.db/t_ods_salary"
(三)将大数据相关职位福利标签统计结果迁移到Mysql的 t_company_count表
./sqoop export \
-Dsqoop.export.records.per.statement=50 \
--batch \
--num-mappers 1 \
--connect "jdbc:mysql://localhost:3306/JobData?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useServerPrepStmts=false&rewriteBatchedStatements=true" \
--username root \
--password 123456 \
--table t_company_count \
--columns "company,count" \
--fields-terminated-by ',' \
--export-dir "/user/hive/warehouse/jobdata.db/t_ods_company"
(四)将大数据相关职位技能标签的统计结果数据迁移到Mysql的t_kill_count表
./sqoop export \
-Dsqoop.export.records.per.statement=50 \
--batch \
--num-mappers 1 \
--connect "jdbc:mysql://localhost:3306/JobData?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useServerPrepStmts=false&rewriteBatchedStatements=true" \
--username root \
--password 123456 \
--table t_kill_count \
--columns "kills,count" \
--fields-terminated-by ',' \
--export-dir "/user/hive/warehouse/jobdata.db/t_ods_kill"
(五)进入Mysql中查询数据,验证是否导入成功
mysql中查询数据时,有些字段会出现“?”,原因是MySQL版本较旧,不支持该字符集。
[root@zyc220 bin]# mysql -u root -p
输入以下代码:
select * from t_city_count;
select * from t_kill_count;
select * from t_salary_dist;
select * from t_company_count;
出现下面内容为正确
(六)在SQLyog中查询数据,验证是否导入成功
至此已经完成sqoop数据迁移。然后我们对数据进行可视化操作,下节我们讲对数据进行可视化!