用Sqoop进行Hive和MySQL之间的数据互导
Hive导数据入MySQL
创建mysql表
use anticheat;
create table anticheat_blacklist(
userid varchar(30) primary key ,
dt int,
update_time timestamp,
delete_flag int,
operator varchar(30)
);
全量导出
用sqoop export全量导出hive表数据入mysql,具体命令如下:
sqoop export -D mapred.job.queue.name=datacenter
--connect jdbc:mysql://localhost:3306/anticheat?tinyInt1isBit=false
--username root
--password ^qn9DFYPm
--table anticheat_blacklist
--input-fields-terminated-by '\t'
--input-null-string '\\N'
--input-null-non-string '\\N'
--num-mappers 10
--export-dir hdfs://dc5/user/test/hive/online/anticheat_blacklist_mysql
增量导出
sqoop export -D mapred.job.queue.name=datacenter
--connect jdbc:mysql://localhost:3306/anticheat?tinyInt1isBit=false
--username root
--password ^qn9DFYPm
--table anticheat_blacklist2
--input-fields-terminated-by '\t'
--input-null-string '\\N'
--input-null-non-string '\\N'
--num-mappers 10
--update-key update_time
--update-mode allowinsert
--export-dir hdfs://dc5/user/test/hive/online/anticheat_blacklist_mysql2
MySQL导数据入Hive
创建Hive表
创建同步mysql表的hive表
CREATE TABLE test.anticheat_blacklist_mysql(
key string,
dt int,
update_time timestamp,
delete_flag int,
operator string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE
LOCATION 'hdfs://dc5/user/test/hive/online/anticheat_blacklist_mysql';
全量导入
用sqoop import全量导出mysql表数据入hive表,具体命令如下:
sqoop import -D mapred.job.queue.name=datacenter
--connect jdbc:mysql://localhost:3306/anticheat?tinyInt1isBit=false
--username root
--password ^qn9DFYPm
--table anticheat_blacklist
--delete-target-dir
--beeline ""
--hive-import --fields-terminated-by '\t'
--hive-database test
--hive-table anticheat_blacklist_mysql
--null-string '\\N'
--null-non-string '\\N'
--hive-overwrite
--num-mappers 1
--outdir /home/test/data/anticheat/mysql2hive
null字符串转为NULL,添加下面两条参数可以实现:
- –null-string
如果指定列为字符串类型,使用指定字符串替换值为null的该类列的值 - –null-non-string
如果指定列为非字符串类型,使用指定字符串替换值为null的该类列的值
增量导入
增量导入:(根据时间来导入,如果表中没有时间属性,可以增加一列时间簇)
核心参数:
- –check-column 用来指定一些列,这些列在增量导入时用来检查这些数据是否作为增量数据进行导入,和关系型数据库中的自增字段及时间戳类似. 注意:这些被指定的列的类型不能使任意字符类型(在关系数据库中),如char、varchar等类型都是不可以的,同时–check-column可以去指定多个列
- –incremental 用来指定增量导入的模式,两种模式分别为Append和Lastmodified
- –last-value 指定上一次导入中检查列指定字段最大值,即会导入比lastvalue指定值大的数据记录
注意:上面三个参数都必须添加!
执行语句:
sqoop import -D mapred.job.queue.name=datacenter
--connect jdbc:mysql://localhost:3306/anticheat?tinyInt1isBit=false
--username root
--password ^qn9DFYPm
--table anticheat_blacklist
--delete-target-dir
--hive-import --fields-terminated-by '\t'
--beeline ""
--hive-database test
--hive-table anticheat_blacklist_mysql
--null-string '\\N'
--hive-overwrite
--num-mappers 1
--check-column update_time
--incremental lastmodified
--last-value "2019-04-12 14:31:34"
--outdir /home/test/data/anticheat/mysql2hive
以上语句使用 lastmodified 模式进行增量导入,结果报错:
错误信息:--incremental lastmodified option for hive imports is not supported. Please remove the parameter --incremental lastmodified
错误原因:Sqoop 不支持 mysql转hive时使用 lastmodified 模式进行增量导入,但mysql转HDFS时可以支持该方式!
我们使用append方式导入:
sqoop import -D mapred.job.queue.name=datacenter
--connect jdbc:mysql://localhost:3306/anticheat?tinyInt1isBit=false
--username root
--password ^qn9DFYPm
--table anticheat_blacklist
--delete-target-dir
--hive-import --fields-terminated-by '\t'
--hive-database test
--hive-table anticheat_blacklist_mysql
--null-string '\\N'
--null-non-string '\\N'
--num-mappers 1
--check-column update_time
--incremental append
--last-value "2019-04-12 14:31:34"
--outdir /home/test/data/anticheat/mysql2hive
增量导入成功!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?