oozie4.3.0+sqoop1.4.6实现mysql到hive的增量抽取

1.准备数据源

mysql中表bigdata,数据如下:

2. 准备目标表

目标表存放hive中数据库dw_stg表bigdata

保存路径为 hdfs://localhost:9000/user/hive/warehouse/dw_stg.db/bigdata

hive中建表语句如下:

create external table bigdata(
class_id string comment ‘课程id‘,
class_name string comment ‘课程名称‘,
class_month int comment ‘课程周期‘,
teacher string comment ‘课程老师‘,
update_time string comment ‘更新日期‘
)
partitioned by(dt string comment ‘年月日‘)
row format delimited
fields terminated by ‘\001‘
lines terminated by ‘\n‘
stored as textfile;

注意点: 字段分隔符使用\001,行分隔符使用\n ,增加表分区dt格式为yyyMMdd

在hive中创建上面表bigdata.

3. 编写oozie脚本文件

3.1 配置job.properties

# 公共变量
timezone=Asia/Shanghai
jobTracker=dwtest-name1:8032
nameNode=hdfs://dwtest-name1:9000
queueName=default
warehouse=/user/hive/warehouse
dw_stg=${warehouse}/dw_stg.db
dw_mdl=${warehouse}/dw_mdl.db
dw_dm=${warehouse}/dw_dm.db
app_home=/user/oozie/app
oozie.use.system.libpath=true

# coordinator
oozie.coord.application.path=${nameNode}${app_home}/bigdata/coordinator.xml
workflow=${nameNode}${app_home}/bigdata

# source
connection=jdbc:mysql://192.168.1.100:3306/test
username=test
password=test
source_table=bigdata

# target
target_path=${dw_stg}/bigdata

# 脚本启动时间,结束时间
start=2018-01-24T10:00+0800
end=2199-01-01T01:00+0800

3.2  配置coordinator.xml

<coordinator-app name="coord_bigdata" frequency="${coord:days(1)}" start="${start}" end="${end}" timezone="${timezone}" xmlns="uri:oozie:coordinator:0.5">
    <action>
        <workflow>
            <app-path>${workflow}</app-path>
            <configuration>
                <property>
                    <name>startTime</name>
                    <value>${coord:formatTime(coord:dateOffset(coord:nominalTime(), -1, ‘DAY‘), ‘yyyy-MM-dd 00:00:00‘)}</value>
                </property>
                <property>
                    <name>endTime</name>
                    <value>${coord:formatTime(coord:dateOffset(coord:nominalTime(), 0, ‘DAY‘), ‘yyyy-MM-dd 00:00:00‘)}</value>
                </property>
                <property>
                    <name>outputPath</name>
                    <value>${target_path}/dt=${coord:formatTime(coord:dateOffset(coord:nominalTime(), 0, ‘DAY‘), ‘yyyyMMdd‘)}/</value>
                </property>
            </configuration>
        </workflow>

    </action>
</coordinator-app>

注意点:

增量的开始时间startTime获取:   当前时间的前一天 输出值为 2018-01-23 00:00:00

${coord:formatTime(coord:dateOffset(coord:nominalTime(), -1, ‘DAY‘), ‘yyyy-MM-dd 00:00:00‘)}

增量的结束时间endTime获取:  输出值为 2018-01-24 00:00:00

${coord:formatTime(coord:dateOffset(coord:nominalTime(), 0, ‘DAY‘), ‘yyyy-MM-dd 00:00:00‘)}

输出路径需要带上分区字段dt:  输出值 /user/hive/warehouse/dw_stg.db/bigdata/dt=20180124/

${target_path}/dt=${coord:formatTime(coord:dateOffset(coord:nominalTime(), 0, ‘DAY‘), ‘yyyyMMdd‘)}/

3.3 配置workflow.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!--
 3   Licensed to the Apache Software Foundation (ASF) under one
 4   or more contributor license agreements.  See the NOTICE file
 5   distributed with this work for additional information
 6   regarding copyright ownership.  The ASF licenses this file
 7   to you under the Apache License, Version 2.0 (the
 8   "License"); you may not use this file except in compliance
 9   with the License.  You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 -->
19 <workflow-app xmlns="uri:oozie:workflow:0.4" name="wf_bigdata">
20     <start to="sqoop-node"/>
21
22     <action name="sqoop-node">
23         <sqoop xmlns="uri:oozie:sqoop-action:0.2">
24             <job-tracker>${jobTracker}</job-tracker>
25             <name-node>${nameNode}</name-node>
26             <prepare>
27                 <delete path="${nameNode}${outputPath}"/>
28             </prepare>
29             <configuration>
30                 <property>
31                     <name>mapred.job.queue.name</name>
32                     <value>${queueName}</value>
33                 </property>
34             </configuration>
35             <arg>import</arg>
36             <arg>--connect</arg>
37             <arg>${connection}</arg>
38             <arg>--username</arg>
39             <arg>${username}</arg>
40             <arg>--password</arg>
41             <arg>${password}</arg>
42             <arg>--verbose</arg>
43             <arg>--query</arg>
44             <arg>select class_id,class_name,class_month,teacher,update_time from ${source_table} where $CONDITIONS and update_time &gt;= ‘${startTime}‘ and update_time &lt; ‘${endTime}‘</arg>
45             <arg>--fields-terminated-by</arg>
46             <arg>\001</arg>
47             <arg>--target-dir</arg>
48             <arg>${outputPath}</arg>
49             <arg>-m</arg>
50             <arg>1</arg>
51         </sqoop>
52         <ok to="end"/>
53         <error to="fail"/>
54     </action>
55
56     <kill name="fail">
57         <message>Sqoop free form failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
58     </kill>
59     <end name="end"/>
60 </workflow-app>

4. 上传脚本

将以上3个文件上传到hdfs的oozie目录app下如下:

5. 执行job

oozie job -config job.properties -run

6. 查看job状态

7. 查询hive中表

使用 msck repair table bigdata 自动修复分区,然后查询结果,测试没用问题。

 8. 开发中遇到的坑如下:

8.1 workflow.xml中字段分隔符不能带单引号。正确的是<arg>\001</arg> ,错误的是<arg>‘\001‘</arg>

8.2 由于sqoop的脚本配置在xml中,所以在判断条件时使用小于号"<"会报错,xml文件校验不通过。

解决方法使用 &lt; 代替 "<"  ,所以使用大于号时最好也使用 &gt;代替 ">" 

原文地址:https://www.cnblogs.com/30go/p/8342536.html

时间: 2024-07-30 10:15:31

oozie4.3.0+sqoop1.4.6实现mysql到hive的增量抽取的相关文章

使用sqoop从mysql往hive中增量导数据shell脚本

一:sqoop增量导入的两种方式 Incremental import arguments: Argument Description --check-column (col) Specifies the column to be examined when determining which rows to import. (the column should not be of type CHAR/NCHAR/VARCHAR/VARNCHAR/ LONGVARCHAR/LONGNVARCHA

hive表增量抽取到mysql(关系数据库)的通用程序(三)

hive表增量抽取到oracle数据库的通用程序(一) hive表增量抽取到oracle数据库的通用程序(二) 这几天又用到了该功能了,所以又改进了一版,增加了全量抽取和批量抽取两个参数.并且可以设置每批次抽取到记录数. 使用shell脚本可以直接方便到将hive中到表抽取到任何关系型数据库中. shell脚本到demo如下,为便于测试,将每批次处理改为2条记录: #!/bin/sh ## !!!注意lib中jar包兼容性问题: ## 如果包含log4j-slf4j-impl-2.6.2.jar

Oozie4.2.0配置安装实战

软件版本: Oozie4.2.0,Hadoop2.6.0,Spark1.4.1,Hive0.14,Pig0.15.0,Maven3.2,JDK1.7,zookeeper3.4.6,HBase1.1.2,MySQL5.6 集群部署: node1~4.centos.com     node1~4      192.168.0.31~34          1G*4 内存    1核*4 虚拟机 node1:NameNode .ResourceManager: node2:SecondaryNameN

Sqoop1.4.4将MySQL数据库表中数据导入到HBase表中

问题导读:         1.--hbase-table.--hbase-row-key.--column-family及--hbase-create-table参数的作用? 2.Sqoop将关系型数据库表中数据导入HBase中,默认Rowkey是什么? 3.如果关系型数据库表中存在多关键字,该怎么办? 一.简介及部分重要参数介绍 Sqoop除了能够将数据从关系型数据库导入到HDFS和Hive中,还能够导入到HBase表中. --hbase-table:通过指定--hbase-table参数值

【转】Oozie4.2.0配置安装实战

什么是Oozie? Oozie是一种Java Web应用程序,它运行在Java servlet容器——即Tomcat——中,并使用数据库来存储以下内容: 工作流定义 当前运行的工作流实例,包括实例的状态和变量 Oozie工作流是放置在控制依赖DAG(有向无环图 Direct Acyclic Graph)中的一组动作(例如,Hadoop的Map/Reduce作业.Pig作业等),其中指定了动作执行的顺序.我们会使用hPDL(一种XML流程定义语言)来描述这个图. 修改HDFS配置: 修改hadoo

oozie-4.1.0 安装

Oozie安装 一.准备 默认hadoop,maven,mysql已经安装,我的hadoop是2.5.0 下载oozie安装包:oozie-4.1.0.tar.gz    http://mirror.bit.edu.cn/apache/oozie/ 下载ext-2.2.zip  http://oozie.apache.org/docs/4.0.1/DG_QuickStart.html该路径有extjs的链接 二.编译 1.      修改源码中的一个bug,在 oozie-4.1.0.tar.g

Apache Tomcat/6.0.39如何配置连接mysql,JDBC:mysql-connector-java-5.1.30-bin.jar-成功连接心得

http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html 前提:开启TOMCAT,MYsql MySQL DBCP Example0. Introduction Versions of MySQL and JDBC drivers that have been reported to work: MySQL 3.23.47, MySQL 3.23.47 using InnoDB,, MySQL 3.23

mysql 数据库定时备份 增量/全备份

实验楼的 MySQL 数据库需要设计一个自动备份方案,能够每周执行一次全备份,每天执行一次增量备份. 数据库名称为 shiyanlou,管理的用户名为 shiyanlou,密码为 shiyanlou.注意需要先手动启动 MySQL 服务. 目标 设计并实现备份方案,任务完成后满足以下要求: MySQL 服务处于运行状态 需要为服务器中的 shiyanlou 用户设定计划任务 计划任务中设定每周的周一凌晨3点执行一次全备份 计划任务中设定每天凌晨3点执行一次增量备份,周一不执行 请不要编写额外的脚

【甘道夫】Sqoop1.4.4 实现将 Oracle10g 中的增量数据导入 Hive0.13.1 ,并更新Hive中的主表

需求 将Oracle中的业务基础表增量数据导入Hive中,与当前的全量表合并为最新的全量表. ***欢迎转载,请注明来源***    http://blog.csdn.net/u010967382/article/details/38735381 设计 涉及的三张表: 全量表:保存了截止上一次同步时间的全量基础数据表 增量表:增量临时表 更新后的全量表:更新后的全量数据表 步骤: 通过Sqoop将Oracle中的表导入Hive,模拟全量表和增量表 通过Hive将"全量表+增量表"合并为