DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Step-by-Step Guide to Implementing Columnar Tables in SQL Server
  • A Low-Latency Routing Pattern for Multiple Small Language Models
  • A Practical Guide to Temporal Workflow Design Patterns
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java

Trending

  • Real-Time Face Recognition Using OpenCV, Dlib, and Python
  • A Step-by-Step Guide to Implementing Columnar Tables in SQL Server
  • Building an Idempotent Job Queue in Node. js That Never Runs the Same Task Twice
  • R&D Engineering: Balancing Prototyping, Infrastructure, and Risk
  1. DZone
  2. Data Engineering
  3. Data
  4. Apache-Nifi: A Short Description of ifElse

Apache-Nifi: A Short Description of ifElse

By 
Sachith Muhandiram user avatar
Sachith Muhandiram
·
Nov. 02, 20 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
8.3K Views

Join the DZone community and get the full member experience.

Join For Free

I had been working with Apache-NiFi for last few months at my work. During this period I mostly used csv-based operations.

Recently, I got a problem with the following scenario.

In my flowfile, I needed to do a regex operation on a column value and `replace` them with some other code. I did this easily using the replaceAll function.

First, let's see what replaceAll does in Apache-NiFi. It takes two string arguments, first one is a regex and second is the replacement value.

For more details, I highly recommend reading Apache-NiFi expression language guide on replaceAll.

For the first attempt, I have used the following syntax for my column using UpdateRecord processor :

Plain Text
xxxxxxxxxx
1
 
1
${field.value:replaceAll(${field.value:length():le(8)},'Undefined'):replaceAll('[a-z]+','Wrong')}


This first checks whether the length of the column value is less than 8 characters. If so, it replaces that with Undefined. But some data matched with [a-z]+ pattern had been replaced with Wrong, even though they are less than 8 characters.

I tried to solve this issue myself, and as I was taking too much time, I asked the StackOverflow community. Lamanus’s answer made me think about ifElse in Apache-NiFi.

So now, let's go and see how Apache-NiFi’s ifElse behaves. It takes two arguments, but the difference is that, its the result based on Subject of the expression.

If the subject is true then, first argument is evaluated else second.

As given in the example: ${bool:ifElse('a','b')}, here bool is true. So, if I use replaceAll with this, the value will be replaced with a. 

In order to do my scenario using this, I had to change the logic by checking whether it has more than 8 characters and then applying my custom regex based operations.

Plain Text
xxxxxxxxxx
1
 
1
${field.value:length():ge(8):ifElse(
2
${field.value:replaceAll(‘[a-z]+’,’Wrong’)
3
:replaceAll('[0-9][a-z]+','Variable1')
4
,${field.value:replace(${field.value},'Undefined')}
5
// this is the statement executed when value length is shorter than 8 characters.)}


I hope this short description will give you a better understanding how to use ifElse in Apache-NiFi.

Apache NiFi Data Types

Opinions expressed by DZone contributors are their own.

Related

  • A Step-by-Step Guide to Implementing Columnar Tables in SQL Server
  • A Low-Latency Routing Pattern for Multiple Small Language Models
  • A Practical Guide to Temporal Workflow Design Patterns
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook