Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

Highlighted

Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

Hello all,

Scenario brief :

I'm trying to create a new Nifi Processor by the use of REST as referred here

https://nifi.apache.org/docs/nifi-docs/rest-api/

I've attached the groovy script that creates the JSON to be consumed by the POST method as mentioned in the docs, and have formatted the builder{} object contents in such a way that it returns the correct JSON that can create a GetFile Processor in currently configured nifi GUI.

JSON :

{ "revision": { "clientId": "CID", "version": 2655 }, "component": { "name": "newgetfileproc", "type": "GetFile", "state": "STOPPED", "description": "value" }, "id": "1234", "permissions": { "canRead": true, "canWrite": true } }

Problem Statement :

After I pass on the below values in POST method, and run it, it reflects nothing in the canvass where i'm expecting a new processor to get created.

try
{
resp = nifi.post(

        path: "/controller/process-groups/7d15606e-8d9b-492a-811a-d2da391eed91",
requestContentType: JSON,
body: builder2.toPrettyString()

)

There is currently no exception from the try block, but when I print the resp that returned from POST(),

print prettyPrint(toJson(resp.data))

i get the below exception

groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChildren.isArray() is applicable for argument types: () values: []
Possible solutions: isEmpty(), every()
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:51)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
        at groovy.json.JsonOutput.toJson(JsonOutput.groovy:123)
        at groovy.json.JsonOutput$toJson$4.callStatic(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:50)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:157)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:165)
        at groovy_create_test_bkp.run(groovy_create_test_bkp.groovy:76)
        at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
        at groovy.lang.GroovyShell.run(GroovyShell.java:229)
        at groovy.lang.GroovyShell.run(GroovyShell.java:159)
        at groovy.ui.GroovyMain.processOnce(GroovyMain.java:550)
        at groovy.ui.GroovyMain.run(GroovyMain.java:337)
        at groovy.ui.GroovyMain.process(GroovyMain.java:323)
        at groovy.ui.GroovyMain.processArgs(GroovyMain.java:120)
        at groovy.ui.GroovyMain.main(GroovyMain.java:100)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
        at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)


I am stuck in here and cant figure out whats causing it, can it be that the JSON is wrong? even though I followed the ProcessorEntity format from nifi rest-api doc, or is it the path that has been given wrong?

Any help is appreciated.

CODE:

groovy-create-test-bkp-groovy.txt

8 REPLIES 8
Highlighted

Re: Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

Guru

The error definitely is not complaing about bad json.

It is complaining that the method signature in

print prettyPrint(toJson(resp.data))

is wrong. Specifically, the object type of resp.data is not a type that toJson() accepts as shown in the stack trace:

groovy.lang.MissingMethodException:No signature of method:

...

at groovy.json.JsonOutput.toJson(JsonOutput.groovy:123)

You are passing in resp.data. Find its type by

println resp.data.getClass()

Whatever its type, it must be converted to the allowable types as specified here: http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonOutput.html

I am guessing you have to convert it to a string ... something like

print prettyPrint(toJson(resp.data.toString()))
Highlighted

Re: Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

groovy-create-test-2.txt@Greg Keys

Thanks Greg for the explanation, rectifying the response print line worked out fine with the message. But, i'm afraid the main issue still remains. I have attached the script again with some modification. The problem that the post method is not working properly has now two aspects.

1. This actually should work fine as far POST goes. I define a RESTClient object with host:port/nifi-api as below

and then in the POST declare the path as : controller/process-groups/root/processors

Note that after path: I do not put another '/' to define it since I have already create the object nifi with http://$host:$port/nifi-api/ (with / in the end)

def nifi = new RESTClient("http://$host:$port/nifi-api/")
....
....

def resp2 = nifi.post(	
	path:"controller/process-groups/root/processors",
	requestContentType: JSON,
	body: builder2.toPrettyString()
   	)

in this case, i'm getting below error

groovyx.net.http.HttpResponseException: Bad Request
        at groovyx.net.http.RESTClient.defaultFailureHandler(RESTClient.java:263)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1047)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:914)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
        at groovy.lang.Closure.call(Closure.java:412)
        at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1070)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1044)
        at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
        at groovyx.net.http.RESTClient.post(RESTClient.java:141)
        at groovyx.net.http.RESTClient$post.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
        at groovy_create_test_bkp.run(groovy_create_test_bkp.groovy:58)
        at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
        at groovy.lang.GroovyShell.run(GroovyShell.java:229)
        at groovy.lang.GroovyShell.run(GroovyShell.java:159)
        at groovy.ui.GroovyMain.processOnce(GroovyMain.java:550)
        at groovy.ui.GroovyMain.run(GroovyMain.java:337)
        at groovy.ui.GroovyMain.process(GroovyMain.java:323)
        at groovy.ui.GroovyMain.processArgs(GroovyMain.java:120)
        at groovy.ui.GroovyMain.main(GroovyMain.java:100)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
        at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)

2. Everything same with 1, just if i try with an extra '/' in path: as below.

def resp2 = nifi.post(
path:"/controller/process-groups/root/processors",
requestContentType: JSON,
body: builder2.toPrettyString()
   )

I'm not getting any exception, but in response I'm getting

NiFi
                Did you mean: /nifiYou may have mistyped...

In either case, obviously the nifi GUI is not getting updated with any processor that I intend to create, and I cant figure out if its a path related problem or not. Can you help?

Highlighted

Re: Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

Guru

The path string definitely does not have a / at the beginning nor end of the string.

The path must also state the process grooup id and processor id, e.g.

path:"controller/process-groups/$processGroup/processors/$processorId",

or

path:"controller/process-groups/7d15606e-8d9b-492a-811a-d2da391eed91/processors/d15606e-8d9b-492a-811a-d2da353daa74",

See the following:

Highlighted

Re: Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

I'm afraid , @Greg, the references that you are pointing to, are the sources from where I started the development of this very approach where I'm stuck. I built on that particular groovy script and followed REST-api docs very closely while doing that. And these do not provide me with a solution, its like a been-there-done-that case now. What I could use is some experience with more practical implementation if anyone have ever done that.

Highlighted

Re: Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

Guru

@sam coderunner Totally understand. I did notice some fundamental mistakes in your code that deviated from the code examples, including path issues as identified above. My suggestion is to continue to work with these examples, but build your script up slowly: add one piece of the code at a time and then see where it breaks, then focus on the last thing you added, fix that and move on. The code examples are from nifi experts and should be good guidance. This stuff is quite meticulous though.

If you get stuck repeatedly on one known part of the script, maybe post a new question on that, since the issue originally posted here was solved but exposed a new issue and resulted in quite a complicated thread.

Highlighted

Re: Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

Mentor

I would recommend debugging your script using nifi-script-tester utility that author of ExecuteScript processor created to mock/test scripts. More info here

http://funnifi.blogspot.com/2016/06/testing-executescript-processor-scripts.htm

Re: Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

Artem,

The script debugger you link to no longer exists. Has it been moved to a new location?

Highlighted

Re: Create A Nifi Processor through Groovy Script and REST Api : POST() Fail

Super Guru

Not sure why the link got moved, this one should be correct: http://funnifi.blogspot.com/2016/06/testing-executescript-processor-scripts.html?m=1

Don't have an account?
Coming from Hortonworks? Activate your account here