Apache Nifi 1.6: crash Groovy script

In Apache Nifi parse xml in json. On local machine with limited data set my code work. In full data set, on server Apache Nifi, when JSON is collect, some values lead to errors.

Full script:

import groovy.json.*
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
import org.apache.nifi.processor.io.StreamCallback

def get_map(Node) {
    nodeRootName = Node.name() 
    if (Node.childNodes().size() == 0) {
        return [(nodeRootName): (Node.text())]
    } else {
        subMap = [(nodeRootName):[]]
        for (subNode in Node.childNodes()) {
            subMap.(subMap.keySet()[0]).add(get_map(subNode))    
        }
        return subMap
    }
}
def check = true
flowFile = session.get()
if(!flowFile) return
session.write(flowFile, {
    inputStream, outputStream ->
        def raw = IOUtils.toString(inputStream, 'UTF-8')
        def xml = new XmlSlurper().parseText(raw)
        def jsonObject = [(xml.nsiKTRUs.name()): []]
        for (node in xml.nsiKTRUs.childNodes()) {
            rootNodeName = node.name()
            nodeMap = [(rootNodeName): [data:[]]] 
            for (subNode in node.childNodes()[0].childNodes()) {
                if (subNode.name() != "cryptoSigns") {
                    nodeMap.position.data.add(get_map(subNode))
                }
            } 
        jsonObject.nsiKTRUs.add(nodeMap)
    }
        try {
            def json = new groovy.json.JsonBuilder( jsonObject )
            outputStream.write(json.getBytes(StandardCharsets.UTF_8))
        } catch(Exception ex) {
            check = false
            outputStream.write(ex.toString().getBytes(StandardCharsets.UTF_8))
        }
    } as StreamCallback
)
if (check) {
    session.transfer(flowFile, REL_SUCCESS)
} else {
    session.transfer(flowFile, REL_FAILURE)
}

Error log: groovy.json.JsonException: Expected no arguments, a single map, a single closure, or a map and closure as arguments.

When I take LinkedHashMap from the server with an error, I get this error on the local machine: Unexpected input: [[position:[data:[[code:01.11.11.111' @ line 2, column 47. [[position:[data:[[code:01.11.11.111-000 ( on this simbol: [[position:[data:[[code:01.11.11.111-000)

Full error Map on Pastebin https://pastebin.com/vLu6ES9Q

How fix it?

Upvotes: 0

Views: 656

Answers (1)

daggett
daggett

Reputation: 28564

problem in this code:

def json = new groovy.json.JsonBuilder( jsonObject )      // <--- at this point `json` is a JsonBuilder object
outputStream.write(json.getBytes(StandardCharsets.UTF_8)) // JsonBuilder does not have .getBytes(StandardCharsets.UTF_8) method

adding .toString() or .toPrettyString() should fix the problem

def json = new groovy.json.JsonBuilder( jsonObject ).toPrettyString()
outputStream.write(json.getBytes(StandardCharsets.UTF_8))

Upvotes: 2

Related Questions