
、Sqoop介绍:
Sqoop是用于在Hadoop(HDFS)和关系型数据库之间,进行数据迁移的工具。
Sqoop的导入:将关系型数据库中的数据导入到HDFS中
Sqoop的导出:将HDFS的数据导出到关系型数据中
本质:将导入或导出命令翻译成 MapReduce 程序来实现
在翻译出的 MapReduce 中主要是对 InputFormat 和 OutputFormat 进行定制

1、上传、解压、配置profile并刷新
(软链接 ln [-s] 原文件名 链接名 不带-s为硬链接)
2、进入到 conf 文件夹,找到 sqoop-env-template.sh,
复制并修改其名称为 sqoop-env.sh
修改 sqoop-env.sh 文件
#Set path to where bin/hadoop is availableexport HADOOP_COMMON_HOME=/opt/hadoop-2.7.3#Set path to where hadoop-*-core.jar is availableexport HADOOP_MAPRED_HOME=/opt/hadoop-2.7.3#set the path to where bin/hbase is availableexport HBASE_HOME=/opt/hbase-1.2.6#Set the path to where bin/hive is availableexport HIVE_HOME=/opt/apache-hive-1.2.2#Set the path for where zookeper config dir isexport ZOOCFGDIR=/opt/zookeeper-3.4.14
3、加入 mysql 驱动包到 sqoop1.4.7/lib 目录下

1.sqoop help -查看sqoop支持的指令
usage: sqoop COMMAND [ARGS]
Available commands:codegen Generate code to interact with database recordscreate-hive-table Import a table definition into Hiveeval Evaluate a SQL statement and display the resultsexport Export an HDFS directory to a database tablehelp List available commandsimport Import a table from a database to HDFSimport-all-tables Import tables from a database to HDFSimport-mainframe Import datasets from a mainframe server to HDFSjob Work with saved jobslist-databases List available databases on a serverlist-tables List available tables in a databasemerge Merge results of incremental importsmetastore Run a standalone Sqoop metastoreversion Display version informationSee 'sqoop help COMMAND' for information on a specific command
2.sqoop help <command> -查看某一具体指令
eg : sqoop help import
……
3、查看数据库列表
sqoop-list-databases \--connect jdbc:mysql://localhost:3306 \--username root \--P
4、查看表
sqoop list-tables \--connect jdbc:mysql://localhost:3306/mysql \--username root \--P
5、创建与mysql中test表相同的hive表hive_test:
sqoop create-hive-table \--connect jdbc:mysql://hadoop1:3306/mysql \--username root \--P \--table test \--hive-table hive_test

连接windows数据库需要:开启远程访问、使用ipconfig中的ip、开启3306端口
1.简单导入
eg1:全部导入
use xzdb
select * from article
sqoop import \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root \--P \--table hero \--target-dir /sqoop/test \[-m 1]
eg2:带条件导入
use xzdb
select * from hero where name = 'name1'
sqoop import \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root \--P \--table hero \--where 'name = "name1"' \--target-dir sqoop/hero1 -m 1
eg3:带条件导入列
sqoop import \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root \--P \--table hero \--columns 'name' \--where 'name = "name1"' \--target-dir sqoop/hero2 -m 1
eg4:自定义导入
sqoop import \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root \--P \--query 'select * from hero where $CONDITIONS and id > 6150' \--target-dir sqoop/hero -m 1
注:
1、自定义的SQL语句中必须带有WHERE $CONDITIONS
2、外层使用单引号,内层使用双引号,$CONDITIONS的$符号不用转义
控制参数:
--table 指定要导入的表
--target-dir 指定目录路径(HDFS)
--columns "id,name,age" 指定要导入的字段
-m 指定mapper任务数量 默认4
--split-by column 通常和--m一起使用,用于并行导入
--where 指定要导入哪些行记录
--query <SQL> 可以导入SQL的执行结果(不能使用--table --columns参数)
编译未来
2.增量导入:
1、导入原始数据
sqoop import \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root --P \--query 'select * from hero where $CONDITIONS' \--target-dir sqoop/hero -m 1
2、增添数据后:
sqoop import \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root --P \--query 'select * from hero where $CONDITIONS' \--incremental append \ --指定增量导入方式--check-column id \ --指定检查列--last-value 6193 \ --上次导入时检查列的最大值--target-dir sqoop/hero -m 1

1.简单导入
sqoop import \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root \--P \--table hero \--hive-import \-m 1
注:org.apache.hadoop.hive.conf.HiveConf. Make sure HIVE_CONF_DIR is set correctly
将hive 里面的lib下的hive-exec-**.jar hive-common-*.jar 放到sqoop 的lib
sqoop import \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root \--P \--table hero \--fields-terminated-by "\t" \--lines-terminated-by "\n" \--hive-import \--hive-overwrite \--create-hive-table \--delete-target-dir \--hive-database test \--hive-table hero
导入过程:
-导入hero表到hdfs的默认路径 (/usr/root/表名)
-通过hdfs导入到hive表中
-自动删除hdfs中数据
2.增量导入
需要清空hive表重新导入 truncate table xxx

eg:普通导入
sqoop import \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root \--P \--table hero \--hbase-table hero \--column-family data \--hbase-row-key id
注:
导入前需要先建表
--hbase-table hbase表名
--column-family 列族名
--hbase-row-key 对应行键

将HDFS中数据导出到关系型数据库中。
要求目标表已经在数据库中
若表为空,则会将操作转换为insert语句
否则转变为update语句
必须确保在导出数据的时候,主键唯一。
sqoop export
参数:
--connect
--username
--P
--table 将HDFS中数据导出到指定表中
--columns<col1,col2> 按照数据顺序,将数据导入到指定列中
--export-dir 指定HDFS的原文件存储路径
--update-key 更新的列
--update-mode 更新模式:updateonly,allowinsert
编译未来
eg:导入 1,xxx,123 (注意数据格式)
sqoop export \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root \--P \--table usr \--export-dir sqoop/data \--columns "id,name,pwd" \-m 1
eg.增量导出 添加 2,yyy,234 后
sqoop export \--connect jdbc:mysql://192.168.56.1:3306/xzdb \--username root \--P \--table usr \--export-dir sqoop/data \--update-key id \--update-mode allowinsert \--columns "id,name,pwd" \-m 1






