Apache Nifi 1.6:Groovy脚本崩溃 [英] Apache Nifi 1.6: crash Groovy script

查看:136
本文介绍了Apache Nifi 1.6:Groovy脚本崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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列. [[position:[data:[[code:01.11.11.111-000 (在此simbol上:[[position:[data:[[code:01.11. 1 1.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆