Search code examples
groovyapache-nifi

NiFi processor to search and replace multiple words in a txt file


I have txt file which I am reading using FetchSFTP in NiFi. Also, I have key and values in json format, as shown below received after REST call and JoltTransformJSON:

[{
    "Key": "k2s2e2",
    "Value": "Ottawa"
}, {
    "Key": "60601",
    "Value": "Chicago"
}, {
    "Key": "",
    "Value": "London"
}]

How can I replace all the occurrences of matching key from above to its value in txt file.

Example: abc.txt

000 apple stocks at k2s2e2 888
9000 samsung stocks at 60601 9990377
88 nokia devivces at 78889 790888071 hgj 7

Output:

000 apple stocks at Ottawa 888
9000 samsung stocks at Chicago 9990377
88 nokia devivces at 78889 790888071 hgj 7

My attempt using ExecuteGroovyScript:

import static groovy.json.JsonOutput.toJson

import java.nio.charset.StandardCharsets

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

class KeyValue{
    String key
    String value
}

private findAndReplace(KeyValueList) {
    def response
    KeyValueList.each {
    def srcExp = it.key
    def replaceText = it.value

   def inputFilepath = "C:\\Project\\abc.txt"
    def outputFilepath = "C:\\Project\\abc_output.txt"
    
    new File(outputFilepath).withWriter { w ->
        new File(inputFilepath).eachLine { line ->
          w << line.replaceAll(srcExp , replaceText ) << '\n'
        }
      response = w
      }
new File(inputFilepath).text= new File(outputFilepath).text
}
return response;
}

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

def KeyValueList = []
//try {
        def is = flowFile.read().withReader("UTF-8"){ new JsonSlurper().parse(it) }
        is.each {
               if(it.Key != "") {
                    KeyValue keyValue = new KeyValue(key:it.Key,value:it.Value)
                    KeyValueList.add(keyValue)
                 }
            }
          def retval =  findAndReplace(KeyValueList)
          flowFile = session.write(flowFile, {outputStream ->
               outputStream.write(retval.toString().getBytes(StandardCharsets.UTF_8))
           } as OutputStreamCallback)
    
    session.transfer(flowFile, REL_SUCCESS)
//}catch(Exception e) {
 //   log.info(e.getMessage())
//    REL_FAILURE << flowFile
//}

Solution

  • it's not a response to your question. just a try to fix your code.

    if i understand correctly you are trying to

    • read json from flowfile
    • read text from some file path
    • write text with replacement to flowfile

    code for ExecuteGroovyScript processor

    import groovy.json.JsonSlurper
    
    def ff = session.get()
    if(!ff) return
    
    ff.write{rawIn, rawOut->
        def keyValueList = rawIn.withReader("UTF-8"){ new JsonSlurper().parse(it) }
    
        new File('c:/Project/abc.txt').withReader("UTF-8"){reader->
            rawOut.withWriter("UTF-8"){writer->
                reader.eachLine{line->
                   keyValueList.each{ if(it.Key) line = line.replaceAll(it.Key, it.Value) }
                   writer << line << '\n'
                }
            }
        }
    }
    
    REL_SUCCESS << ff
    

    don't have time to test it...