|
|
NiFi templating notes
Russell Bateman
April 2023
last update:
|
I don't generally write flows, but only custom processors. However, from time to
time I'm asked to provide templates that demonstrate how to use my custom
processors. Step by step, here's how I go about it. I used Apache NiFi 1.13.2
as I wrote the following methodology.
Table of Contents
- Instructions on creating a flow to become a template
- Create a template
- Download a template (save to the filesystem for distributing)
- Upload a template (from the filesystem to your NiFi UI canvas)
- Use a template (in your flow on the NiFi UI canvas)
Instructions on creating a flow to become a template...
- This step supposes that you already know how to work with
the NiFi UI canvas and create flows that do something.
- I prefer to design a flow inside a dedicated process group,
then make a template of that process group. If you do this, you will need
to ensure either an Input Port or an Output Port to
begin and/or end the flow, usually both, to get flowfiles into the
templated flow and/or out of it.
- Create a new process group with a descriptive name, create a
a flow, then test it.
- Once I'm happy with the result, I erase the non-prescribable
configuration of the processors and controllers in the flow. (This is why
the thing I'm creating is a "template" that can serve in other flows.)
- In the template we create later, it might not be totally
obvious what the user of your template needs to do. I use labels to help
thing along. In a label of default color (#FFF7D7, see below)
near each processor needing configuration, list the properties of that
processor (and any controller to which it subscribes) that must be
configured to get it working. For example (JdbcToXml):
Left to do:
- DBCPConnectionPool configure
- Adjust Base SQL Query if needed
DBCPConnectionPool
- Fix Database Connection URL
- Fix Database User
- Fix Password
- I also like to put
a big, green (#4CBB17) label
as a background under the relationship arcs and processor flows that
represent successful outcomes and
a big, red (#E34234) label
where failures are taken care of in my flow. It imparts a certain
clarity. Use your imagination here—or don't.
- Leave the processor group, i.e.: go "upstairs" where our
template will be created.
Create a template...
- If the name of the template you're about to create is already
in use, consider deleting the template you want to replace. You cannot
create a new or updated template with a name that conflicts with an
existing one.
- Click the General (or "hamburger") menu.
- Select Templates.
- Find and select the template whose name you wish to use.
- Click the Remove Template (trashcan icon).
- Close the NiFi Templates dialog by click the X
button.
- Create a template of the process group.
- Open the Operate palette.
- Select the process group to become a template.
- Click the Create Template icon on the Operate
palette.
- Name the template.
Note: consider prefixing with a number to aid flow writer with the
relative order if this template is part of a greater collection.
For example, if the second thing a flow does is to implements
importing data from Mirth, name the template that performs this,
2-mirth-flow.
- In Description, describe what the template implements.
In the example immediately above, this might be:
JdbcToXml from Mirth, excerpt document, note its type and hash
it ready for ETL..
- Click OK.
- Verify the template you've just created by:
- Clicking the General (or "hamburger") menu.
- Select Templates.
- Finding the template just created.
- Examining the Name and Description.
- Close the NiFi Templates dialog by click the X
button.
Download a template (saving to the filesystem for distributing)...
- In the UI canvas containing any created template:
- Click the General (or "hamburger") menu.
- Select Templates.
- Find the template you wish to use based on Name or
Description.
- Click the Download icon and save the resulting XML file in
the place from which you'd like to distribute it.
- Close the NiFi Templates dialog by click the X
button.
- You can distribute the template as a simple XML file for
yourself or others to upload and use.
Upload a template (from the filesystem to your NiFi UI canvas)...
- In the UI canvas where you need a template that exists in
your filesystem, do this:
- Open the Operate palette.
- Click on the Upload Templates.
- In the Upload Template dialog, click the
Select Template button (a magnifying glass).
- Navigate to find the XML file in your filesystem that is the
template you wish to use. Select it and click Open.
- Back in the Upload Template dialog, click the
Upload button.
- Close the NiFi Templates dialog by click the X
button.
- This enters the chosen template into the list of templates to which
the flow you're writing has access.
Use a template (in your flow on the NiFi UI canvas)...
- In the UI canvas where you are implementing your flow and
wish to use an existing template:
- Click and drag an instance of the Template tool, in the
Components Toolbar at the top of the UI canvas window to
the place on the cavas where you want to deploy the templated flow.
- In the Add Template dialog that comes up, select the
template you wish to us from the Choose Template drop-down
list.
- If you have already used this template elsewhere on your "visible"
canvas (that is, at the current processor group level), you'll see
the template name prefixed with "Copy of" and probably want to
change that to a more specific description. (Of course, this hint
depends entirely on what is in the template. If it's a process
group—how I like to do templates—then what I have just
said is relevant. However, if it's a group or pile of loose
processors, then there will be no "name" or "prefixed name.")