Sqoop应用_导入Hive

4.5 全量导入hive表

4.5.1 导入文本表

# 导入命令
sqoop import \
--connect jdbc:mysql://nn1:3306/sqoop_db"?useUnicode=true&characterEncoding=UTF-8" \
--username root \
--password 12345678 \
--table goods_table \
--num-mappers 1 \
--delete-target-dir \
--hive-import \
--fields-terminated-by "\001" \
--hive-overwrite \
--hive-table hainiu.goods_table

上面过程分为两步:

1)第一步将数据导入到HDFS,默认的临时目录是/user/当前操作用户/mysql表名;

2)第二步将导入到HDFS的数据迁移到Hive表,如果hive表不存在,sqoop会自动创建内部表;(我们的是在/user/hainiu/goods_table,通过查看job的configuration的outputdir属性得知)

file

结果:

file

查询数据:

file

4.6 增量数据导入

现在我们已经实现了 hive的数据导入方式,那么我们怎么实现hive的增量数据导入呢?

1、append方式

2、lastmodified方式,必须要加–append(追加)或者–merge-key(合并,一般填主键)

file

我们先建新表进行增量数据的演示

CREATE TABLE `goods_update_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_sn` varchar(50) COLLATE utf8_bin NOT NULL COMMENT '商品的唯一编号、货号',
  `goods_cname` varchar(100) COLLATE utf8_bin NOT NULL COMMENT '商品名称(中文)',
  `goods_ename` varchar(100) COLLATE utf8_bin DEFAULT NULL COMMENT '商品名称(英文)',
  `goods_price` double NOT NULL COMMENT '商品价格',
  `last_update_time` datetime NOT NULL DEFAULT now()COMMENT '最近一次更新商品配置的时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- 添加数据
INSERT INTO `goods_update_table` (goods_sn,goods_cname,goods_ename,goods_price,last_update_time) 
VALUES ('111111', '漂亮的高跟鞋1', '', 888, '2020-10-10 11:00:00');

4.6.1 全量导入

sqoop import \
--connect jdbc:mysql://nn1:3306/sqoop_db"?useUnicode=true&characterEncoding=UTF-8" \
--username root \
--password 12345678 \
--table goods_update_table \
--num-mappers 1 \
--delete-target-dir \
--hive-import \
--fields-terminated-by "\001" \
--hive-overwrite \
--hive-table hainiu.goods_update_table

file

由于 --hive import 与 incremental 冲突, 所以增量导入不能直接导入到hive表中,但可以导入到hive表对应的hdfs目录里

4.6.2 按照id增量导入

incremental append 用法

-- MySQL添加一条新的数据
INSERT INTO `goods_update_table` (goods_sn,goods_cname,goods_ename,goods_price,last_update_time) VALUES 
('222222', '漂亮的长筒靴1', '', 999, '2020-10-10 12:00:00');

-- 按照id增量导入
sqoop import \
--connect jdbc:mysql://nn1:3306/sqoop_db"?useUnicode=true&characterEncoding=UTF-8" \
--username root \
--password 12345678 \
--table goods_update_table \
--num-mappers 1 \
--target-dir /hive/warehouse/hainiu.db/goods_update_table \
--fields-terminated-by "\001" \
--incremental append \
--check-column id \
--last-value 1

-- 参数解释:
-- 1)incremental <mode> : append或lastmodified,使用lastmodified方式导入数据要指定增量数据是要--append(追加)还是要--merge-key(合并) 
-- 2)check-column <字段> : 作为增量导入判断的列名
-- 3)last-value val :  指定某一个值,用于标记增量导入的位置,这个值的数据不会被导入到表中,只用于标记当前表中最后的值。

file

4.6.3 按照时间增量导入

–incremental lastmodified --append 用法

如果按照时间增量进行数据导入可以使用 --incremental lastmodified --append 这种方式进行数据导入,lastmodified 用于更新的日期列

file

INSERT INTO `goods_update_table` (goods_sn,goods_cname,goods_ename,goods_price,last_update_time) VALUES 
('333333', '漂亮的长筒靴2', '', 999, '2020-10-10 13:00:00');

sqoop import \
--connect jdbc:mysql://nn1:3306/sqoop_db"?useUnicode=true&characterEncoding=UTF-8" \
--username root \
--password 12345678 \
--table goods_update_table \
--num-mappers 1 \
--target-dir /hive/warehouse/hainiu.db/goods_update_table \
--fields-terminated-by "\001" \
--incremental lastmodified \
--check-column last_update_time \
--last-value '2020-10-10 13:00:00' \
--append

-- 注意:last-value 的设置是把包括 2020-10-10 13:00:00 时间的数据做增量导入。

结果:id=3的数据成功导入

file

4.6.3 按照时间增量并按照主键合并导入

–incremental lastmodified --merge-key 用法

如果之前的数据有修改的话可以使用–incremental lastmodified --merge-key进行数据合并执行修改的SQL

-- 更改商品价格
update goods_update_table set goods_price=666 where id=3;

进行合并导入

sqoop import \
--connect jdbc:mysql://nn1:3306/sqoop_db"?useUnicode=true&characterEncoding=UTF-8" \
--username root \
--password 12345678 \
--table goods_update_table \
--num-mappers 1 \
--target-dir /hive/warehouse/hainiu.db/goods_update_table \
--fields-terminated-by "\001" \
--incremental lastmodified \
--check-column last_update_time \
--last-value '2020-10-10 13:00:00' \
--merge-key id

-- --incremental lastmodified  --merge-key的作用:修改过的数据和新增的数据(前提是满足last-value的条件)都会导入进来,并且重复的数据(不需要满足last-value的条件)都会进行合并 

lue ‘2020-10-10 13:00:00’
–merge-key id

– --incremental lastmodified --merge-key的作用:修改过的数据和新增的数据(前提是满足last-value的条件)都会导入进来,并且重复的数据(不需要满足last-value的条件)都会进行合并


![file](https://img-blog.csdnimg.cn/img_convert/9af2a4423338ebe6e9c41e30270f8fe5.png)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值