Support Questions

Find answers, ask questions, and share your expertise

How to change csv attribute/header name in apache nifi ?

avatar
Contributor

I am using apache nifi to convert json to csv. I want to change the headers of the generated csv . Is there any specific processor for this. I know how to achieve this using ExecuteScript processor but is there any easy approach.
Ex.
"_id", "name","time" to "id", "browser_name","duration"

1 ACCEPTED SOLUTION

avatar
Master Guru

@Vivek Singh

we can add user defined header by using replace text processor instead of writing any script.

For this case in your csvsetwriter controller service change the below property to false,

Include Header Line

false

now we are not going to get the header line included in the output flowfile, then use Replace text processor to add our own header to the flowfile content.

Replace text configs:-

64982-replacetext.png

Search Value

(?s)(^.*$)

Replacement Value

"_id", "name","time" to "id", "browser_name","duration"

Character Set

UTF-8

Maximum Buffer Size

1 MB //needs to change the value according to the file size that we are getting after convertrecord processor

Replacement Strategy

Prepend //we are prepend the the content with the above header line

Evaluation Mode

Entire text

Input content:-

sample content from convert record processor is as follows

"1","foo","12:00AM" to "123","Mozilla","1hr"

Output Content:-

we are adding our header to the above flowfile content the output flowfile content from replacetext processor would be

"_id", "name","time" to "id", "browser_name","duration"
"1","foo","12:00AM" to "123","Mozilla","1hr"
 If the Answer helped to resolve your issue, Click on Accept button below to accept the answer, That would be great help to Community users to find solution quickly for these kind of issues.

View solution in original post

2 REPLIES 2

avatar
Master Guru

@Vivek Singh

we can add user defined header by using replace text processor instead of writing any script.

For this case in your csvsetwriter controller service change the below property to false,

Include Header Line

false

now we are not going to get the header line included in the output flowfile, then use Replace text processor to add our own header to the flowfile content.

Replace text configs:-

64982-replacetext.png

Search Value

(?s)(^.*$)

Replacement Value

"_id", "name","time" to "id", "browser_name","duration"

Character Set

UTF-8

Maximum Buffer Size

1 MB //needs to change the value according to the file size that we are getting after convertrecord processor

Replacement Strategy

Prepend //we are prepend the the content with the above header line

Evaluation Mode

Entire text

Input content:-

sample content from convert record processor is as follows

"1","foo","12:00AM" to "123","Mozilla","1hr"

Output Content:-

we are adding our header to the above flowfile content the output flowfile content from replacetext processor would be

"_id", "name","time" to "id", "browser_name","duration"
"1","foo","12:00AM" to "123","Mozilla","1hr"
 If the Answer helped to resolve your issue, Click on Accept button below to accept the answer, That would be great help to Community users to find solution quickly for these kind of issues.

avatar
Explorer

Instead of using the function: prepend (this just shifts your data), how do you do an outright replace for the header row? I tried using Literal search, but I end up having strange characters when the replace occurs