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?

5
  • 1
    Script is acceptable? Commented Feb 6, 2020 at 10:29
  • @daggett Yes, I was trying with groovy, but my groovy knowledge is not enough for this. Commented Feb 6, 2020 at 10:36
  • 1
    the problem that {dep_name:computer,location:2323,3434} is not a valid json... it should be as minimum like this: {dep_name:computer,location:[2323,3434]}. is it possible to fix this? Commented Feb 6, 2020 at 12:08
  • @daggett what if is its in {"dep_name":"computer","location":"2323,3434"}? This is valid, right? Commented Feb 6, 2020 at 12:11
  • yep. with this it's possible to work Commented Feb 6, 2020 at 12:16

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Failed to process session due to javax.script.ScriptException.... No Signature of method... Possible solutions: wait(),wait(long)..
I think you are not using ExecuteGroovyScript
Yes, It was my mistake. What is the difference between ExecuteScript -> groovy and ExecuteGroovyScript
ExecuteGroovyScript has more groovy features. It compiled once (on code change). It supports start-stop event handling. Better error displaying. Finally it just newer )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.