Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache NiFi: Creating new column using a condition

I have asked a similar question. Yet I wasn't able to find a solution for my problem through that approach. I have a csv which looks like this:

studentID,regger,age,number
123,west,12,076392367
456,nort,77,098123124
231,west,33,076346325

I want to add a new column and add values according to the data in the number field.This is the logic.

If the first 4 digits of data in the number column is equal to "0763" then the new column named (status) must be set as INSIDE or if it is any other value its OUTSIDE

As mentioned in the logic the output must look like this:

studentID,regger,age,number,status
123,west,12,076392367,INSIDE
456,nort,77,098123124,OUTSIDE
231,west,33,076346325,INSIDE

My Approach

I tried to achieve this by first duplicating the number column to the status column. And then trying to take the first 4 digits and dealing with it.

Hope you would be able to suggest a way to Nifi Workflow to make this possible.

like image 309
Himsara Gallege Avatar asked May 14 '26 02:05

Himsara Gallege


1 Answers

I used the UpdateRecord processor twice and got the results that you want.

Input

I started with your input data.

studentID,regger,age,number
123,west,12,076392367
456,nort,77,098123124
231,west,33,076346325

Process

First, set the UpdateRecord processor as follows:

Record Reader               CSVReader
Record Writer               CSVRecordSetWriter
Replacement Value Strategy  Record Path Value
/status                     /number

it will create the new column status with the value of number column.

Second, the first output should go to another UpdateRecord processor with the options

Record Reader               CSVReader
Record Writer               CSVRecordSetWriter
Replacement Value Strategy  Literal Value
/status                     ${field.value:substring(0,4):equals('0763'):ifElse(${field.value:replace(${field.value},'INSIDE')},${field.value:replace(${field.value},'OUTSIDE')})}

and this will give you the final results.

Be aware that the number column is not an integer column, so you have to set the record reader CSVReader with the option Schema Access Strategy to the Use String Fields From Header.

Output

studentID,regger,age,number,status
123,west,12,076392367,INSIDE
456,nort,77,098123124,OUTSIDE
231,west,33,076346325,INSIDE
like image 87
Lamanus Avatar answered May 19 '26 04:05

Lamanus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!