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

增量导入成功!

用Sqoop进行Hive和MySQL之间的数据互导的更多相关文章

  1. 解决kettle在两个mysql之间迁移数据时乱码的问题 和 相关报错 及参数调整, 速度优化

    1. 乱码问题 编辑目标数据库的链接: 配置编码参数即可. 2. 报错 No operations allowed after statement closed. 需要调整wait_timeout:  ...

  2. <关于数据仓库>基于docker的Mysql与Hadoop/Hive之间的数据转移 (使用Apache Sqoop™)

    原创博客,转载请联系博主! 摘要:本文介绍了如何使用docker快速搭建一个可以从外部访问的mysql服务容器,和由docker搭建的分布式Hadoop文件系统,并且使用ApacheSqoop完成将m ...

  3. sqoop从hive导入数据到mysql时出现主键冲突

    今天在将一个hive数仓表导出到mysql数据库时出现进度条一直维持在95%一段时间后提示失败的情况,搞了好久才解决.使用的环境是HUE中的Oozie的workflow任何调用sqoop命令,该死的o ...

  4. ETL数据从sqlserver到mysql之间迁移

    因近期需要进行sqlserver数据到mysql之间的数据同步.偶然之间发现了这一款工具ELK 一.下载 1.Kettle可以在http://kettle.pentaho.org/网站下载 2.下载的 ...

  5. Hadoop Hive概念学习系列之HDFS、Hive、MySQL、Sqoop之间的数据导入导出(强烈建议去看)

    Hive总结(七)Hive四种数据导入方式 (强烈建议去看) Hive几种数据导出方式 https://www.iteblog.com/archives/955 (强烈建议去看) 把MySQL里的数据 ...

  6. 如何利用sqoop将hive数据导入导出数据到mysql

    运行环境  centos 5.6   hadoop  hive sqoop是让hadoop技术支持的clouder公司开发的一个在关系数据库和hdfs,hive之间数据导入导出的一个工具. 上海尚学堂 ...

  7. hive、sqoop、MySQL间的数据传递

    hdfs到MySQL csv/txt文件到hdfs MySQL到hdfs  hive与hdfs的映射: drop table if exists emp;create table emp ( id i ...

  8. sqoop与mysql之间中文乱码

    sudo -u hive sqoop export --connect "jdbc:mysql://192.168.22.201/LauncherDB?useUnicode=true& ...

  9. Sqoop export(Hive to MySQL) 的一些 reference

    之后可能会整理成文章..还有一些坑没趟完. Reference: https://cloud.tencent.com/developer/article/1078473  Sqoop抽取Hive Pa ...

随机推荐

  1. 浏览器如何对HTML5的离线储存资源进行管理和加载

    在线的情况下,浏览器发现html头部有manifest属性,它会请求manifest文件,如果是第一次访问app,那么浏览器就会根据manifest文件的内容下载相应的资源并且进行离线存储.如果已经访 ...

  2. maven到Gradle,一些对比和分享

    Gradle作为一款基于Groovy语言的构建工具,已经吸引众多的ant,maven使用者转投gradle的怀抱,和Gradle相比,ant显得冗余复杂,maven显得有些死板落后,而gradle基于 ...

  3. 数据分析---SQL(删除数据或表)

    一.SQL中的语法 1.drop table 表名称                         eg: drop table  dbo.Sys_Test   2.truncate table 表 ...

  4. 给出一百分制成绩,要求输出成绩等级’A’、’B’、’C’、’D’、’E’。

    import java.util.Scanner; public class test5 { public static void main(String[] args) { // TODO Auto ...

  5. test--2

    $(function () {   if (isLogined && !c_has_follwed) {   $.ajax({   url: "/mvc/Follow/Fol ...

  6. react-native-router-flux

    这是一个路由,可以用来做Android底部的导航栏,学Android的都知道,如果用原生的代码来 做导航栏,会很复杂,关系到很多复杂的知识. 接下来我就简单的说明一下如何插入和使用吧: 1.你要先依赖 ...

  7. 常见IO模型

    在socket的通信中,recv,accept,recvfrom(UDP协议接收信息)这些阶段由于需要收到信息,才能继续下面的代码,所以这些阶段叫做阻塞,类似于我们python变成中的input函数, ...

  8. 使用vagrant构建你们团队的开发环境

    vagrant可以让团队快速搭建统一的开发环境. 搭建vagrant你需要准备三个东西: 1.vagrant安装包 . 2.virtualbox安装包. 3.打包后的vagrant虚拟环境镜像 (ln ...

  9. this的学习

    面试经常被问到this的问题,每次回答都感觉回答的不尽人意,今天周六就在家好好研究this的问题 1.function定义的时候this的指向是无法确定的,执行function的时候才知道this的指 ...

  10. 猪懂傻改之《powershell 代码规范》

    猪懂傻改之<powershell 代码规范> 脚本程序员或许都经历过这样的场景:接手别人的代码时,因为没有注释,变量名五花八门,模块之间逻辑关系如麻,弄得满头雾水,一脸茫然,痛定思痛之后不 ...