03 使用Sqoop完成mysql与hive之间的数据交换

2020-06-30  本文已影响0人  张力的程序园

本节演示使用Sqoop完成mysql与hive之间的数据交换。

1、前提约束

2、操作步骤

(1)mysql数据导入hive

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;
cd /root/hadoop-2.5.2
./hdfs dfs -mkdir /data
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 ' '
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之间进行数据导入导出的过程。

上一篇 下一篇

猜你喜欢

热点阅读