hive mysql互导_用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 "jdbc:hive2://dsrv2.heracles.sohuno.com:10000/test;principal=hive/dsrv2.heracles.sohuno.com@HERACLE.SOHUNO.COM;"

--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 "jdbc:hive2://dsrv2.heracles.sohuno.com:10000/test;principal=hive/dsrv2.heracles.sohuno.com@HERACLE.SOHUNO.COM;"

--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

增量导入成功!

原文出处:https://www.cnblogs.com/xiaodf/p/10712766.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值