本节演示使用Sqoop完成mysql与hive之间的数据交换。
1、前提约束
- 已安装Sqoop
https://www.jianshu.com/p/c0f8d7ac4ae0 - 已安装Hadoop并启动
https://www.jianshu.com/p/b7ae3b51e559 - 已安装mysql并启动
https://www.jianshu.com/p/9a4976b23495 - 已安装Hive
https://www.jianshu.com/p/755944f01fab
假设虚拟机所在的Centos服务器ip为192.168.100.141,且已关闭防火墙。
假设sqoop安装目录为 /root/sqoop-1.4.7.bin__hadoop-2.6.0
假设hive安装目录为 /root/apache-hive-0.14.0-bin
假设mysql的账号密码为 root/zhangli
2、操作步骤
(1)mysql数据导入hive
- 在mysql中初始化数据,执行以下命令:
mysql -uroot -pzhangli
create database test;
use test;
create table t_info(id int,name varchar(20),age int);
insert into t_info(id,name,age) values(1,'ali',20);
insert into t_info(id,name,age) values(2,'xiaoli',3);
exit;
- 在hdfs中创建文件夹
cd /root/hadoop-2.5.2
./hdfs dfs -mkdir /data
- 将mysql数据导入hdfs,即把t_info表导入到hdfs
cd /root/sqoop-1.4.7.bin__hadoop-2.6.0/bin
./sqoop import --connect jdbc:mysql://localhost:3306/test --username root --password zhangli --query 'select id, name,age from t_info where $CONDITIONS LIMIT 100' --target-dir /data --delete-target-dir --num-mappers 1 --direct --fields-terminated-by ' '
- 把hdfs中的数据导入hive
cd /root/apache-hive-0.14.0-bin
./hive
create table if not exists t_info(id int comment 'user id', name string comment 'user name',age int 'user age') comment 'user info' row format delimited fields terminated by ' ' lines terminated by '\n' stored as textfile;
load data inpath '/data/part-m-00000' into table t_info
以上我们完成了把mysql中的数据导入到了hive中,接下来我们把hive中的数据导入到mysql
(2)hive数据导入mysql
执行以下语句:
mysql -uroot -pzhangli
use test;
delete from t_info;
exit;
cd /root/sqoop-1.4.7.bin__hadoop-2.6.0/bin
./sqoop export --connectjdbc:mysql://localhost:3306/test --username root --password zhangli --table t_info --fields-terminated-by ' ' --export-dir /data
以上就是mysql和hive之间进行数据导入导出的过程。
网友评论