Sqoop是许多公司日常使用的业务数据迁移工具,具有多种数据迁移方式,并且支持自定义数据迁移规则,灵活方便,使用时需要根据具体业务的具体需求,配置不同的数据迁移方式
注意:执行如下命令,都需要到sqoop安装目录下(或者配置环境变量也可)
数据导入
一般情况下公司的各种需求有很多,不仅仅是数据迁移工作,需要结合实际业务配置不同的参数进行数据的导入,数据导入有如下多种方式:
全量导入
全量导入会将所有数据都进行迁移
mysql -> HDFS
bin/sqoop import \ # \ 为换行连接符,连接多行参数
--connect jdbc:mysql://linux01:3306/db_test \ # 指定mysql地址
--username root \ # mysql用户名
--password root \ # mysql密码
--table dim_pginfo \ # 迁移的是表dim_pginfo的数据
--target-dir /testdata/dim_pginfo \ # 数据迁移到hdfs中的 /testdata/dim_pginfo目录下
--as-textfile \ # 指定保存文件格式
--fields-terminated-by ',' \ # 指定字段分隔符为 ,
--delete-target-dir \ # 若指定目录已经存在,就删除该目录再重建
--compress \ # 压缩保存的文件
--compression-codec gzip \ # 指定压缩格式
--split-by id \ # 按照id字段划分maptask任务
-m 2 # 指定maptask任务数为 2
可以添加保存为哪种文件格式:
--as-avrodatafile
--as-parquetfile # 指定保存为parquet文件,就不要指定分隔符参数
--as-sequencefile
--as-textfile
若表中不存在数字主键,也可以根据字符串字段划分maptask任务,只是需要添加一个额外的参数
-Dorg.apache.sqoop.splitter.allow_text_splitter=true # 允许text字段作为划分依据
mysql -> hive
bin/sqoop import \
--connect jdbc:mysql://linux01:3306/db_test \ # 源数据库地址
--username root \ # mysql用户名
--password root \ # mysql密码
--table table_test \ # 要迁移的数据表
--hive-import \ # 迁入到hive中
--hive-table hive_db.hive_test \ # 迁入到hive中hive_db库下的hive_test表中
--delete-target-dir \ # 若目标路径存在就删除
--as-textfile \ # 保存为text文件格式
--fields-terminated-by ',' \ # 指定分隔符为 ,
--compress \ # 指定为压缩文件
--compression-codec gzip \ # 指定压缩格式
--null-string '\\N' \ # 空值处理,string类型的字段为null,则换为 \N
--null-non-string '\\N' \ # 非string类型的字段为null,也换为 \N
--hive-overwrite \ # 指定数据插入格式为覆盖
--split-by stu_id \ # 指定按照stu_id字段划分maptask任务数
-m 2 # 指定maptask任务数为 2
实质:
是先将数据从mysql导入hdfs,然后利用hive的元数据操作jar包,去hive的元数据库中生成相应的元数据,并将数据文件导入hive表目录,相当于手动在hive中建表,然后将hdfs中的数据文件load过去
增量导入
增量导入就是将新增的或者更新过的数据进行导入
根据递增字段界定增量数据(导入到hive表中)
bin/sqoop import \
--connect jdbc:mysql://linux01:3306/db_test \ # 源数据库地址
--username root \ # mysql用户名
--password root \ # mysql密码
--table table_test \ # 要迁移的数据表
--hive-import \ # 迁入到hive中
--hive-table hive_db.hive_test \ # 迁入到hive中hive_db库下的hive_test表中
--split-by id \ # 根据id字段进行maptask任务划分
--incremental append \ # 指定增量导入规则
--check-column id \ # 监控id字段的增量数据
--last-value 40 \ # 指定40以后的数据全部为增量数据
-m 2 # 指定maptask个数为2
根据修改时间界定增量数据(导入到hdfs中)
bin/sqoop import \
--connect jdbc:mysql://linux01:3306/db_test \
--username root \
--password root \
--table table_test \
--target-dir '/testdata/hive_test' \
--incremental lastmodified \ # 指定增量导入规则为最后修改数据
--check-column update_time \ # 监控字段 update_time
--last-value '2020-03-18 23:59:59' \ # 指定在 2020-03-18 23:59:59 以后的数据要全部导入
--fields-terminated-by ',' \
-m 1 \
前提:业务表中必须有一个时间字段,且该字段会跟随数据的修改而修改
注意:lastmodified 模式下的增量导入,不支持hive导入
条件导入
where条件导入
bin/sqoop import \
--connect jdbc:mysql://linux01:3306/db_test \
--username root \
--password root \
--table table_test \
--hive-import \
--hive-table hive_db.hive_test \
--delete-target-dir \
--as-textfile \
--fields-terminated-by ',' \
--compress \
--compression-codec gzip \
--split-by stu_id \
--null-string '\\N' \
--null-non-string '\\N' \
--hive-overwrite \
--where "stu_age>25" \
-m 2
column,指定导入字段
bin/sqoop import \
--connect jdbc:mysql://linux01:3306/db_test \
--username root \
--password root \
--table table_test \
--hive-import \
--hive-table hive_db.hive_test \
--delete-target-dir \
--as-textfile \
--fields-terminated-by ',' \
--compress \
--compression-codec gzip \
--split-by stu_id \
--null-string '\\N' \
--null-non-string '\\N' \
--hive-overwrite \
--where "stu_age>25" \
--columns "stu_id,stu_name,stu_phone" \
-m 2
query,导入查询结果
bin/sqoop import \
--connect jdbc:mysql://linux01:3306/db_test \
--username root \
--password root \
--hive-import \
--hive-table hive_db.hive_test \
--as-textfile \
--fields-terminated-by ',' \
--compress \
--compression-codec gzip \
--split-by stu_id \
--null-string '\\N' \
--null-non-string '\\N' \
--hive-overwrite \
--query 'select stu_id,stu_name,stu_age,stu_term from table_test where stu_createtime>"2020-10-01 23:59:59" and stu_sex="1" and $CONDITIONS' \
--target-dir '/user/root/tmp' \
-m 2
注意:
query自由查询导入时,sql语句中必须带 $CONDITIONS条件,$CONDITIONS是一个占位符条件
数据导出
数据导出与数据导入的配置基本一致,只需要更改个别参数的名字即可
普通导出
bin/sqoop export \
--connect jdbc:mysql://linux01:3306/db_test \
--username root \
--password root \
--table table_test2 \
--input-fields-terminated-by ',' \
--export-dir '/user/hive/warehouse/table_test2' \ # 导出hdfs中的/user/hive/warehouse/table_test2中的数据到 db_test.table_test2中
--batch
控制新旧数据导出到mysql
bin/sqoop export \
--connect jdbc:mysql://linux01:3306/db_test \
--username root \
--password root \
--table table_test2 \
--export-dir '/export/' \
--input-null-string '\\N' \
--input-null-non-string '\\N' \
--update-mode allowinsert \ # 设置更新模式
--update-key id \ # 监控 id 字段
--batch
设置更新模式:
--update-mode # 如果选择updateonly,只会对mysql中已存在的id数据进行更新,不存在的id数据不会插入了
--update-mode # 如果选择allowinsert,既会更新已存在id数据,也会插入新的id数据