Apache Nifi 1.6: crash Groovy script

喜你入骨 提交于 2020-06-01 06:20:14

问题


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?


回答1:


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))


来源:https://stackoverflow.com/questions/62039371/apache-nifi-1-6-crash-groovy-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!