我正在使用Apache Nifi,我的一个流文件是一个稍微格式错误的 Json:
{
"field" : "value",
"field1" : "value1"
}0;0
我不想使用之前应用的转换,而是使用 Groovy 脚本作为ExecuteScript的一部分。这就是我目前所拥有的:
import org.apache.nifi.processor.io.StreamCallback
import java.nio.charset.StandardCharsets
import org.apache.commons.io.IOUtils
import java.nio.charset.*
def flowFile = session.get()
if (!flowFile) return
def slurper = new groovy.json.JsonSlurper()
flowFile = session.write(flowFile, { inputStream, outputStream ->
def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
def resultingText = text.substring(0, text.indexOf('}'))
def json = slurper.parseText(resultingText)
outputStream.write(json.toString().getBytes(StandardCharsets.UTF_8))
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
但是,我返回以下错误:
ExecuteScript[id=69ae1948-f20b-446c-b33f-298c6faa7c98] Failed to process session due to org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: javax.script.ScriptException: groovy.json.JsonException: expecting '}' or ',' but got current char [SPACE] with an int value of 32
The current character read is [SPACE] with an int value of 32
expecting '}' or ',' but got current char [SPACE] with an int value of 32
line number 5
index number 61
...^: org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: javax.script.ScriptException: groovy.json.JsonException: expecting '}' or ',' but got current char [SPACE] with an int value of 32
The current character read is [SPACE] with an int value of 32
expecting '}' or ',' but got current char [SPACE] with an int value of 32
line number 5
index number 61
...^
我在做任何明显错误的事情吗?谢谢您的帮助。