将原始数据导入mysql
1 选中mysql 运行脚本
2 验证结果
数据存储格式和压缩方案
存储格式
分类
1.行式存储(textFile)
缺点:可读性较好 执行 select * 效率比较高
缺点:耗费磁盘资源 执行 select 字段 效率比较低
2.列式存储(orc)
优点:节省磁盘空间. 执行 select 字段 效率比较高
缺点:执行 select * 效率比较低 , 可读性不是特别好
orc的本质
ORC是兼具行式存储优势又具有列式存储优势, 数据按行分块, 每块中按列存储数据, 同时在每个块内部, 对数据构建索引, 提升查询的效率。
总结
在hive建表中, 一般采用那种存储格式呢?
在hive中, 一般我们的选择都是ORC存储格式,
除非需求对接的数据源是普通文本文件数据, 此时会让对接此文件的表构建为textFile,其余的层次结构的表依然使用ORC。压缩方案
压缩有什么用?答:能够在有限的空间下, 存储更多的数据分类ZIP(GZIP), SNAPPY, LZO,ZLIB ....等snappy:读写效率特别高, 压缩率不是最高(只比 zlib低), 他注重读写效率.zlib: 默认的 读写效率不高, 但是 压缩率高, 相同的内容 压缩后更小.本项目采用哪种方案?在 ODS层, 一般会使用zlib:压缩率高 读写慢 读一次在其他层次中, 一般采用snappy:压缩率比zlib稍微高一些 读写效率高 频繁读写说明1.如果读取次数较少, 写入了较大, 优先保证压缩比 --- zlib(gzip) 比如说 ODS层2.如果读取次数比较高, 优先保障解压缩性能 -- snappy 比如说 DW层相关的表3.如果不清楚, 建议使用snappy, 或者如果空间足够, 统一采用snappy也没有问题创建表的时候, 选择内部表, 还是外部表?
判断标准: 对表数据是否有管理的权限1.有权限删除数据, 那么我们可以构建内部表, 当然也可以构建外部表2.如果没有权限删除数据, 只能构建外部表内部表转换为外部表:alter table 表名 set tblproperties('EXTERNAL'='FALSE'); 通过true和false 来修改是否为内部表还是外部表
结论:一般来说, 在数仓中, 除了ODS层可能会出现外部表以外, 其余的层次结构, 大多数还是内部表.
创建表的时候, 是否需要构建分区表呢?
一般情况下, 都是分区表(分区的字段大多数的都是以时间为主)
解决hive中文注释乱码 重点重点重点
准备工作
drop database if exists db_1 cascade; create database if not exists db_1; create table db_1.tb_user( id int comment '编号', username string comment '用户名', password string comment '密码' ); desc db_1.tb_user;
现象
原因:编码 和 解码 不一致导致的中文乱码
解决
在mysql中执行
use hive; alter table COLUMNS_V2 modify column COMMENT varchar(256) character set utf8; alter table TABLE_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8; alter table PARTITION_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8 ; alter table PARTITION_KEYS modify column PKEY_COMMENT varchar(4000) character set utf8; alter table INDEX_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;
再次测试
数据同步方式
全量覆盖同步方式
应用场景:表数据变更的频次并不多,不需要记录其历史数据 而且整个表数据量相对较少
操作方式
1.每次同步数据, 都是要先将原有的数仓中表数据全部删除, 然后重新从业务端导入即可
2.建表的时候, 不需要构建分区表
仅新增同步方式
应用场景:业务端数据只会有新增的操作, 不会有变更的时候, 数据量比较多
操作方式
1.在数仓中建表的时候, 需要构建分区表, 分区字段和同步数据的周期是一致的,
比如说: 每天都需要同步数据, 分区字段 需要按天 如果每月同步一次, 分区字段按照月.2.每次进行同步的时候, 将对应周期下的新增数据放置到对应日期分区下
新增及更新同步方式
应用场景:1.每天都需要同步数据, 分区字段 需要按天; 如果每月同步一次, 分区字段按照月
2.业务端表数据既有更新操作, 又有新增操作的时候, 而且数据量比较多
操作方式
1.在数仓中建表的时候, 需要构建分区表 , 分区字段和同步数据的周期是一致的,
比如说: 每天都需要同步数据, 分区字段 需要按天 如果每月同步一次, 分区字段按照月.2.每次进行同步的时候, 将对应这个周期的下新增数据和更新数据放置到对应日期分区下即可
全量同步方式
应用场景:业务端数据量不是特别大, 但是也存在更新和新增, 而且不需要保留太多的历史版本
操作方式:
1.在数仓中建表的时候, 需要构建分区表, 分区字段和同步数据的周期是一致的,
比如说: 每天都需要同步数据, 分区字段 需要按天 如果每月同步一次, 分区字段按照月2.每次导入都是导入截止当前时间的全量数据, 定期将历史的日期数据删除即可
在hive中操作ODS层相关的表
说明
ODS层表与 业务库的表保持一致,
也就说, 业务库中有哪些表, 那么我们就需要在ODS层构建有那些表, 表中到的字段也要一致,
额外在HIVE建表的时候, 需要多加一个分区字段, 用于标记数据在何时导入进来的然后判断, 那些表是属于全量覆盖的表, 那些表是属于仅新增的表, 那些表是属于全量及更新的表, 基于各个不同同步方式, 构建对应表即可.
1 创建ods库
路径
-- todo 1 创建库 -- todo 1.1 如果库 yp_ods 存在 就先删除 -- todo 1.2 如果 yp_ods 不存在, 就创建 yp_ods 库
实现
-- todo 1 创建库 -- todo 1.1 如果库 yp_ods 存在 就先删除 drop database if exists yp_ods; -- todo 1.2 如果 yp_ods 不存在, 就创建 yp_ods 库 create database if not exists yp_ods;
2 操作 全量覆盖表
准备增量数据
-- 全量覆盖表的 增量 insert into yipin.t_district value('666','888','xxxx市',555,"yyyy"); select * from yipin.t_district where id='666';
路径
-- todo 2 操作 全量覆盖表 -- todo 2.1 建表 -- todo 2.2 采用全量覆盖导入操作(第一次) -- todo 2.3 导入增量数据 -- todo 2.3.1 先删除表 -- todo 2.3.2 再创建表 -- todo 2.3.3 导入操作
实现
-- todo 2 操作 全量覆盖表 -- todo 2.1 建表 CREATE TABLE yp_ods.t_district ( id string COMMENT '主键ID', code string COMMENT '区域编码', name string COMMENT '区域名称', pid int COMMENT '父级ID', alias string COMMENT '别名' ) comment '区域字典表' row format delimited fields terminated by '\t' stored as orc tblproperties ('orc.compress' = 'ZLIB'); -- todo 2.2 采用全量覆盖导入操作(第一次) sqoop import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select * from yipin.t_district where 1 = 1 and \$CONDITIONS" \ --hcatalog-database 'yp_ods' \ --hcatalog-table 't_district' \ --fields-terminated-by '\t' \ -m 1 select * from yp_ods.t_district; -- todo 2.3 导入增量数据 -- todo 2.3.1 先删除表 drop table if exists yp_ods.t_district; -- todo 2.3.2 再创建表 CREATE TABLE yp_ods.t_district ( id string COMMENT '主键ID', code string COMMENT '区域编码', name string COMMENT '区域名称', pid int COMMENT '父级ID', alias string COMMENT '别名' ) comment '区域字典表' row format delimited fields terminated by '\t' stored as orc tblproperties ('orc.compress' = 'ZLIB'); -- todo 2.3.3 导入操作 sqoop import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select * from yipin.t_district where 1 = 1 and \$CONDITIONS" \ --hcatalog-database 'yp_ods' \ --hcatalog-table 't_district' \ --fields-terminated-by '\t' \ -m 1 select * from yp_ods.t_district;
3 操作 仅新增同步表
准备工作
在mysql中增加增量数据
-- todo 3.3.1 在mysql中 插入增量数据 insert into yipin.t_user_login value('111','a36d197f61ae11e998ec7cd30ad32e2e', null, null, '2022-05-31 08:35:30', '210.42.153.38', null); -- todo 3.2.2 在mysql中 查询上一天的增量数据 select *, '2022-05-31' as dt from yipin.t_user_login where login_time between '2022-05-31 00:00:00' and '2022-05-31 23:59:59';
路径
-- todo 3 操作 仅新增同步表 -- todo 3.1 建表 -- todo 3.2 全量操作 -- todo 3.2.1 如何在mysql中获取今天以前的数据 -- todo 3.2.2 在linux中执行sqoop 导入操作 -- todo 3.3 增量操作 -- todo 3.3.1 在mysql中 插入增量数据 -- todo 3.2.2 在mysql中 查询上一天的增量数据 -- todo 3.2.3 使用sqoop导入上一天的增量数据
实现
-- todo 3 操作 仅新增同步表 -- todo 3.1 创建登录记录表: t_user_login DROP TABLE if exists yp_ods.t_user_login; CREATE TABLE yp_ods.t_user_login ( id string, login_user string, login_type string COMMENT '登录类型(登陆时使用)', client_id string COMMENT '推送标示id(登录、第三方登录、注册、支付回调、给用户推送消息时使用)', login_time string, login_ip string, logout_time string ) COMMENT '用户登录记录表' partitioned by (dt string) row format delimited fields terminated by '\t' stored as orc tblproperties ('orc.compress' = 'ZLIB'); -- todo 3.2 全量操作 -- todo 3.2.1 如何在mysql中获取今天以前的数据 select *, '2022-05-30' as dt from yipin.t_user_login where login_time<='2022-05-30 23:59:59'; -- todo 3.2.2 在linux中执行sqoop 导入操作 sqoop import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select *, '2022-05-30' as dt from yipin.t_user_login where login_time<='2022-05-30 23:59:59' and \$CONDITIONS" \ --hcatalog-database 'yp_ods' \ --hcatalog-table 't_user_login' \ --fields-terminated-by '\t' \ -m 1 select * from yp_ods.t_user_login; -- todo 3.3 增量操作 -- todo 3.3.1 在mysql中 插入增量数据 insert into yipin.t_user_login value('111','a36d197f61ae11e998ec7cd30ad32e2e', null, null, '2022-05-31 08:35:30', '210.42.153.38', null); -- todo 3.2.2 在mysql中 查询上一天的增量数据 select *, '2022-05-31' as dt from yipin.t_user_login where login_time between '2022-05-31 00:00:00' and '2022-05-31 23:59:59'; -- todo 3.2.3 使用sqoop导入上一天的增量数据 sqoop import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select *, '2022-05-31' as dt from yipin.t_user_login where login_time between '2022-05-31 00:00:00' and '2022-05-31 23:59:59' and \$CONDITIONS" \ --hcatalog-database 'yp_ods' \ --hcatalog-table 't_user_login' \ --fields-terminated-by '\t' \ -m 1
4 操作 新增及更新同步表
准备工作
-- todo 4.3.1 在mysql 模拟增量数据 update yipin.t_shop_order set order_state=6, update_time='2022-05-31 12:12:12' where id='dd190227318021f41f'; insert into yipin.t_shop_order value('dd111111f',105,'1bdc27b55dd211e998ec7cd30ad32e2e','cc0202c13a7111e998ec7cd30ad32e2e',3,5,'2022-05-31 17:36:44', '2022-05-31 15:44:44',0,0,0,'SHOP',4,'1bdc27b55dd211e998ec7cd30ad32e2e','2022-05-31 17:36:44','1bdc27b55dd211e998ec7cd30ad32e2e','2022-05-31 15:44:44',1); -- todo 4.3.2 在mysql 查询昨日增量数据 select *, '2022-05-31' as dt from yipin.t_shop_order where create_date between '2022-05-31 00:00:00' and '2022-05-31 23:59:59' or update_time between '2022-05-31 00:00:00' and '2022-05-31 23:59:59'
路径
-- todo 4 操作 新增及更新同步表 -- todo 4.1 建表 -- todo 4.2 全量操作 -- todo 4.2.1 在mysql查询今天以前的数据 -- todo 4.2.2 使用sqoop导入今天以前的数据 -- todo 4.2.3 在hive中验证是否导入成功 -- todo 4.3 增量操作 -- todo 4.3.1 模拟增量数据 -- todo 4.3.2 查询昨日增量数据 -- todo 4.3.3 使用sqoop导入增量数据 -- todo 4.3.4 查询增量数据是否导入成功
实现
-- todo 4 操作 新增及更新同步表 -- todo 4.1 建表: 订单表 t_shop_order DROP TABLE if exists yp_ods.t_shop_order; CREATE TABLE yp_ods.t_shop_order ( id string COMMENT '根据一定规则生成的订单编号', order_num string COMMENT '订单序号', buyer_id string COMMENT '买家的userId', store_id string COMMENT '店铺的id', order_from TINYINT COMMENT '是来自于app还是小程序,或者pc 1.安卓; 2.ios; 3.小程序H5 ; 4.PC', order_state INT COMMENT '订单状态:1.已下单; 2.已付款, 3. 已确认 ;4.配送; 5.已完成; 6.退款;7.已取消', create_date string COMMENT '下单时间', finnshed_time timestamp COMMENT '订单完成时间,当配送员点击确认送达时,进行更新订单完成时间,后期需要根据订单完成时间,进行自动收货以及自动评价', is_settlement TINYINT COMMENT '是否结算;0.待结算订单; 1.已结算订单;', is_delete TINYINT COMMENT '订单评价的状态:0.未删除; 1.已删除;(默认0)', evaluation_state TINYINT COMMENT '订单评价的状态:0.未评价; 1.已评价;(默认0)', way string COMMENT '取货方式:SELF自提;SHOP店铺负责配送', is_stock_up INT COMMENT '是否需要备货 0:不需要 1:需要 2:平台确认备货 3:已完成备货 4平台已经将货物送至店铺 ', create_user string, create_time string, update_user string, update_time string, is_valid TINYINT COMMENT '是否有效 0: false; 1: true; 订单是否有效的标志' ) comment '订单表' partitioned by (dt string) row format delimited fields terminated by '\t' stored as orc tblproperties ('orc.compress' = 'ZLIB'); -- todo 4.2 全量操作 -- todo 4.2.1 在mysql查询今天以前的数据 select *, '2022-05-30' as dt from yipin.t_shop_order where create_date<='2022-05-30 23:59:59' or create_date<='2022-05-30 23:59:59' -- todo 4.2.2 使用sqoop导入今天以前的数据 sqoop import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select *, '2022-05-30' as dt from t_shop_order where create_time <= '2022-05-30 23:59:59' OR update_time <= '2022-05-30 23:59:59' and \$CONDITIONS" \ --hcatalog-database 'yp_ods' \ --hcatalog-table 't_shop_order' \ --fields-terminated-by '\t' \ -m 1 -- todo 4.2.3 在hive中验证是否导入成功 select * from yp_ods.t_shop_order; -- todo 4.3 增量操作 -- todo 4.3.1 在mysql 模拟增量数据 update yipin.t_shop_order set order_state=6, update_time='2022-05-31 12:12:12' where id='dd190227318021f41f'; insert into yipin.t_shop_order value('dd111111f',105,'1bdc27b55dd211e998ec7cd30ad32e2e','cc0202c13a7111e998ec7cd30ad32e2e',3,5,'2022-05-31 17:36:44', '2022-05-31 15:44:44',0,0,0,'SHOP',4,'1bdc27b55dd211e998ec7cd30ad32e2e','2022-05-31 17:36:44','1bdc27b55dd211e998ec7cd30ad32e2e','2022-05-31 15:44:44',1); -- todo 4.3.2 在mysql 查询昨日增量数据 select *, '2022-05-31' as dt from yipin.t_shop_order where create_date between '2022-05-31 00:00:00' and '2022-05-31 23:59:59' or update_time between '2022-05-31 00:00:00' and '2022-05-31 23:59:59' -- todo 4.3.3 使用sqoop导入增量数据 sqoop import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select *, '2022-05-31' as dt from yipin.t_shop_order where create_date between '2022-05-31 00:00:00' and '2022-05-31 23:59:59' or update_time between '2022-05-31 00:00:00' and '2022-05-31 23:59:59' and \$CONDITIONS" \ --hcatalog-database 'yp_ods' \ --hcatalog-table 't_shop_order' \ --fields-terminated-by '\t' \ -m 1 -- todo 4.3.4 查询增量数据是否导入成功 select * from yp_ods.t_shop_order where dt='2022-05-31';
5 编写shell脚本
问题: 目前sqoop命令是特别死板的
因为: 当中日期本应该是一个变量, 随着时间的推移, 每天都应该自动指向上一天的日期数据,
但是目前都是写死了, 每天都需要手动的修改, 这种方式, 并不是我们想要的吧.....解决:
1.希望除了能够自动获取上一天的日期, 还能支持根据指定的日期导入相关的数据
2.基于 SHELL脚本来实现, 后续将shell脚本通过oozie完成自动化调度操作
1 如何shell脚本中获取对应的日期?
如何在linux中获取上一天的日期?
路径
获取当前日期 获取上一小时日期 获取上一天日期 获取上一周日期 获取上一月日期
实现
date date -d '-1 hour' date -d '-1 day' date -d '-1 week' date -d '-1 month'
如何在linux中按照特定格式输出日期?
路径
-- todo 1 将当前日期以 2022-10-01 格式输出 -- todo 2 将昨天以 2022-10-01 格式输出 -- todo 3 将当前日期以 2022-10-01 10:02:03 格式输出 -- todo 4 将指定格式日期 交给变量a
实现
-- todo 1 将当前日期以 2022-10-01 格式输出 date +'%Y-%m-%d' -- todo 2 将昨天以 2022-10-01 格式输出 date -d '-1 day' +'%Y-%m-%d' -- todo 3 将当前日期以 2022-10-01 10:02:03 格式输出 date -d '-1 day' +'%Y-%m-%d %H:%M:%S' -- todo 4 将指定格式日期 交给变量a d1=`date -d '-1 day' +'%Y-%m-%d %H:%M:%S'` echo $d1
在shell脚本中输出昨天的日期
实现
#!/bin/bash if [ $# == 1 ] then d1=$1 else d1=`date -d '-1 day' +"%Y-%m-%d"` fi echo $d1
在linux中执行sql脚本?
实现
# 1 显示所有库 # 2 等第一个执行完 # 3 实现指定库的所有表
在shell脚本中执行 hive的hql语句?
路径
#!/bin/bash HIVE_HOME=/usr/bin/hive # 1 显示所有库 ${HIVE_HOME} -S -e 'show databases;' # 2 等第一个执行完 wait # 3 实现指定库的所有表 ${HIVE_HOME} -S -e 'show tables in yp_ods;'
实现
# 1 显示所有库 # 2 等第一个执行完 # 3 实现指定库的所有表
2 执行全量脚本
路径
# 1 声明日期变量 # 1.1 如果没有传参数, 就是昨天 # 1.2 如果传递参数,就是参数值 # 2 声明sqoop路径变量 # 3 声明hive路径变量 # 4 执行全量操作 # 4.1 执行建表语句 # 4.2 执行插入全量操作
实现
#!/bin/bash # 1 声明日期变量 # 1.1 如果没有传参数, 就是昨天 # 1.2 如果传递参数,就是参数值 if [ $# == 0 ];then TD_DATE=`date -d '-1 day' +"%Y-%m-%d"` else TD_DATE=$1 fi # 2 声明sqoop路径变量 SQOOP_HOME=/usr/bin/sqoop # 3 声明hive路径变量 HIVE_HOME=/usr/bin/hive # 4 执行全量操作 # 4.1 执行建表语句 ${HIVE_HOME} -S -e " drop database if exists yp_ods_2; create database if not exists yp_ods_2; DROP TABLE if exists yp_ods_2.t_district; CREATE TABLE if not exists yp_ods_2.t_district ( id string COMMENT '主键ID', code string COMMENT '区域编码', name string COMMENT '区域名称', pid int COMMENT '父级ID', alias string COMMENT '别名' ) comment '区域字典表' row format delimited fields terminated by '\t' stored as orc tblproperties ('orc.compress' = 'ZLIB'); DROP TABLE if exists yp_ods_2.t_user_login; CREATE TABLE yp_ods_2.t_user_login ( id string, login_user string, login_type string COMMENT '登录类型(登陆时使用)', client_id string COMMENT '推送标示id(登录、第三方登录、注册、支付回调、给用户推送消息时使用)', login_time string, login_ip string, logout_time string ) COMMENT '用户登录记录表' partitioned by (dt string) row format delimited fields terminated by '\t' stored as orc tblproperties ('orc.compress' = 'ZLIB'); DROP TABLE if exists yp_ods_2.t_shop_order; CREATE TABLE yp_ods_2.t_shop_order ( id string COMMENT '根据一定规则生成的订单编号', order_num string COMMENT '订单序号', buyer_id string COMMENT '买家的userId', store_id string COMMENT '店铺的id', order_from TINYINT COMMENT '是来自于app还是小程序,或者pc 1.安卓; 2.ios; 3.小程序H5 ; 4.PC', order_state INT COMMENT '订单状态:1.已下单; 2.已付款, 3. 已确认 ;4.配送; 5.已完成; 6.退款;7.已取消', create_date string COMMENT '下单时间', finnshed_time timestamp COMMENT '订单完成时间,当配送员点击确认送达时,进行更新订单完成时间,后期需要根据订单完成时间,进行自动收货以及自动评价', is_settlement TINYINT COMMENT '是否结算;0.待结算订单; 1.已结算订单;', is_delete TINYINT COMMENT '订单评价的状态:0.未删除; 1.已删除;(默认0)', evaluation_state TINYINT COMMENT '订单评价的状态:0.未评价; 1.已评价;(默认0)', way string COMMENT '取货方式:SELF自提;SHOP店铺负责配送', is_stock_up INT COMMENT '是否需要备货 0:不需要 1:需要 2:平台确认备货 3:已完成备货 4平台已经将货物送至店铺 ', create_user string, create_time string, update_user string, update_time string, is_valid TINYINT COMMENT '是否有效 0: false; 1: true; 订单是否有效的标志' ) comment '订单表' partitioned by (dt string) row format delimited fields terminated by '\t' stored as orc tblproperties ('orc.compress' = 'ZLIB'); " wait # 4.2 执行插入全量操作 ${SQOOP_HOME} import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select * from yipin.t_district where 1 = 1 and \$CONDITIONS" \ --hcatalog-database 'yp_ods_2' \ --hcatalog-table 't_district' \ --fields-terminated-by '\t' \ -m 1 wait ${SQOOP_HOME} import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select *, '${TD_DATE}' as dt from yipin.t_user_login where login_time<='${TD_DATE} 23:59:59' and \$CONDITIONS" \ --hcatalog-database 'yp_ods_2' \ --hcatalog-table 't_user_login' \ --fields-terminated-by '\t' \ -m 1 wait ${SQOOP_HOME} import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select *, '${TD_DATE}' as dt from t_shop_order where create_time <= '${TD_DATE} 23:59:59' OR update_time <= '${TD_DATE} 23:59:59' and \$CONDITIONS" \ --hcatalog-database 'yp_ods_2' \ --hcatalog-table 't_shop_order' \ --fields-terminated-by '\t' \ -m 1
3 执行增量脚本
路径
# 1 声明日期变量 # 1.1 如果没有传参数, 就是昨天 # 1.2 如果传递参数,就是参数值 # 2 声明sqoop路径变量 # 3 声明hive路径变量 # 4 执行增量操作 # 4.1 操作全量覆盖表的增量 # 4.2 操作仅新增表的增量 # 4.3 操作新增或修改表的增量
实现
#!/bin/bash # 1 声明日期变量 # 1.1 如果没有传参数, 就是昨天 # 1.2 如果传递参数,就是参数值 if [ $# == 0 ];then TD_DATE=`date -d '-1 day' +"%Y-%m-%d"` else TD_DATE=$1 fi # 2 声明sqoop路径变量 SQOOP_HOME=/usr/bin/sqoop # 3 声明hive路径变量 HIVE_HOME=/usr/bin/hive # 4 执行增量操作 # 4.1 操作全量覆盖表的增量 ${HIVE_HOME} -S -e " drop table if exists yp_ods_2.t_district; CREATE TABLE yp_ods_2.t_district ( id string COMMENT '主键ID', code string COMMENT '区域编码', name string COMMENT '区域名称', pid int COMMENT '父级ID', alias string COMMENT '别名' ) comment '区域字典表' row format delimited fields terminated by '\t' stored as orc tblproperties ('orc.compress' = 'ZLIB'); " ${SQOOP_HOME} import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select * from yipin_2.t_district where 1 = 1 and \$CONDITIONS" \ --hcatalog-database 'yp_ods_2' \ --hcatalog-table 't_district' \ --fields-terminated-by '\t' \ -m 1 # 4.2 操作仅新增表的增量 ${SQOOP_HOME} import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select *, '${TD_DATE}' as dt from yipin.t_user_login where login_time between '${TD_DATE} 00:00:00' and '${TD_DATE} 23:59:59' and \$CONDITIONS" \ --hcatalog-database 'yp_ods_2' \ --hcatalog-table 't_user_login' \ --fields-terminated-by '\t' \ -m 1 # 4.3 操作新增或修改表的增量 ${SQOOP_HOME} import \ --connect jdbc:mysql://hadoop01:3306/yipin \ --username root \ --password 123456 \ --query "select *, '${TD_DATE}' as dt from yipin.t_shop_order where create_date between '${TD_DATE} 00:00:00' and '${TD_DATE} 23:59:59' or update_time between '${TD_DATE} 00:00:00' and '${TD_DATE} 23:59:59' and \$CONDITIONS" \ --hcatalog-database 'yp_ods_2' \ --hcatalog-table 't_shop_order' \ --fields-terminated-by '\t' \ -m 1