可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
i have processor InvokeHTTP which gives json result. for instance:
{ "revision" : { "clientId" : "dc572274-4b71-11b6-e415-b91e391bcf4d", "version" : 7 }, "id" : "dc572260-4b71-11b6-0371-f73573ab44fe", "uri" : "http://x.x.x.x:9090/nifi-api/processors/dc572260-4b71-0371-73ab44fe", "position" : { "x" : -1021.9568138214972, "y" : 333.2029958718132 } }
i want to change the value of version dynamically for each incoming response of InvokeHTTP. how to achieve this?
回答1:
i could suggest ExecuteScript processor with groovy language
import groovy.json.* def ff = session.get() if(!ff) return ff = session.write(ff, {rawIn, rawOut-> //parse flowfile content to maps & arrays def json = new JsonSlurper().parse(rawIn, "UTF-8") //change json json.revision.version = (json.revision.version as Long) + 1 //write to output changed content rawOut.withWriter("UTF-8"){ it.write( JsonOutput.toJson(json) )} } as StreamCallback) session.transfer(ff, REL_SUCCESS)
回答2:
you can use this sequence of processors: EvaluateJsonPath to get value of attribute, UpdateAttribute to change it, and ReplaceText to substitute old value in content with new one using regular expression.
For the ReplaceText processor use following parameters:
Regexp Replace strategy
Search Value : (?s)("version"\s*:\s*)(\d+)
Replacement Value : $1${VERSION} (where VERSION is an attribute name that holds new value)
Here is a nice resource to test the regular expressions: https://regex101.com/r/JOrZNp/1