一. 导入数据
在Sqoop中,“导入”概念指:从非大数据集群(RDBMS)向大数据集群(HDFS,HIVE,HBASE)中传输数据,叫做:导入,即使用import关键字。
1. RDBMS到HDFS
- 确定Mysql服务开启正常
[bigdata@hadoop002 sqoop]$ mysql -uroot -p199712
- 在Mysql中新建一张表并插入一些数据
// 创建脚本
[bigdata@hadoop002 datas]$ vim company.sql
create database company;
use company;
create table staff(id int(4) primary key not null auto_increment, name varchar(255), sex varchar(255));
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
insert into staff(name, sex) values('Thomas', 'Male');
insert into staff(name, sex) values('Catalina', 'FeMale');
// 批量执行脚本
mysql> source /opt/module/datas/company.sql
// 查看是否成功
mysql> use company;
mysql> select * from staff;
- 开启所需要的组件
[bigdata@hadoop002 datas]$ start-dfs.sh
[bigdata@hadoop003 module]$ start-yarn.sh
[bigdata@hadoop002 zookeeper-3.4.10]$ bin/start-allzk.sh
[bigdata@hadoop002 hbase]$ bin/start-hbase.sh
// 要起的服务和下图一样
- 导入数据
// (1)全表导入
[bigdata@hadoop002 sqoop]$ bin/sqoop import \
--connect jdbc:mysql://hadoop002:3306/company \
--username root \
--password 199712 \
--table staff \
--target-dir company \
--delete-target-dir \
--num-mappers 2 \
--split-by id \
--fields-terminated-by "\t"
运行成功:
在web端查看
// (2)查询导入sqoop_query
[bigdata@hadoop002 sqoop]$ bin/sqoop import \
--connect jdbc:mysql://hadoop002:3306/company \
--username root \
--password 199712 \
--target-dir company \
--delete-target-dir \
--num-mappers 2 \
--split-by id \
--fields-terminated-by "\t" \
--query 'select * from staff where $CONDITIONS'
提示:must contain 'CONDITIONS' in WHERE clause. 如果query后使用的是双引号,则CONDITIONS前必须加转移符,防止shell识别为自己的变量。
//(3)部分导入
[bigdata@hadoop002 sqoop]$ bin/sqoop import \
--connect jdbc:mysql://hadoop002:3306/company \
--username root \
--password 199712 \
--table staff \
--columns id,name \
--where 'id>=10 and id<=20' \
--target-dir company \
--delete-target-dir \
--num-mappers 2 \
--split-by id \
--fields-terminated-by "\t"
提示:columns中如果涉及到多列,用逗号分隔,分隔时不要添加空格
查看web端并下载查看内容:
//(4)使用sqoop关键字筛选查询导入数据
[bigdata@hadoop002 sqoop]$ bin/sqoop import \
--connect jdbc:mysql://hadoop002:3306/company \
--username root \
--password 199712 \
--target-dir company \
--delete-target-dir \
--num-mappers 2 \
--fields-terminated-by "\t" \
--table staff \
--where "id=1"
2. RDBMS到Hive
$ bin/sqoop import \
--connect jdbc:mysql://hadoop002:3306/company \
--username root \
--password 199712 \
--hive-import \
--hive-overwrite \
--hive-table staff_hive \
--target-dir company \
--delete-target-dir \
--num-mappers 2 \
--split-by id \
--fields-terminated-by "\t" \
--query 'select * from staff where $CONDITIONS'
提示:该过程分为两步,第一步将数据导入到HDFS,第二步将导入到HDFS的数据迁移到Hive仓库,第一步默认的临时目录是/user/bigdata/表名 如果运行成功结果如图:
启动hive并查看是否成功
[bigdata@hadoop002 module]$ cd hive/
[bigdata@hadoop002 hive]$ bin/hive
hive> show tables;
// 查看数据
hive> select * from staff_hive;
3. RDBMS到Hbase
$ bin/sqoop import \
--connect jdbc:mysql://hadoop002:3306/company \
--username root \
--password 199712 \
--hbase-table staff_hbase \
--hbase-row-key "id" \
--column-family "info" \
--target-dir company \
--delete-target-dir \
--num-mappers 2 \
--split-by id \
--fields-terminated-by "\t" \
--query 'select * from staff where $CONDITIONS'
提示:sqoop1.4.6只支持HBase1.0.1之前的版本的自动创建HBase表的功能
解决方案:手动创建HBase表
hbase> create 'staff_hbase','info'
- 在HBase中scan这张表得到如下内容
hbase(main):001:0> scan 'staff_hbase'
二. 导出数据
在Sqoop中,“导出”概念指:从大数据集群(HDFS,HIVE,HBASE)向非大数据集群(RDBMS)中传输数据,叫做:导出,即使用export关键字。
HIVE/HDFS到RDBMS
- 1. 先登录mysql
[bigdata@hadoop002 hbase]$ mysql -uroot -p199712
mysql> use company;
mysql> show tables;
// 存在数据先把数据清空
mysql> truncate table staff;
- 2.运行脚本
$ bin/sqoop export \
--connect jdbc:mysql://hadoop002:3306/company \
--username root \
--password 199712 \
--table staff \
--num-mappers 2 \
--export-dir /user/hive/warehouse/staff_hive \
--input-fields-terminated-by "\t"
提示:Mysql中如果表不存在,不会自动创建
在Mysql中查看
三. 脚本打包
使用opt格式的文件打包sqoop命令,然后执行
- 1. 创建一个.opt文件
[bigdata@hadoop002 sqoop]$ vim opt.txt
- 2. 编写sqoop脚本
--connect
jdbc:mysql://hadoop002:3306/company
--username
root
--password
199712
- 3. 执行该脚本
[bigdata@hadoop002 sqoop]$ bin/sqoop list-databases --options-file opt.txt
本次的分享就到这里了