美文网首页
03 使用Sqoop完成mysql与hive之间的数据交换

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

作者: 张力的程序园 | 来源:发表于2020-06-30 20:49 被阅读0次

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

1、前提约束

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之间进行数据导入导出的过程。

相关文章

网友评论

      本文标题:03 使用Sqoop完成mysql与hive之间的数据交换

      本文链接:https://www.haomeiwen.com/subject/hxffqktx.html