blackfury
blackfury

Reputation: 685

Split Nifi Attribute Value To Multiple Attributes

I have a file which i get using the GetFile Processor.

The flowfile generated from this has an attribute (filename).

I want to split this "filename" attribute with value "ABC_gh_1245_ty.csv" by "_" into multiple attributes

ATTR1 = "ABC"
ATTR2 = "gh"
ATTR3 = "1245"
ATTR4 = "ty.csv"

I presume that there are no processors available for this functionality in nifi 1.7.1

I googled and found this custom processor: https://github.com/guvencenanguvenal/nifi-splitcreateattribute

It still doesnt work. It errors out with 'Attribuite dont found'

Upvotes: 2

Views: 3783

Answers (2)

Sivaprasanna Sethuraman
Sivaprasanna Sethuraman

Reputation: 4132

To solve this, you can leverage ExecuteScript or ExecuteGroovyScript processor. The following scrip would do the job just fine. It is written in Groovy though.

flowFile = session.get()

if (!flowFile)
    return

filename = flowFile.getAttribute('filename')
splits = filename.split('_')
attrsMap = [:]

splits.eachWithIndex {
    split, index -> attrsMap.put("ATTR" + index, split)
}

attrsMap.each{ k, v -> println "${k}:${v}" }
flowFile = session.putAllAttributes(flowFile, attrsMap)
session.transfer(flowFile, REL_SUCCESS)

Screenshots

enter image description here

enter image description here

enter image description here

Upvotes: 4

blackfury
blackfury

Reputation: 685

The below processor works perfectly fine...

https://github.com/guvencenanguvenal/nifi-splitcreateattribute

Below is the config needed:

Split Attributes Name: ATTR1, ATTR2, ATTR3, ATTR4 Attribute Name Which Split: filename\ Split Separator (Regex): _

Upvotes: 1

Related Questions