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. js 获取二级域名

    js获取页面完整地址: window.location.href; var s =" https://ejym.baidu.com";            var h = s.s ...

  2. TP5.0 PHPExcel 数据表格导出导入(原)

    今天看的是PHPExcel这个扩展库,Comporse 下载不下来,最后只能自己去github里面手动下载,但有一个问题就是下载下来的PHPExcel没有命名空间,所以框架里面的use根本引入不进去, ...

  3. 在Linux和Windows之间的远程控制的实现

    主要开发工作用Linux,邮件和文档等主要在Windows,两者之间经常需要传输数据,两台主机都必须同时运行着. 但是,通常来说,它们需要同时准备两套显示器.鼠标和键盘,既占地方又不够方便. 远程控制 ...

  4. 基于thinkphp的后台管理系统模板快速搭建

    当我们在搭建网站的时候,后端开发人员在编写后台的管理系统的时候,往往会因为缺少一个合适的后台管理系统的模板,而必须去重新编写一个,这几天由于工作上的安排,需要去研究一下thinkcmf的后台管理系统, ...

  5. Angular2 之父子组件交互方式

    父子组件交互方式,这里介绍主要的三种方式 1.事件传值 下面以列表页和分页组件举例. list.component.html <pagination *ngIf="pageParams ...

  6. vue-router路由讲解

    此文章用来系统讲解vue-router路由 安装 只介绍npm安装 npm install vue-router --save 项目所需依赖 在main.js或者app.vue中导入 import V ...

  7. OSI,TCP/IP,五层协议的体系结构,以及各层协议

    OSI分层 (7层):物理层.数据链路层.网络层.传输层.会话层.表示层.应用层. TCP/IP分层(4层):网络接口层. 网际层.运输层. 应用层. 五层协议 (5层):物理层.数据链路层.网络层. ...

  8. eclipse 打开时一闪而过解决办法

    编辑文件:eclipse.ini,在 -vmargs 上一行添加: -vmC:/Program Files/Java/jdk1.8.0_131/jre/bin “C:/Program Files/Ja ...

  9. Datatables一些常见的JS用法(多与表格有关)

    **********************--倒--序--上--升--(方便新增.更新)*********************** (3)给 动态生成 的 行 内按钮,添加点击事件(多用于分页情 ...

  10. 使用mybatis-generator工具自动生成mybatis代码

    使用mybatis-generator工具自动生成mybatis代码 步骤如下: 1.引入maven  依赖,在项目pom.xml文件中添加 <plugin> <groupId> ...