Mysql + NiFi + Kudu 实战

Objective

This tutorial demonstrates how to use the QueryDatabaseTable and PutKudu processors to read data from a MySQL database and put into Kudu. Thanks to @Cam Mach for his assistance with this article.

Note: The PutKudu processor was introduced in NiFi 1.4.0.

Environment

This tutorial was tested using the following environment and components:

Mac OS X 10.11.6

Apache NiFi 1.4.0

Apache Kudu 1.5.0

MySQL 5.7.13

PutKudu (AvroReader)

Demo Configuration

MySQL Setup

Create table

Create the table 'users' In your MySQL instance, choose a database ("nifi_db" in my instance) and create the table "users":

unix> mysql -u root -p
unix> Enter password:<enter>
mysql> use nifi_db;
mysql>CREATE TABLE `users` (
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  `title` text,
  `first_name` text,
  `last_name` text,
  `street` text,
  `city` text,
  `state` text,
  `zip` text,
  `gender` text,
  `email` text,
  `username` text,
  `password` text,
  `phone` text,
  `cell` text,
  `ssn` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=latin1;
Insert data

Add data to the "users" table:

mysql>INSERT INTO `users` (`id`, `title`, `first_name`, `last_name`, `street`, `city`, `state`, `zip`, `gender`, `email`, `username`, `password`, `phone`, `cell`, `ssn`)
VALUES (1, 'miss', 'marlene', 'shaw', '3450 w belt line rd', 'abilene', 'florida', '31995', 'F', 'marlene.shaw75@example.com', 'goldenpanda70', 'naughty', '(176)-908-6931', '(711)-565-2194', '800-71-1872'),
(2, 'ms', 'letitia', 'jordan', '2974 mockingbird hill', 'irvine', 'new jersey', '64361', 'F', 'letitia.jordan64@example.com', 'lazytiger614', 'aaaaa1', '(860)-602-3314', '(724)-685-3472', '548-93-7031'),
(3, 'mr', 'todd', 'graham', '5760 spring hill rd', 'garden grove', 'north carolina', '81790', 'M', 'todd.graham39@example.com', 'purplekoala484', 'paintball', '(230)-874-6532', '(186)-529-4912', '362-31-5248'),
(4, 'mr', 'seth', 'martinez', '4377 fincher rd', 'chandler', 'south carolina', '73651', 'M', 'seth.martinez82@example.com', 'bigbutterfly149', 'navy', '(122)-782-5822', '(720)-778-8541', '200-80-9087'),
(5, 'mr', 'guy', 'mckinney', '4524 hogan st', 'iowa park', 'ohio', '24140', 'M', 'guy.mckinney53@example.com', 'blueduck623', 'office', '(309)-556-7859', '(856)-764-9146', '973-37-9077'),
(6, 'ms', 'anna', 'smith', '5047 cackson st', 'rancho cucamonga', 'pennsylvania', '56486', 'F', 'anna.smith74@example.com', 'goldenfish121', 'albion', '(335)-388-7351', '(485)-150-6348', '680-20-6440'),
(7, 'mr', 'johnny', 'johnson', '7250 bruce st', 'gresham', 'new mexico', '83973', 'M', 'johnny.johnson73@example.com', 'crazyduck127', 'toast', '(142)-971-3099', '(991)-131-1582', '683-26-4133'),
(8, 'mrs', 'robin', 'white', '7882 northaven rd', 'orlando', 'connecticut', '40452', 'F', 'robin.white46@example.com', 'whitetiger371', 'elizabeth', '(311)-659-3812', '(689)-468-6420', '960-70-3399'),
(9, 'miss', 'allison', 'williams', '7648 edwards rd', 'edison', 'louisiana', '52040', 'F', 'allison.williams82@example.com', 'beautifulfish354', 'sanfran', '(328)-592-3520', '(550)-172-4018', '164-78-8160');

[图片上传失败...(image-50a5a4-1534481079313)]

Kudu Setup

Find IP Addr

For my setup, I followed the Apache Kudu Quickstart instructions to easily set up and run a Kudu VM.

To check that your VM is running:

unix> VBoxManage list runningvms"kudu-demo" {b39279b5-3dd6-478a-ac9d-2204bf88e7b9}

To see what IP Kudu is running on:

unix> VBoxManage guestproperty get kudu-demo /VirtualBox/GuestInfo/Net/0/V4/IPValue: 192.168.58.100

The Kudu web client runs on port 8051:

[图片上传失败...(image-c766ed-1534481079313)]

Create table

Create a table in Kudu by first connecting to Impala in the virtual machine:

ssh demo@quickstart.cloudera -t impala-shelldemo@quickstart.cloudera's password:[quickstart.cloudera:21000] >

( Note: The username and password for the Quickstart VM is "demo".)

Create the Kudu table with the same columns and data types as the MySQL table:

CREATE TABLE users_kudu
    (
    id BIGINT,
    title STRING,
    first_name STRING,
    last_name STRING,
    street STRING,
    city STRING,
    state STRING,
    zip STRING,
    gender STRING,
    email STRING,
    username STRING,
    password STRING,
    cell STRING,
    ssn STRING,
    PRIMARY KEY(id)
    )
    PARTITION BY HASH PARTITIONS 16
    STORED AS KUDU;

[图片上传失败...(image-dec020-1534481079314)]

NiFi Flow Setup

Follow the following detailed instructions to set up the flow. Alternatively, a template of the flow can be downloaded here:putkudu-querydatabasetable.xml

Two Controller Services

Two controller services are needed for the flow. Click the "Configuration" button (gear icon) from the Operate palette:

DBCPConnectionPool

[图片上传失败...(image-b66773-1534481079314)]

This opens the NiFi Flow Configuration window. Select the "Controller Services" tab. Click the "+" button and add a DBCPConnectionPool controller service:

[图片上传失败...(image-659b26-1534481079314)]

Configure the controller service as follows (adjusting the property values to match your own MySQL instance and environment):

[图片上传失败...(image-545220-1534481079314)]

AvroReader

Next, add an AvroReader controller service:

[图片上传失败...(image-8587df-1534481079314)]

Apply the default configuration:

[图片上传失败...(image-bd70b8-1534481079314)]

Select the "lightning bolt" icon for each controller service to enable them:

[图片上传失败...(image-7a8c8a-1534481079314)]

QueryDatabaseTable processor:

[图片上传失败...(image-38221-1534481079314)]

Configure the processor as follows:

[图片上传失败...(image-f6fe7b-1534481079314)]

where:

The DBCPConnectionPool controller service created earlier is selected for Database Connection Pooling Service

"users" is entered for the Table Name

"id" is entered for the Maximum-value Columns

PutKudu Processor

[图片上传失败...(image-f2e548-1534481079314)]

Configure the PuKudu processor as follows:

[图片上传失败...(image-d72b8a-1534481079314)]

where:

"192.168.58.100:7051" is entered for the Kudu Masters IP and port (7051 is the default port)

"impala::default.users_kudu" is entered for the Table Name

Skip head line property is set to "false"

The AvroReader controller service created earlier is selected for Record Reader

Auto-terminate the Success relationship:

[图片上传失败...(image-b1d62-1534481079314)]

On the canvas, make a "failure" relationship connection from the PutKudu processor to itself:

[图片上传失败...(image-ef6a27-1534481079314)]

Run Flow

Start the QueryDatabaseTable processor.

[图片上传失败...(image-499032-1534481079314)]

Looking at the contents of the FlowFile in the queue, the data from the MySQL table has been ingested and converted to Avro format:

[图片上传失败...(image-52de0c-1534481079314)]

Start the PutKudu processor to put the data into Kudu:

[图片上传失败...(image-ef7584-1534481079314)]

This can be confirmed via a Select query:

[图片上传失败...(image-365058-1534481079314)]

With the flow still running, add another row of data to the Mysql "users" table:

[图片上传失败...(image-a68a37-1534481079314)]

The flow processes this data and the new row appears in Kudu:

[图片上传失败...(image-496f97-1534481079314)]

Helpful Links

Here are some links to check out if you are interested in other flows which utilize the record-oriented processors and controller services in NiFi:

Convert CSV to JSON, Avro, XML using ConvertRecord

Installing a local Hortonworks Registry to use with Apache NiFi

Running SQL on FlowFiles using QueryRecord Processor

Using PublishKafkaRecord_0_10 (CSVReader/JSONWriter) in Apache NiFi 1.2+

Using PutElasticsearchHttpRecord (CSVReader)

Using PartitionRecord (GrokReader/JSONWriter) to Parse and Group Log Files

Geo Enrich NiFi Provenance Event Data using LookupRecord

Using PutMongoRecord to put CSV into MongoDB

How-To/Tutorialapache-kuduapache-nifiavromysql

1a-mysql-users-table.png (332.1 kB)

1b-kuduwebclient.png (178.4 kB)

1c-users-kudu-table-created.png (131.8 kB)

2-flow-configuration.png (123.8 kB)

3-add-dbcpconnectionpool.png (238.6 kB)

4-dbcpconnectionpool-configuration.png (129.1 kB)

5-add-avroreader.png (244.4 kB)

6-avroreader-configuration.png (106.4 kB)

7-controllerservices-enabled.png (94.4 kB)

8-add-querydatabasetable.png (157.1 kB)

9-querydatabasetable-configuration.png (92.0 kB)

10-add-putkudu-connect.png (183.4 kB)

11-putkudu-configuration.png (131.0 kB)

12-autoterminate-success.png (139.7 kB)

13-putkudu-failure.png (191.0 kB)

14-querydatabasetable-start.png (191.7 kB)

15-querydatabasetable-flowfile-contents.png (199.1 kB)

16-putkudu-start.png (193.1 kB)

17-users-kudu-table-data.png (253.7 kB)

18-mysql-new-row.png (360.5 kB)

19-kudu-new-row.png (261.2 kB)

putkudu-querydatabasetable.xml (15.1 kB)

可能遇到的问题及其处理方案:

RPC问题

场景

machine003:8000PutKudu[id=83d7455e-7db4-1cb5-0000-00005e82a276] Exception occurred while interacting with Kudu due to RPC can not complete before timeout: KuduRpc(method=GetTableSchema, tablet=null, attempt=23, DeadlineTracker(timeout=30000, elapsed=29282), Traces: [0ms] 
 querying master, [2ms] Sub rpc: ConnectToMaster sending RPC to server master-47.97.109.43:7051, [26ms] Sub rpc: ConnectToMaster received from server master-47.97.109.43:7051 response Network error: [Peer master-47.97.109.43:7051] Connection disconnected, [27ms] delaying RPC due to Service unavailable: Master config (47.97.109.43:7051) has no leader. Exceptions received: org.apache.kudu.client.RecoverableException: [Peer master-47.97.109.43:7051] Connection disconnected, [42ms] 
 

问题定位

到CDH页面查看Kudu的角色日志:

Unauthorized connection attempt: Server connection negotiation failed: server connection from 47.96.114.97:53732: unauthenticated connections from publicly routable IPs are prohibited. See --trusted_subnets flag for more information.: 47.96.114.97:53732

修改方法

CDH集成Kudu配置调整
# 集群-->kudu -->配置 --> Master -->高级 -->gflagfile 的 Master 高级配置代码段>
-rpc_encryption=disabled
-rpc_authentication=disabled
-trusted_subnets=0.0.0.0/0
# 右上角 ‘保存更改’ --> 重启集群 --> OK 

Tips

CDH 查看Kudu运行日志方法:

  1. 集群 --> kudu --> 命令 -->点击最新的启动命令
  2. 正在启动服务上的 4 角色 -->对角色 Master (gateway002) 执行命令
  3. 查看日志[图片上传失败...(image-14035f-1534481079314)]
非CDH:
> find / -name '*master.gflagfile' -type f # 查找配置文件位置
/etc/kudu/conf.dist/master.gflagfile
 vi /etc/kudu/conf.dist/master.gflagfile # 修改配置文件
-----------------------------------------------------------
#添加权限设置:
#方式一: 
-rpc_encryption=disabled
-rpc_authentication=disabled
#方式二: 
# --trusted_subnets=0.0.0.0/0
---------------------------------

参见:

kudu 1.4 单机安装后 运行java-sample

Authentication

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,468评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,620评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,427评论 0 334
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,160评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,197评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,334评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,775评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,444评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,628评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,459评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,508评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,210评论 3 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,767评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,850评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,076评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,627评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,196评论 2 341

推荐阅读更多精彩内容