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

02 使用Sqoop完成mysql与hdfs之间的数据交换

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

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

1、前提约束

2、操作步骤

(1) 把mysql中的数据导入HDFS

  • 在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
  • 把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中导入的文件
cd  /root/hadoop-2.5.2/bin
./hdfs dfs -ls /data

(2)将hdfs中的数据导入到mysql
hdfs的/data文件夹下有文档,内容如下:

1 ali 20
2 xiaoli 3

执行以下命令:

mysql -uroot -pzhangli
create database test;
use test;
create table t_info(id int,name varchar(20),age int);
exit;
cd /root/sqoop-1.4.7.bin__hadoop-2.6.0/bin
./sqoop export   --connect jdbc:mysql://localhost:3306/test --username root  --password zhangli  --table t_info --export-dir /data --input-fields-terminated-by ' '
  • 执行完毕,查看mysql数据库实例test中的t_info表,便能看到数据已经被导入该表。
    以上就是使用Sqoop完成mysql与hdfs之间的数据交换。

相关文章

网友评论

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

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