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. labview web发布局域网内访问

    按照labview的web访问具体步骤操作完之后,把电脑的网络要设置在局域网环境下,网络要处于专用网络中,就可以进行局域网内访问

  2. Web基础学习

    Servlet和Servlet容器.Web服务器概念:https://blog.csdn.net/lz233333/article/details/68065749 <初学 Java Web 开 ...

  3. KiB 、十进制单位转换 、二进制单位转换

    KiB是kilo binary byte的缩写,指的是千位二进制字节 KB是kilobyte的缩写,指的是千字节   二进制标准命名   十进制国际单位制SI标准: 1 KB= 1,000 Byte ...

  4. 【css3】画‘百分比圆’

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. linux php5.6 安装Redis扩展

    wget http://pecl.php.net/get/redis-4.2.0.tgz tar -zxvf redis-.tgz cd redis- /usr/local/php5./bin/php ...

  6. checkbox和radio元素的样式设置(简易版)

    html代码 //html <div> <p style="font-size:18px;margin-top:30px;color:rgba(0,0,0,0.44)&qu ...

  7. spring MVC 入门:

    概述: MVC: M model 模型,除控制和视图以外,都可以成为模型. V view 视图,JSP,HTML,ASP,PHP,XHTML,FREEMARK,JSF,直接与用户做交互的资源. C c ...

  8. 软件151 王楚博 JavaEE的配置

    一.准备以下压缩包 1.JDK1.7 文件:jdk1.7.rar 2. eclipse-jee-mars-2 文件:32位系统准备eclipse-jee-mars-2-win32.zip,64位系统准 ...

  9. Oracle 11g OGG mgr定期清理tail 文件

    OGG mgr定期清理tail 文件 2018-06-11 11:58 440 0 原创 GoldenGate 本文链接:https://www.cndba.cn/leo1990/article/28 ...

  10. git版本控制系统更新

    版本控制系统: 一.概念: 版本控制系统(Version Control System):是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 二.版本控制系统分类 1.本地版本控制 ...