将MySQL同步到HIVE并分区 #nifi#

一、初始化(第一次将mysql全量写入hive)

0. 整体流程图

image

1. 流程说明

  • 从mysql中查询出数据
  • 写入到hive

2. 查询mysql数据库(配置ExecuteSQL Processor)

image

其中必填项:

  • Database Connection URL
  • SQL select query
select * from test_user
image

其中必填项:

  • Database Connection URL
jdbc:mysql://10.0.31.239:3309/csust
  • Database Driver Class Name
com.mysql.jdbc.Driver
  • Database Driver Location(s)
/opt/monica/nifi-1.8/lcylibs/mysql-connector-java-5.1.47.jar

3. 写入hive数据仓库(配置PutHiveStreaming Processor)

image

其中必填项:

  • Hive Metastore URI
thrift://10.0.31.239:9083
  • Hive Configuration Resources
/usr/hdp/2.6.4.0-91/hive2/conf/hive-site.xml
  • Database Name
default
  • Table Name
test_user2019

二、增量写入

0. 整体流程图

image

1. 流程说明

  • 先从hive中查询当前最大id
  • 将查询出的数据类型从avro转成json
  • 从json中提取该id
  • 根据id从mysql查出未写入到hive的数据
  • 写入hive

2. SelectMaxIdFromHive(配置SelectHiveQL Processor)

image

其中必填项:

  • Hive Database Connection Pooling Service
  • HiveQL Select Query
select max(id) as maxid from default.test_user2019
image

其中必填项:

  • Database Connection URL
jdbc:hive2://10.0.31.239:10000
  • Hive Configuration Resources
/usr/hdp/2.6.4.0-91/hive2/conf/hive-site.xml
  • Database User
hive

3. 将查询出的数据类型从avro转成json(配置ConvertAvroToJSON Processor)

image

4. 从json中提取该id(配置EvaluateJsonPath Processor)

image

其中,需添加属性maxid,$.maxid命名与json字符串中一致。

5. SelectDataFromMysql(配置ExecuteSQL Processor)

image

其中必填项:

  • Database Connection Pooling Service
  • SQL select query
select id,name,age,create_time from test_user where id > ${maxid} and ${maxid} is not null

6. InsertHive(配置PutHiveStreaming Processor)

image

其中必填项:

  • Hive Metastore URI
thrift://10.0.31.239:9083
  • Hive Configuration Resources
/usr/hdp/2.6.4.0-91/hive2/conf/hive-site.xml
  • Database Name
default
  • Table Name
test_user2019

三、分区

0. 整体流程图

image

1. 流程说明

  • 清空分区表数据
  • 从原始表导入到分区表

2. TruncateTable(配置ExecuteSQL Processor)

image

其中必填项:

  • Hive Database Connection Pooling Service
  • HiveQL Select Query
truncate table default.test_user2019a

3. TablePartition(配置ExecuteSQL Processor)

image

其中必填项:

  • Hive Database Connection Pooling Service
  • HiveQL Select Query
insert into table test_user2019a partition(create_time) select id, name, age, create_time from test_user2019

四、注意事项

  1. 定时调度(Scheduling)
  • CORN表达式和Quartz一样,最小单位为秒。

五、附录

1. MySQL建表语句

create table test_user
(
    id int auto_increment
        primary key,
    name varchar(20) null,
    age int null,
    create_time varchar(8) null
)
engine=InnoDB;

2. Hive建表语句

create table test_user2019(
id int,
name string,
age int,
create_time string
)
clustered by (id) into 3 buckets
row format delimited
fields terminated by '\t'
stored as orc
tblproperties('transactional'='true');

3. Hive分区表建表语句

create table test_user2019a(
id int,
name string,
age int
)
partitioned by(create_time string)
clustered by (id) into 3 buckets
row format delimited
fields terminated by '\t'
stored as orc
tblproperties('transactional'='true');
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spark SQL, DataFrames and Datasets Guide Overview SQL Dat...
    草里有只羊阅读 18,428评论 0 85
  • 什么是数据库? 数据库是存储数据的集合的单独的应用程序。每个数据库具有一个或多个不同的API,用于创建,访问,管理...
    chen_000阅读 4,075评论 0 19
  • Hive是什么? Hive是一种基于Hadoop的数据库技术并提供完整的sql查询功能, . HIVE能做什么? ...
    日出卡尔阅读 2,776评论 0 0
  • Xgboost原理 1. 说明:  难了不会,会了不难,你明白了,觉得这还用说?不明白,跳步之后,似懂非懂。...
    xieyan0811阅读 2,288评论 1 5
  • 又是一次长途…… 如果行李不是太多,时间不是太赶,没有结伴的话,还是火车比较划算。 听着车厢里的聊天,想着自己以后...
    jiaxiaolei阅读 113评论 0 0