1.导入导出数据区分
导入:关系型数据库到hdfs上
导出:hdfs到关系型数据库
导入导出与参数位置没关系!!!
2.sqoop用法手册:
http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html
3.导入数据
3 .1导入全部数据到hdfs
bin/sqoop import \ -- bin/sqoop启动命令 import 导入
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--delete-target-dir \ -- 如果存在就删除该目录(公司工作慎用!!!)
--target-dir /userdb/1 \ -- 导出的目录
--table emp \ -- 导出的数据表
--m 1 -- 定义map数
代码:
bin/sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--delete-target-dir \
--target-dir /userdb/1 \
--table emp --m 1
3.2自定义查询导入
bin/sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--target-dir /wherequery12 \
--query 'select id,name,deg from emp WHERE id>1203 and $CONDITIONS' \
--split-by id \
--fields-terminated-by '\t' \
--m 2
(双引号: “select id,hno,street,city from emp_add where id <=1 and \$CONDITIONS;“)需要转义一下
3.3导入指定的列
sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--columns id,phno \ --指定列
--target-dir /wherequery1 \
--table emp_conn \
--m 1
代码:
sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--columns id,phno \
--target-dir /wherequery1 \
--table emp_conn \
--m 1
3.4关键字筛选查询导入数据
where
sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--where "city ='sec-bad'" \ --where 条件!!!
--target-dir /wherequery \
--table emp_add --m 1
代码:
sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--where "city ='sec-bad'" \
--target-dir /wherequery \
--table emp_add --m 1
where+指定列
sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--columns id,phno \
--where "city ='sec-bad'" \
--target-dir /wherequery1 \
--table emp_conn \
--m 1
代码:
sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--columns id,phno \
--where "city ='sec-bad'" \
--target-dir /wherequery1 \
--table emp_conn \
--m 1
3.5导入到hive中
(1)先复制表结构到hive中再导入数据
将关系型数据的表结构复制到hive中
sqoop create-hive-table \
--connect jdbc:mysql://node01:3306/userdb \
--table emp_add \ --为mysql中的数据库sqoopdb中的表
--username root \
--password 123456 \
--hive-table test.emp_add_sp --为hive中新建的表名称
代码:
sqoop create-hive-table \
--connect jdbc:mysql://node01:3306/userdb \
--table emp_add \
--username root \
--password 123456 \
--hive-table test.emp_add_sp
注意:一定要先建好hive数据库,不然会报错!!!(java.io.IOException: Hive exited with status 88)
https://mp.csdn.net/postedit/103397944
从关系数据库导入文件到hive中
bin/sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--table emp_add \
--hive-table test.emp_add_sp \
--hive-import \
--m 1 \
--delete-target-dir
(2)直接复制表结构数据到hive中
sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--table emp_conn \
--hive-import \
--m 1 \
--hive-database test \
--delete-target-dir ;
实际动作是mysql-->hdfs-->hive 分为了两步
4.导出数据
4.1hive/hdfs导出
sqoop export \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password 123456 \
--table employee \
--export-dir /emp/emp_data
事先要创好mysql的数据库!!!
4.2
5.脚本打包
定时执行任务!!!
创建.opt文件
编写sqoop脚本
执行脚本