Sqoop导入导出的几个例子
node2
Sqoop参数介绍_其他命令_导入导出
导入
例一:从DBMS到HDFS
1.node4下启动mysql
service mysqld start
mysql -u root -p
输入密码:123
2.node4下创建test数据库
create database test;
创建psn表
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151446357-1020620322.png)
3.node2中创建sqoop_test,编辑sqoop1
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151447279-1204999049.png)
import
--connect
jdbc:mysql://node4/test
--username
root
--password
123
--as-textfile
--columns
id,name,msg
--table
psn
--delete-target-dir
--target-dir
/sqoop/data
-m
1
4.执行命令--从DBMS导入到HDFS
sqoop --options-file sqoop1//选项使用脚本
sqoop --options-file sqoop1
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151447889-1437556269.png)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151449123-558398524.png)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151451295-432356683.png)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151457373-779178648.png)
例二:从DBMS到Hive
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151501420-1793598698.png)
注意!
$CONDITIONS规定
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151501967-2131568658.png)
/sqoop/tmp先将数据导入hdfs的临时目录,再将数据导入hive的目录
1.node4下启动mysql
service mysqld start
mysql -u root -p
输入密码:123
2.node4下创建test数据库
create database test;
创建psn表
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151446357-1020620322.png)
3.node2中创建sqoop_test,编辑sqoop2
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151447279-1204999049.png)
-e,--query和--table两个选项是冲突的,因为一个是指定单表,一个是指定多表的导入;
--warehouse-dir指定导入到hive中的时候到底从哪个目录下面导入;这个选项和hive中的warehouse-dir
配置项很像;
--hive-home指定hive的安装目录,如果节点上面配置了环境变量,并且导入了需要的jar包,那么就不需要
指定了;
sqoop将关系型数据库中的数据导入到hive中去的话,首先需要将数据导入到hdfs上面,因此需要先指定
hdfs上面一个临时存储这些数据的目录,然后才会把hdfs上面的数据导入到hive中去;也就是首先使用mr
到hdfs上面,然后在创建hive的表;可以再打开一个终端,查看hive中的表创建的情况;创建的时候
首先在mysql通过查看表字段,然后在hive中创建;我们没有指定hive表存放在什么位置。这个位置是由
hive的配置文件指定的,需要在那个位置查看;
import
--connect
jdbc:mysql://node4/test
--username
root
--password
123
--as-textfile
--query
'select id, name, msg from psn where id like "1%" and $CONDITIONS'
--delete-target-dir
-target-dir
/sqoop/tmp
-m
1
--hive-home
/home/hive-1.2.1
--hive-import
--create-hive-table
--hive-table
t_test
4.执行命令--从DBMS导入到Hive
先启动hive服务端node2: hive --service metastore
再启动hive客户端node1:
hive
sqoop --options-file sqoop2
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151504529-321809924.png)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151504873-804200550.png)
node1查看表t_test是否生成
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151505264-2110404465.png)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151505639-262532334.png)
导出
例三:从HDFS到DBMS
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151510357-1342046129.png)
指定HDFS中数据对应的目录
1.node4下启动mysql
service mysqld start
mysql -u root -p
输入密码:123
2.node4下创建test数据库
create database test;
创建h_psn表
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151510967-692550875.png)
3.node2中创建sqoop_test,编辑sqoop3
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151447279-1204999049.png)
export
--connect
jdbc:mysql://node4/test
--username
root
--password
123
-m
1
--columns
id,name,msg
--export-dir
/sqoop/data
--table
h_psn
4.执行sqoop命令
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151511561-2093108346.png)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151512092-1699605142.png)
查看mysql中的表,看数据是否导入
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151512342-1386506053.png)
例四:从Hive到DBMS
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170312151512951-748275216.png)
附件列表