【问题标题】:Framing JSON object in groovy - Streamsets在 groovy 中构建 JSON 对象 - Streamsets
【发布时间】:2022-11-15 23:30:30
【问题描述】:

我对 Streamsets 很陌生,我发现在我的 Groovy Evaluator 对象中构建 JSON 对象有点令人困惑和具有挑战性。

我需要构建以下 JSON:

{
    "filter": "(equals(type,'my/specific/Type') and equals(attributes.number, '1234') and (equals(attributes.status,'ACTIVE'))",
    "max": 10
}

我试过这个:

import groovy.json.*

records = sdc.records
for (record in records) {
    try {
       event = "{"filter": "(equals(type,'my/specific/Type') and equals(attributes.number, '1234') and (equals(attributes.status,'ACTIVE'))","max": 10}"
       record.value = event

        // Write a record to the processor output
        sdc.output.write(record)
    } catch (e) {
        // Write a record to the error pipeline 
        sdc.log.error(e.toString(), e)
        sdc.error.write(record, e.toString())
    }
}

但我收到以下错误:

SCRIPTING_03 - 脚本编译失败: 'javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: 启动失败:Script1076.groovy:6:意外令牌:和@第6行, 第 59 列.uals(type,'my/specific/Type') and equals ^ 1 error '

请帮助解决这个问题。

【问题讨论】:

  • event = ... 行的字符串值不正确。
  • 我能理解!! :) 什么是不正确的字符串以及如何更改它是我的问题。
  • 使用单引号包裹字符串,即event = '{"filt.... 而不是event = "{"filt....
  • 收到了这个。 com.streamsets.pipeline.api.base.OnRecordErrorException: SCRIPTING_04 - Script sent record to error: groovy.lang.MissingPropertyException: No such property: configuration for class: Script1082 at com.streamsets.pipeline.stage.processor.scripting.ScriptingProcessorInitDestroyBindings$Err.write(ScriptingProcessorInitDestroyBindings.java:48) at com.streamsets.pipeline.stage.processor.scripting.ScriptingProcessorInitDestroyBindings$Err$write.call(Unknown Source)

标签: groovy streamsets


【解决方案1】:

这不是关于 JSON 本身,而是关于转义双引号字符。 Groovy 只不过是 Java + 一些语法糖,所以在 Groovy 中转义 " 字符是相同的:使用 ",例如:

foo = "I use so called "Groovy" for that"

【讨论】:

    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    相关资源
    最近更新 更多