0

My csv contain

date,name,department
2020-2-4,sachith,{dep_name:computer,location:2323,3434}
2020-2-5,nalaka,{dep_name:engineering,location:3343,5454}

final csv should be like :

date,name,dep_name,lat,lot
2020-2-4,sachith,computer,2323,3434
2020-2-5,nalaka,engineering,3343,5454

here lat,lot are taken from location:3343,5454 data.

I have tried to use UpdateRecord processor for this. In it has some ${field.value:join(','):substringAfter('dep_name:')}

But its not working. How can I complete this using apache-nifi?

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94

1 Answers1

2

plain groovy to test script in groovyConsole:

import groovy.json.*

def parser = new JsonSlurper().setType(JsonParserType.LAX) //LAX to accept strings without double-quotes

def w = System.out
new StringReader('''date,name,department
2020-2-4,sachith,{"dep_name":"computer","location":"2323,3434"}
2020-2-5,nalaka,{"dep_name":"engineering","location":"3343,5454"}''').withReader{r->
    r.eachLine{line, lineNum->
        if(lineNum==1){
            w<<line<<',lon,lat'<<'\n'
        }else{
            def row=line.split(',')          //split line by coma
            def json=row[2..-1].join(',')    //join back to string starting from 3rd element
            json = parser.parseText(json)
            w<<"${row[0]},${row[1]},${json.dep_name},${json.location}"<<'\n'
        }
    }
}

now the same script modified for nifi ExecuteGroovyScript processor:

import groovy.json.*

def ff=session.get()
if(!ff)return

def parser = new JsonSlurper().setType(JsonParserType.LAX)

ff.write{streamIn,streamOut->
    streamIn.withReader('UTF-8'){r->      //convert in stream to reader
        streamOut.withWriter('UTF-8'){w-> //convert out stream to writer
            //go line by line
            r.eachLine{line, lineNum->
                if(lineNum==1){
                    w<<line<<',lon,lat'<<'\n'        //for the first line just add some headers
                }else{
                    def row=line.split(',')          //split line by coma
                    def json=row[2..-1].join(',')    //join back to string starting from 3rd element
                    json = parser.parseText(json)
                    w<<"${row[0]},${row[1]},${json.dep_name},${json.location}"<<'\n'
                }
            }
        }
    }
}
REL_SUCCESS<<ff

daggett
  • 26,404
  • 3
  • 40
  • 56
  • `Failed to process session due to javax.script.ScriptException.... No Signature of method... Possible solutions: wait(),wait(long)..` – Sachith Muhandiram Feb 06 '20 at 14:49
  • 1
    I think you are not using `ExecuteGroovyScript` – daggett Feb 06 '20 at 14:56
  • Yes, It was my mistake. What is the difference between `ExecuteScript -> groovy` and `ExecuteGroovyScript` – Sachith Muhandiram Feb 06 '20 at 15:11
  • 1
    ExecuteGroovyScript has more groovy [features](https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-groovyx-nar/1.9.0/org.apache.nifi.processors.groovyx.ExecuteGroovyScript/additionalDetails.html). It compiled once (on code change). It supports start-stop event handling. Better error displaying. Finally it just newer ) – daggett Feb 06 '20 at 15:51