sqoonp简单使用

1、sqoop简介

sqoop是一款开源ETL工具,主要用于Hadoop(hive)与传统数据库(mysql,postgresql,oracle…)之间进行数据的传递。

2、sqoop操作

2.1 导入:从非大数据集群向大数据集群中传输数据叫导入

(1)全量导入HDFS
从sql全量导入到指定HDFS路径

sqoop import \ --sqoop调用sqoop;import导入参数
--connect jdbc:mysql://192.168.5.100:3306/lee \ --connect使用jdbc连接关系型数据库;参数内容为jdbc连接的url
--username root \ --username 关系型数据库的登录用户名
--password root \ --password 关系型数据库的登录用户密码
--table t_chongzhi \ --table 需要导入的关系型数据库中的表
--num-mappers 1 \ --指定sqoop使用map数量(设置为8,当成系统变量,可以导入使用,依据集群资源和脚本数量而设置的)
--fields-terminated-by "," \ --导入HDFS的文件分隔符
--target-dir /shengtu/tc \ --导入HDFS的目录
--delete-target-dir --HDFS中若存在目标目录则先删除该目录再新建并导入,若不存在则直接新建导入(相当于全量更新)

(2)查询导入HDFS
查询sql表导入到HDFS指定路径

sqoop import \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--query 'select id,ename,amount,dt from t_chongzhi where id > 1000 and $CONDITIONS;' \ --query 在关系型数据库中查询数据源
--num-mappers 1 \
--fields-terminated-by "," \
--target-dir /shengtu/tc \
--delete-target-dir

(3)指定列导入到HDFS
选择sql指定列的表导入到HDFS指定路径

sqoop import \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \	
--password root \
--table t_chongzhi \
--columns id,ename \ --columns 筛选关系型数据库数据源的列
--num-mappers 1 \
--fields-terminated-by "," \
--target-dir /shengtu/tc \
--delete-target-dir

(4)关键字筛选查询导入HDFS
从SQL筛选指定数据的表导入到HDFS指定路径

sqoop import \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--table t_chongzhi \
--where "id>1003" \ --where 使用参数方法筛选关系型数据库数据源的行
--num-mappers 1 \
--fields-terminated-by "," \
--target-dir /shengtu/tc \
--delete-target-dir

2.2增量导入HDFS

append

sqoop import \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--table t_chongzhi \
--incremental append \ --incremental指定增量更新方式(append或lastmodified)
--check-column id \ --check-column指需要依据关系型数据库数据源的哪个字段进行更新
--last-value 1003 \ --last-value上次更新的最后一个值,本次更新需要大于该值。
--num-mappers 1 \
--fields-terminated-by "," \
--target-dir /shengtu/tc

(修改表):默认:CURRENT_TIMESTAMP
额外的:on update CURRENT_TIMESTAMP
ALTER TABLE lee.t_chongzhi ADD update_t TIMESTAMP DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL;

(2)lastmodified

sqoop import \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--table t_chongzhi \
--incremental lastmodified \ --incremental指定增量更新方式(append或lastmodified)
--check-column update_t \  --check-column指需要依据关系型数据库数据源的哪个字段进行更新
--last-value '2022-08-26 18:40:38.1' \ --last-value要比上次更新的最后一个值大,本次更新需要大于该值。
--num-mappers 1 \
--append  --使用lastmodified方式需增加该参数。
--fields-terminated-by "," \
--target-dir /shengtu/tc

(3)append方式

sqoop import \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--table t_chongzhi \
--incremental append \
--check-column update_t \
--last-value '2022-07-29 12:54:12.1' \
--num-mappers 1 \
--fields-terminated-by "," \
--target-dir /shengtu/tc

2.3导入hive表的两种形式

(1)指定hive中表的HDFS路径来导入/user/hive/warehouse/test.db/t_chongzhi2/day=20220728
这里需要先建立该hive表的该分区,才能在hive中查询到数据

hive -e "alter table test.t_chongzhi2 add partition(day='20220728');"
sqoop import \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--table t_chongzhi \
--fields-terminated-by "," \
--target-dir /user/hive/warehouse/test.db/t_chongzhi2/day=20220728 \  --该路径为新创建的hive表的分区路径
--num-mappers 1 \
--incremental append \
--check-column update_t \
--last-value '2022-07-29 12:54:12.1'

(2)调用–hive参数直接向hive表中导入数据(外部导入)

sqoop import \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--table t_chongzhi \
--num-mappers 1 \
--incremental append \
--check-column update_t \
--last-value '2022-07-29 12:54:12.1' \	
--fields-terminated-by "," \
--hive-import \  --指定向hive导入
--hive-table test.t_chongzhi2 \  --指定向hive中哪张表导入数据
--hive-partition-key day \ --指定hive表的分区字段
--hive-partition-value 20230606  --指定导入哪个分区

最常用导入,使用–query与–hive-import将数据导入hive表

sqoop import \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--query 'select id,ename,amount,dt,update_t from t_chongzhi where id > 1000 and $CONDITIONS;' \
--num-mappers 1 \
--fields-terminated-by "," \
--target-dir /shengtu/t_chongzhi \
--delete-target-dir \
--hive-import \
--hive-table test.t_chongzhi2 \
--hive-partition-key day \
--hive-partition-value 20230607 \
--hive-overwrite

2.4导出:从HDFS将数据导出至关系型数据库(MySQL)

2.4.1全量导出
sqoop export \ --sqoop调用sqoop;export导出参数
--connect jdbc:mysql://192.168.5.100:3306/lee \ --connect使用jdbc连接关系型数据库;参数内容为jdbc连接的url
--username root \
--password root \
--table t_chongzhi_export \  --table 需要导出到关系型数据库中的哪张表
--input-fields-terminated-by "," \
--export-dir /user/hive/warehouse/test.db/t_chongzhi2/day=20230607 --需要导出的HDFS上数据的目录
2.4.2 --update-mode:updateonly(仅更新已有行),allowinsert(若该行已存在则更新,行不存在即插入)

注:allowinsert当update-key为主键时有效,否则均为插入。

sqoop export \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--table t_chongzhi_export \
--input-fields-terminated-by "," \
--export-dir /user/hive/warehouse/test.db/t_chongzhi2/day=20220726 \  --hive做中间转换,此处拿的是hive的表
--update-key id \
--update-mode updateonly --仅修改不能新增新列的数据

updateonly验证步骤:
1.在MySQL数据库的名为lee的database中右键点击名为t_chongzhi的表==>复制==>仅结构,后改名为t_chongzhi_export;
2.导出(export)hive表t_chongzhi2的分区day='20220729’中的数据到MySQL中的t_chongzhi_export表;
3.在t_chongzhi中修改一条数据,并且新增一条数据;
4.将3中新增和修改的数据增量导入(append或者lastmodified)到hive表t_chongzhi2的分区day='20220726’中;
5.将hive表t_chongzhi2的分区day='20220726’数据导出(export使用updateonly)导出至MySQL表t_chongzhi_export中

allowinsert(若update-key非主键,则仅插入,不更新已有行) 不加主键的话数据会全部插入进去

sqoop export \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--table t_chongzhi_export \
--input-fields-terminated-by "," \
--export-dir /user/hive/warehouse/test.db/t_chongzhi2/day=20220726 \
--update-key id \
--update-mode allowinsert
2.4.3使用hcatalog工具从hive表导出至mysql

1.配置环境变量

编辑~/.bashrc文件,在文件末尾添加环境变量:
vim ~/.bashrc
export HCAT_HOME=/opt/module/apache-hive-2.1.1-bin/hcatalog
export PATH=$PATH:$HCAT_HOME/bin
使环境变量生效:
source ~/.bashrc

2.使用hcatalog

sqoop export \
--connect jdbc:mysql://192.168.5.100:3306/lee \
--username root \
--password root \
--table t_chongzhi_export \
--hcatalog-database test \
--hcatalog-table t_chongzhi2 \
--hcatalog-partition-keys day \
--hcatalog-partition-values 20220827
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值