Apache Nifi 1.6:崩溃 Groovy 脚本 [英] Apache Nifi 1.6: crash Groovy script
问题描述
在 Apache Nifi 中解析 json 中的 xml.在数据集有限的本地机器上,我的代码工作.在完整数据集中,在服务器 Apache Nifi 上,当收集 JSON 时,某些值会导致错误.
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.
完整脚本:
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)
}
错误日志:groovy.json.JsonException:期望没有参数、单个映射、单个闭包或一个映射和闭包作为参数.
当我从服务器获取 LinkedHashMap 时出错,我在本地机器上收到此错误:意外输入:[[位置:[数据:[[代码:01.11.11.111'@第2行,第47列.[[位置:[数据:[[代码:01.11.11.111-000(在这个符号上:[[位置:[数据:[[代码:01.11.11.111-000])
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)
Pastebin 上的完整错误地图 https://pastebin.com/vLu6ES9Q
Full error Map on Pastebin https://pastebin.com/vLu6ES9Q
如何解决?
推荐答案
这段代码的问题:
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
添加 .toString()
或 .toPrettyString()
应该可以解决问题
adding .toString()
or .toPrettyString()
should fix the problem
def json = new groovy.json.JsonBuilder( jsonObject ).toPrettyString()
outputStream.write(json.getBytes(StandardCharsets.UTF_8))
这篇关于Apache Nifi 1.6:崩溃 Groovy 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!