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

本节演示使用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之间进行数据导入导出的过程。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值