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

 
  • 相关阅读:
    postgresql 简单入门
    自用 docker-compose
    前后端分离 开发环境通过CORS实现跨域联调
    导出 java.io.IOException: 权限不够
    mysql 数值与字符类型 长度梳理
    easyui input未设id导致的问题
    springmvc处理url请求步骤
    js中的变量提升(hoisting)
    auto semicolon insertion 自动分号补齐的坑
    $.ajax dataType设置为json 回调函数不执行
  • 原文地址:https://www.cnblogs.com/javalinux/p/14993809.html
  • 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 '	' 
    --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 '	' 
    --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 '	'
    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 '	' 
    --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 '	' 
    --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 '	' 
    --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