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:
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', '[email protected]', 'goldenpanda70', 'naughty', '(176)-908-6931', '(711)-565-2194', '800-71-1872'),
(2, 'ms', 'letitia', 'jordan', '2974 mockingbird hill', 'irvine', 'new jersey', '64361', 'F', '[email protected]', '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', '[email protected]', 'purplekoala484', 'paintball', '(230)-874-6532', '(186)-529-4912', '362-31-5248'),
(4, 'mr', 'seth', 'martinez', '4377 fincher rd', 'chandler', 'south carolina', '73651', 'M', '[email protected]', 'bigbutterfly149', 'navy', '(122)-782-5822', '(720)-778-8541', '200-80-9087'),
(5, 'mr', 'guy', 'mckinney', '4524 hogan st', 'iowa park', 'ohio', '24140', 'M', '[email protected]', 'blueduck623', 'office', '(309)-556-7859', '(856)-764-9146', '973-37-9077'),
(6, 'ms', 'anna', 'smith', '5047 cackson st', 'rancho cucamonga', 'pennsylvania', '56486', 'F', '[email protected]', 'goldenfish121', 'albion', '(335)-388-7351', '(485)-150-6348', '680-20-6440'),
(7, 'mr', 'johnny', 'johnson', '7250 bruce st', 'gresham', 'new mexico', '83973', 'M', '[email protected]', 'crazyduck127', 'toast', '(142)-971-3099', '(991)-131-1582', '683-26-4133'),
(8, 'mrs', 'robin', 'white', '7882 northaven rd', 'orlando', 'connecticut', '40452', 'F', '[email protected]', 'whitetiger371', 'elizabeth', '(311)-659-3812', '(689)-468-6420', '960-70-3399'),
(9, 'miss', 'allison', 'williams', '7648 edwards rd', 'edison', 'louisiana', '52040', 'F', '[email protected]', '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 [email protected] -t [email protected]'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

你可能感兴趣的:(Mysql + NiFi + Kudu 实战)