目录
一、sqoop概述和安装
1.1 sqoop的简介
1.2 sqoop的原理
1.3 sqoop的安装
二、常用指令介绍
2.1 查看常用指令
2.2 指令帮助信息
2.3 去官网上查询
三、sqoop的基本用法(重点)
3.1 查询数据库和表
3.1.1 查看数据库:sqoop list-databases
3.1.2 查看表
3.2 Sqoop的import
3.2.1 数据准备
hdfs">3.2.2 mysql->hdfs
hive">3.2.3 mysql–>hive
hbase">3.2.4 mysql->hbase
3.3 Sqoop的export
mysql">3.3.1 hdfs->mysql
mysql">3.3.2 hive-->mysql
一、sqoop概述和安装
1.1 sqoop的简介
1. sqoop是apache基金会的一个顶级项目(已经退役,)
2. sqoop是一款可以将数据在RDBMS和Hadoop生态中导入导出的项目
3. sqoop最后一个版本是1.4.7
apache有一个专门管理退役软件的仓库。
退役原因:
1. 软件开发的不够好,弃用了。
2. 没什么提升空间了,已经趋近于完美了,不再更新了,apache对这样的项目,如果发现一两年左右都没有更新时,就会开会决定移动到退役仓库中。
1.2 sqoop的原理
1. sqoop采用了MapReduce的N个MapTask 来进行导入导出数据。 并行执行,以便提高效率
2. 在导入时,sqoop会逐行读取RDBMS中的数据,并落地到相应的Hadoop生态中
3. 真正导入数据前,会根据指定的切分字段,先计算数据的范围,然后使用切分器将数据的值范围按照MapTask的数量平分,由MapTask开始导入数据。(注意:有可能某个MapTask要处理的数据范围是空的)
注意: MapTask的数量默认是4个。
1.3 sqoop的安装
1. sqoop是apache基金会的一个顶级项目(已经退役,) 2. sqoop是一款可以将数据在RDBMS和Hadoop生态中导入导出的项目 3. sqoop最后一个版本是1.4.7 apache有一个专门管理退役软件的仓库。 退役原因: 1. 软件开发的不够好,弃用了。 2. 没什么提升空间了,已经趋近于完美了,不再更新了,apache对这样的项目,如果发现一两年左右都没有更新时,就会开会决定移动到退役仓库中。
1.2 sqoop的原理
1. sqoop采用了MapReduce的N个MapTask 来进行导入导出数据。 并行执行,以便提高效率
2. 在导入时,sqoop会逐行读取RDBMS中的数据,并落地到相应的Hadoop生态中
3. 真正导入数据前,会根据指定的切分字段,先计算数据的范围,然后使用切分器将数据的值范围按照MapTask的数量平分,由MapTask开始导入数据。(注意:有可能某个MapTask要处理的数据范围是空的)
注意: MapTask的数量默认是4个。
1.3 sqoop的安装
步骤1) 上传、解压、更名、配置环境变量、生效、验证
[root@xxx01 ~]# tar -zxvf sqoop-1.4.7.bin__hadoop-2.6.0.tar.gz -C /usr/local/ [root@xxx01 ~]# cd /usr/local/ [root@xxx01 local]# mv sqoop-1.4.7.bin__hadoop-2.6.0/ sqoop [root@xxx01 local]# vim /etc/profile [root@xxx01 local]# source /etc/profile [root@xxx01 local]# sqoop-version
步骤2)配置sqoop的环境脚本文件
[root@xxx01 local]# cd sqoop/conf/ [root@xxx01 conf]# cp sqoop-env-template.sh sqoop-env.sh [root@xxx01 conf]# vim sqoop-env.sh ...........省略............. export HADOOP_COMMON_HOME=/usr/local/hadoop #Set path to where hadoop-*-core.jar is available export HADOOP_MAPRED_HOME=/usr/local/hadoop #set the path to where bin/hbase is available export Hbase_HOME=/usr/local/hbase #Set the path to where bin/hive is available export HIVE_HOME=/usr/local/hive #Set the path for where zookeper config dir is export ZOOCFGDIR=/usr/local/zookeeper/conf
步骤3)导入mysql驱动到sqoop的lib目录
1. 如果连接的mysql是5.7版本以下, 可以导入mysql-connector-java-5.1.xxx-bin.jar 2. 如果连接的mysql是8.0版本以上, 需要导入mysql-connector-java-8.0.xxx-bin.jar
二、常用指令介绍
2.1 查看常用指令
[root@xxx01 lib]# sqoop help
Available commands:
codegen Generate code to interact with database records
create-hive-table import a table definition into Hive
eval evaluate a SQL statement and display the results
export Export an HDFS directory to a database table
help List available commands
import import a table from a database to HDFS
import-all-tables import tables from a database to HDFS
import-mainframe import datasets from a mainframe server to HDFS
job Work with saved jobs
list-databases List available databases on a server
list-tables List available tables in a database
merge Merge results of incremental imports
metastore Run a standalone Sqoop metastore
version Display version information
2.2 指令帮助信息
[root@xxx01 ~]# sqoop help list-databases
Common arguments:
--connect 用于指定连接jdbc的url
--username 用户名
--password 密码
................省略......................
2.3 去官网上查询
https://sqoop.apache.org/docs/1.4.7/SqoopUserGuide.html#_syntax_12
三、sqoop的基本用法(重点)
3.1 查询数据库和表
[root@xxx01 lib]# sqoop help Available commands: codegen Generate code to interact with database records create-hive-table import a table definition into Hive eval evaluate a SQL statement and display the results export Export an HDFS directory to a database table help List available commands import import a table from a database to HDFS import-all-tables import tables from a database to HDFS import-mainframe import datasets from a mainframe server to HDFS job Work with saved jobs list-databases List available databases on a server list-tables List available tables in a database merge Merge results of incremental imports metastore Run a standalone Sqoop metastore version Display version information
2.2 指令帮助信息
[root@xxx01 ~]# sqoop help list-databases
Common arguments:
--connect 用于指定连接jdbc的url
--username 用户名
--password 密码
................省略......................
2.3 去官网上查询
https://sqoop.apache.org/docs/1.4.7/SqoopUserGuide.html#_syntax_12
三、sqoop的基本用法(重点)
3.1 查询数据库和表
https://sqoop.apache.org/docs/1.4.7/SqoopUserGuide.html#_syntax_12
三、sqoop的基本用法(重点)
3.1 查询数据库和表
连接mysql,一定要远程授权
5.7版本: grant all privileges on *.* to root@'%' identified by '远程密码' with grant option 5.8版本: 百度
3.1.1 查看数据库:sqoop list-databases
1) 连接windows上的mysql8.0的写法:
sqoop list-databases
--connect jdbc:mysql://10.20.152.47:3306?serverTimezone=UTC
--username root
--password mmforu
2) 连接xxx03上的mysql5.7的写法
sqoop list-databases
--connect jdbc:mysql://xxx03:3306
--username root
--password @Mmforu45
注意:查看所有库,url只需要写到3306
3.1.2 查看表
sqoop list-tables
--connect jdbc:mysql://xxx03:3306/hive
--username root
--password @Mmforu45
注意:要指定一个具体的database进行查看
3.2 Sqoop的import
sqoop将数据从RDBMS中传输到Hadoop生态中,叫导入,即import
3.2.1 数据准备
create database sz2103 default character set utf8;
use sz2103;
-- emp表 有主键约束
DROp TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`EMPNO` int(4) NOT NULL,
`ENAME` varchar(10),
`JOB` varchar(9),
`MGR` int(4),
`HIREDATE` date,
`SAL` int(7),
`COMM` int(7),
`DEPTNO` int(2),
PRIMARY KEY (`EMPNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `emp` VALUES ('7200', 'SMITH', 'CLERK', '7902', '1980-12-17', '800', null, '20');
INSERT INTO `emp` VALUES ('7499', 'ALLEN', 'SALESMAN', '7698', '1981-02-20', '1600', '300', '30');
INSERT INTO `emp` VALUES ('7509', 'TOM', 'SALESMAN', '7698', '1983-02-20', '1300', '400', '20');
INSERT INTO `emp` VALUES ('7521', 'WARD', 'SALESMAN', '7698', '1981-02-22', '1250', '500', '30');
INSERT INTO `emp` VALUES ('7599', 'JONES', 'MANAGER', '7800', '1981-04-02', '2975', null, '20');
INSERT INTO `emp` VALUES ('7600', 'MARTIN', 'SALESMAN', '7698', '1981-09-28', '1250', '1400', '30');
INSERT INTO `emp` VALUES ('7698', 'BLAKE', 'MANAGER', '7800', '1981-05-01', '2850', null, '30');
INSERT INTO `emp` VALUES ('7782', 'CLARK', 'MANAGER', '7800', '1981-06-09', '2450', null, '10');
INSERT INTO `emp` VALUES ('7799', 'SCOTT', 'ANALYST', '7599', '1987-04-19', '3000', null, '20');
INSERT INTO `emp` VALUES ('7800', 'KING', 'PRESIDENT', null, '1981-11-17', '5000', null, '10');
INSERT INTO `emp` VALUES ('7844', 'TURNER', 'SALESMAN','7698', '1981-09-08', '1500', '0', '30');
INSERT INTO `emp` VALUES ('7876', 'ADAMS', 'CLERK', '7799', '1987-05-23', '1100', null, '20');
INSERT INTO `emp` VALUES ('7900', 'JAMES', 'CLERK', '7698', '1981-12-03', '950', null, '30');
INSERT INTO `emp` VALUES ('7902', 'FORD', 'ANALYST', '7599', '1981-12-03', '3000', null, '20');
INSERT INTO `emp` VALUES ('7934', 'MILLER', 'CLERK', '7782', '1982-01-23', '1300', null, '10');
INSERT INTO `emp` VALUES ('8000', 'superman', null, '7782', '1982-01-23', '1300', null, '10');
select * from emp;
-- 学生表 没有主键
DROp TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`s_id` varchar(20) NOT NULL,
`s_name` varchar(20) NOT NULL DEFAULT '',
`s_birth` varchar(20) NOT NULL DEFAULT '',
`s_sex` varchar(10) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `student` VALUES
('01','赵雷','1990-01-01','男'),
('02','钱电','1990-12-21','男'),
('03','孙风','1990-05-20','男'),
('04','李云','1990-08-06','男'),
('05','周梅','1991-12-01','女'),
('06','吴兰','1992-03-01','女'),
('07','郑竹','1989-07-01','女'),
('08','王菊','1990-01-20','女'),
('09','张飞','1990-9-25','男'),
('10','刘备','1990-01-25','男'),
('11','关羽','1990-01-25','男');
select * from student;
3.2.2 mysql->hdfs
案例1)导入有主键的表到hdfs
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp --target-dir /sqoop/emp -m 3 注意:再次导入时,会报目录已经存在,可以使用--delete-target-dir [root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp --target-dir /sqoop/emp -m 3 --delete-target-dir
总结
1. 默认分隔符是逗号
2. 如何切分的?
计算主键字段的最大和最小值,然后差值/MapTask的数量,然后平均
案例2)导入无主键的表到hdfs上
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table student --target-dir /sqoop/student -m 3 --delete-target-dir 以上内容会报错 解决方式如下: sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table student --target-dir /sqoop/student -m 1 --delete-target-dir 参数:--delete-target-dir 用于删除已经存在的目录 第二种解决方式如下: sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table student --target-dir /sqoop/student --split-by s_id --delete-target-dir
注意事项:如果表没有主建约束的字段
方式1: 指定--split-by参数 方式2: MapTask的数量只能是1
注意: 如果报以下错误,需要将提示中的属性和值添加到导入语句中
ERROR tool.importTool: import failed: java.io.IOException: Generating splits for a textual index column allowed only in case of "-Dorg.apache.sqoop.splitter.allow_text_splitter=true" property passed as a parameter sqoop import -Dorg.apache.sqoop.splitter.allow_text_splitter=true --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table student --target-dir /sqoop/student --split-by s_id --delete-target-dir
案例3)指定列分隔符进行导入
参数:--fields-terminated-by 用于指定列分隔符
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp --target-dir /sqoop/emp --fields-terminated-by 't' -m 3 --delete-target-dir
案例4)测试columns
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp --columns "empno,ename" --target-dir /sqoop/emp --fields-terminated-by 't' -m 3 --delete-target-dir
案例5)测试where
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp --columns "empno,ename,deptno" --target-dir /sqoop/emp --fields-terminated-by 't' -m 3 --delete-target-dir --where 'deptno = 10 or deptno = 30'
注意:参数--where后的条件建议 加上引号
案例6)测试-e|--query
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --query "select empno,mgr,deptno from emp where $ConDITIONS and deptno = 10" --target-dir /sqoop/emp --fields-terminated-by 't' --split-by 'empno' -m 3 --delete-target-dir
注意事项:
1. 当使用--query|-e 时, 一定不能用--table 2. 当使用--query|-e 时, 一定要使用--split-by 注意:指定的字段必须是select子句中的某一个字段 3. 当使用--query|-e 时, 在from tablename 后一定要在where后带上$CONDITIONS关键词, 注意:如果是双引号,$前要添加转义字符 4. 当使用--query|-e 时, 参数where失效,所以没有必要指定 5. 当使用--query|-e 时, 参数columns的作用,是对select中的字段进行筛选。 但是,用的不多,没有必要在columns里进行筛选。
案例7)测试字符串类型的null处理和非字符串类型的null处理
参数:--null-string --null-non-string 这两个参数是指将mysql里的null值 导入到hdfs上以什么的样式显示。
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --query "select * from emp where $CONDITIONS" --target-dir /sqoop/emp --fields-terminated-by 't' --split-by 'empno' -m 3 --delete-target-dir --null-string '\N' --null-non-string 'null' 注意: 在hive表中,hive的null使用N表示
3.2.3 mysql–>hive
案例1)导入hive中
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --query "select * from emp where $CONDITIONS" --split-by 'empno' --target-dir /sqoop/emp --delete-target-dir --hive-import --hive-table 'sz2103.emp_1' 不需要提前在hive中创建表。
注意的问题如下:
1. sqoop导入hive的原理是:先导入到hdfs上 ,然后再使用load data指令上传到hive的表目录下 2. sqoop指令会在所在机器上临时运行hive客户端使用load进行加载数据,所以要注意客户端的问题,是本地还是远程。 3. 如果报以下错误,说明sqoop找不到hive的common包 java.lang.ClassNotFoundException: org.apache.hadoop.hive.conf.HiveConf 解决办法如下: 1. 将hive的lib目录下的hive-common-2.xxxx.jar 拷贝到sqoop的lib目录 2. 在sqoop-env.sh里配置 export HIVE_CONF_DIR=/usr/local/hive/conf, 最好source /etc/profile
案例2)测试--hive-overwrite
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --query "select * from emp where $CONDITIONS" --split-by 'empno' --target-dir /sqoop/emp --delete-target-dir --hive-import --hive-overwrite --hive-table 'sz2103.emp_1'
案例3)指定分隔符
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --query "select * from emp where $CONDITIONS" --split-by 'empno' --target-dir /sqoop/emp --delete-target-dir --hive-import --hive-overwrite --hive-table 'sz2103.emp_1' --fields-terminated-by 't'
结论:
1. 添加--hive-overwrite会覆盖原有的hive的表数据,反之没有此参数,表示追加数据。 2. 必须要有--hive-import选项。 3. 从mysql中导入到hive中时,如果不指定分隔符,默认使用的是^A, 4. 可以使用--fields-terminated-by指定分隔符, 注意,该表要么不存在,要么表的分隔符和你要指定的分隔符一致。
案例4) 测试--create-hive-table
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --query "select * from emp where $CONDITIONS" --split-by 'empno' --target-dir /sqoop/emp --delete-target-dir --hive-import --hive-overwrite --hive-table 'sz2103.emp_11' --fields-terminated-by 't' --create-hive-table
结论:如果表提前存在,就会报错
案例5)分区导入hive
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp --target-dir /sqoop/emp --delete-target-dir --hive-import --create-hive-table --hive-overwrite --hive-table 'sz2103.emp_12' --fields-terminated-by 't' --hive-partition-key "year" --hive-partition-value "10"
注意事项
1. 可以提前创建分区表,方便指定分区字段 2. 如果没有提前创建分区表,导入语句会主动创建分区表。分区字段不要是mysql里查询的字段。
3.2.4 mysql->hbase
案例演示
[root@xxx01 ~]# sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --query 'select empno,ename,deptno from emp where $CONDITIONS' --target-dir /sqoop/emp --delete-target-dir --hbase-table 'myns:emp1001' --column-family 'f1' --split-by 'empno' --hbase-row-key "empno,deptno"
结论:
导入时: 可以使用--query和--split-by的组合形式
也可以使用--table
1. rowkey是由--hbase-row-key指定的,如果没有指定,则使用表的主键或者--split-by指定的切分字段充当.
2. 也可以使用--hbase-row-key指定复合主键,字段是用逗号分开。复合主键是由下划线拼接而成。
3. 大小写问题:
如果指定的是--query|-e,
--split-by和--hbase-row-key的字段的大小写都要select子句中保持一致。
如果是--table 或者是select * 都应该大写。
3.3 Sqoop的export
Table 29. Export control arguments:
3.3.1 hdfs->mysql
注意:导出时,mysql中的表必须提前创建。
1)创建一个带有主键约束的表emp_1,字段数与hdfs上的一致 drop table `emp_1`; CREATE TABLE `emp_1` ( `EMPNO` int(4) NOT NULL, `ENAME` varchar(10), `JOB` varchar(9), `MGR` int(4), `HIREDATE` date, `SAL` int(7), `COMM` int(7), `DEPTNO` int(2), PRIMARY KEY (`EMPNO`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 2)创建一个带有主键约束的表emp_2,字段数少于hdfs上的列数 CREATE TABLE `emp_2` ( `EMPNO` int(4) NOT NULL, `ENAME` varchar(10), `JOB` varchar(9), `MGR` int(4), `HIREDATE` date, `SAL` int(7), `COMM` int(7), PRIMARY KEY (`EMPNO`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 3)创建一个带有主键约束的表emp_3,字段数多于hdfs上的列数 CREATE TABLE `emp_3` ( `EMPNO` int(4) NOT NULL, `ENAME` varchar(10), `JOB` varchar(9), `MGR` int(4), `HIREDATE` date, `SAL` int(7), `COMM` int(7), `DEPTNO` int(2), `desc` varchar(100), PRIMARY KEY (`EMPNO`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 4)创建一个无主键的表 CREATE TABLE `emp_4` ( `EMPNO` int(4) NOT NULL, `ENAME` varchar(10), `JOB` varchar(9), `MGR` int(4), `HIREDATE` date, `SAL` int(7), `COMM` int(7), `DEPTNO` int(2), `desc` varchar(100) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
案例1)有主键字段、字段数相同
注意类型是否匹配,字符串转成int,要注意是不是纯数字字符串。
[root@xxx01 ~]# sqoop export --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp_1 --export-dir /sqoop/emp --fields-terminated-by 't'
注意事项:
1. 分隔符:
hdfs上的数据的分隔符,必须在导出语句时,指定出来
2. 类型能否隐式转换, 隐式转换不了就会报错
案例2)有主键字段,字段数少于hdfs上列数
[root@xxx01 ~]# sqoop export --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp_2 --export-dir /sqoop/emp --fields-terminated-by 't'
案例3)代有主键字段的表,字段数多于hdfs上列数
[root@xxx01 ~]# sqoop export --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp_3 --export-dir /sqoop/emp --fields-terminated-by 't' [root@xxx01 ~]# sqoop export --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp_1 --columns "empno,job,ename,deptno" --export-dir /sqoop/emp --fields-terminated-by 't'
总结:
通过案例2和3 总结如下: --1. 导出时,如果不指定columns,是按照hdfs上的顺序给建表期间指定的字段顺序一一赋值。与mysql的字段的个数无关。注意mysql字段的not null约束情况。 --2. 如果指定了columns参数,也是按照顺序将hdfs上的列依次赋值给columns指定的字段。 注意数据类型的隐式转换。
案例4)导出到无主键约束的表中
[root@xxx01 ~]# sqoop export --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp_4 --export-dir /sqoop/emp --fields-terminated-by 't'
总结:
mysql的表没有主键约束,更方便导出到msyql里,如果mysql中有主键,则要注意主键字段的重复值问题。
案例5)测试类型没有对应上
DROP TABLE IF EXISTS `emp_5`; CREATE TABLE `emp_5` ( `EMPNO` int(4) NOT NULL, `ENAME` varchar(10), `JOB` varchar(9), `MGR` int(4), `SAL` int(7), `COMM` int(7), `DEPTNO` int(2) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 说明:emp_5的第五个字段sal是int类型,但是hdfs上的第五列是日期格式“yyyy-MM-dd”。 sqoop export --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp_5 --export-dir /sqoop/emp --fields-terminated-by 't' 注意:一定会报错
结论:
1. 导出时,mysql中的表必须提前创建。 2. 导出时,字段之间的切分符号默认是逗号。如果hdfs上的文件不是逗号分隔符,需要使用--fields-terminated-by或--input-fields-terminated-by参数指定分隔符 3. 导出时,是按照hdfs上文件从左到右的顺序给mysql表的字段赋值 4. 导出时,mysql的表的字段数与hdfs上的列数可以不相同 5. 导出时,字段类型要一致或者可以隐式转换。 6. 带有主键约束的mysql表,要注意导出的数据的主键约束的情况,不能重复 7. 使用--columns给mysql中的某些字段赋值时,没有包含在参数中的字段要么有默认值,要么不能设置not null 8. 在sqoop1.4.7中,hdfs上的字符串'null'是可以转成mysql中字符串类型字段的null值, 也可以转成mysql中非字符串类型字段的null值。
案例6)input-null-string和input-null-non-string参数的用法
就是将hdfs上的字符串序列转成mysql中的string的null或者是非String类型的null.
说明:
1. --input-null-string 该参数的作用:将字符串序列,转成mysql里字符串类型的null 2. --input-null-non-string 该参数的作用:将非字符串序列,转成mysql中整型的null,但是可能会失败。 如果是对null-non-string转到hdfs上的数字字符串,可以在导出时,再次转成null. 如果不是参数null-non-string转成的数字字符串,在导出时,会报错。 drop table `emp_6`; CREATE TABLE `emp_6` ( `EMPNO` int(4) NOT NULL, `ENAME` varchar(10), `JOB` varchar(9), `MGR` int(4), `HIREDATE` date, `SAL` int(7), `COMM` int(7), `DEPTNO` int(2), PRIMARY KEY (`EMPNO`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; sqoop export --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp_6 --export-dir /sqoop/emp --fields-terminated-by 't' --input-null-string "SMITH" --input-null-non-string '10000' sqoop import --connect jdbc:mysql://xxx03:3306/sz2103 --username root --password @Mmforu45 --table emp --target-dir /sqoop/emp --fields-terminated-by 't' --delete-target-dir --null-string '\N' --null-non-string '10000'
3.3.2 hive-->mysql
hive到mysql的本质其实就是hdfs到mysql
1. 先查看表目录下的文件的分隔符,或者show create table tablename;
2. 然后使用hdfs-->mysql的相关语法导出即可
drop table `emp_7`;
CREATE TABLE `emp_7` (
`EMPNO` int(4) NOT NULL,
`ENAME` varchar(10),
`JOB` varchar(9),
`MGR` int(4),
`HIREDATE` date,
`SAL` int(7),
`COMM` int(7),
`DEPTNO` int(2),
PRIMARY KEY (`EMPNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
sqoop export
--connect jdbc:mysql://xxx03:3306/sz2103
--username root
--password @Mmforu45
--table emp_7
--export-dir /user/hive/warehouse/sz2103.db/emp_11
--fields-terminated-by 't'
--null-string '\N'
--null-non-string 'null'
(=-=,sqoop简单易懂,上手挺快的,距离元旦假期还有四天!!!大伙都准备去哪里玩啊~)



