Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Can i stop InvokeHttp processor by another processor?

avatar
Contributor

I have service invokation in invokeHTTP processor and it has relaited response i want to stop invokehttp as soon as response fails is it possible to use another invokehttp processor for rest api to stop previous invokehttp or i will need to generate another flowfile for it?

1 ACCEPTED SOLUTION

avatar

@sally sally

I don't have a specific link to a doc, but here is an example of what I did once using the ExecuteStreamCommand processor.

Here is a a snapshot of the configuration of the ExecuteStreamCommand processor. My particular example stops and starts the processor. Also, you can put the script anywhere the NiFi process is able to access and execute it.

30381-screen-shot-2017-08-17-at-41234-pm.png

Here is the script, this script had to retrieve a token, because my particular instance of NiFi was secured and used LDAP for authentication. If your instance of NiFi isn't secured then you could skip that first step and none of your curl commands will need the -H 'Authorization: Bearer'$token'' parameter. In addition, you'll have to change the processor uuid to match the uuid of the InvokeHttp processor and the URL of the NiFi instance should match yours.

#!/bin/bash

# Get access token from LDAP server
token=$(curl 'https://nifi-server:9091/nifi-api/access/token' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' --data 'username=username&password=password' --compressed --insecure --tlsv1.2)
#echo token = $token

# Get the latest version of the flow
version=$(curl 'https://nifi-server:9091/nifi-api/processors/62c34df2-015b-1000-89e3-e2e77533331e' -X GET -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Authorization: Bearer '$token'' --compressed --insecure --tlsv1.2 | awk -F'[/:]' '{print $4}' | head -c2)
#echo version = $version

# This command is stopping the processor
curl 'https://nifi-server:9091/nifi-api/processors/62c34df2-015b-1000-89e3-e2e77533331e' -X PUT -H 'Host: nifi-ambari-02:9091' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://nifi-ambari-02:9091/nifi/' -H 'Content-Type: application/json' -H 'Authorization: Bearer '$token'' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' --data '{"revision":{"clientId":"6386076d-015b-1000-2a22-714f84689191","version":'$version'},"component":{"id":"62c34df2-015b-1000-89e3-e2e77533331e","state":"STOPPED"}}' --insecure --tlsv1.2
echo
sleep 15

# This step starts the processor
curl 'https://nifi-server:9091/nifi-api/processors/62c34df2-015b-1000-89e3-e2e77533331e' -X PUT -H 'Host: nifi-ambari-02:9091' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://nifi-ambari-02:9091/nifi/' -H 'Content-Type: application/json' -H 'Authorization: Bearer '$token'' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' --data '{"revision":{"clientId":"6386076d-015b-1000-2a22-714f84689191","version":'$version'},"component":{"id":"62c34df2-015b-1000-89e3-e2e77533331e","state":"RUNNING"}}' --insecure --tlsv1.2
# Echo a blank line
echo

View solution in original post

6 REPLIES 6

avatar
Contributor

I have tried restapi with put request i used links like this in my another invokehttp processsor http://localhost:8080/nifi-api/processors/ea5db028-015d-1000-5ad5-80fd006dda92 and now i want to know ow can i link json components like this to my put request body?

{
  "status": {
    "runStatus": "STOPPED"
  },
  "component": {
    "state": "STOPPED",
    "id": "ea5db028-015d-1000-5ad5-80fd006dda92"
  },
  "id": "ea5db028-015d-1000-5ad5-80fd006dda92",
  "revision": {
    "version": 46,
    "clientId": "ef592126-015d-1000-bf4f-93c7cf7eedc0"
  }
} 




avatar
@sally sally

You can use the response output to trigger another processor, you won't have to generate another flow file. A better processor to use would be the ExecuteScript or ExecuteStreamCommand processor to make the rest api call.

avatar
Contributor

Can you give me a link of example how can i do it, I have tried this but it doesn't work for me:

For GenerateFlowFiele i  have:

[{
  "status": {
    "runStatus": "STOPPED"
  },
  "component": {
    "state": "STOPPED",
    "id": "ea5db028-015d-1000-5ad5-80fd006dda92"
  },
  
  "revision": {
    "version": 46,
    "clientId": "ef592126-015d-1000-bf4f-93c7cf7eedc0"
  }
} ]
and related groovy code in executeScript processor:

import org.apache.commons.io.IOUtils
import java.nio.charset.*
def flowFile = session.get();
if (flowFile == null) {
    return;
}


def slurper = new groovy.json.JsonSlurper()
def attrs = [:] as Map<String,String>
 
session.read(flowFile,
    { inputStream ->
        def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
		text=flowFile.getAttribute('text')
        def obj = slurper.parseText(text)
		
        obj.each {k,v ->
           attrs[k] = v.toString()
        }
    } as InputStreamCallback)
flowFile = session.putAllAttributes(flowFile, attrs)
session.transfer(flowFile, REL_SUCCESS)

avatar

@sally sally

I don't have a specific link to a doc, but here is an example of what I did once using the ExecuteStreamCommand processor.

Here is a a snapshot of the configuration of the ExecuteStreamCommand processor. My particular example stops and starts the processor. Also, you can put the script anywhere the NiFi process is able to access and execute it.

30381-screen-shot-2017-08-17-at-41234-pm.png

Here is the script, this script had to retrieve a token, because my particular instance of NiFi was secured and used LDAP for authentication. If your instance of NiFi isn't secured then you could skip that first step and none of your curl commands will need the -H 'Authorization: Bearer'$token'' parameter. In addition, you'll have to change the processor uuid to match the uuid of the InvokeHttp processor and the URL of the NiFi instance should match yours.

#!/bin/bash

# Get access token from LDAP server
token=$(curl 'https://nifi-server:9091/nifi-api/access/token' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' --data 'username=username&password=password' --compressed --insecure --tlsv1.2)
#echo token = $token

# Get the latest version of the flow
version=$(curl 'https://nifi-server:9091/nifi-api/processors/62c34df2-015b-1000-89e3-e2e77533331e' -X GET -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Authorization: Bearer '$token'' --compressed --insecure --tlsv1.2 | awk -F'[/:]' '{print $4}' | head -c2)
#echo version = $version

# This command is stopping the processor
curl 'https://nifi-server:9091/nifi-api/processors/62c34df2-015b-1000-89e3-e2e77533331e' -X PUT -H 'Host: nifi-ambari-02:9091' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://nifi-ambari-02:9091/nifi/' -H 'Content-Type: application/json' -H 'Authorization: Bearer '$token'' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' --data '{"revision":{"clientId":"6386076d-015b-1000-2a22-714f84689191","version":'$version'},"component":{"id":"62c34df2-015b-1000-89e3-e2e77533331e","state":"STOPPED"}}' --insecure --tlsv1.2
echo
sleep 15

# This step starts the processor
curl 'https://nifi-server:9091/nifi-api/processors/62c34df2-015b-1000-89e3-e2e77533331e' -X PUT -H 'Host: nifi-ambari-02:9091' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: https://nifi-ambari-02:9091/nifi/' -H 'Content-Type: application/json' -H 'Authorization: Bearer '$token'' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' --data '{"revision":{"clientId":"6386076d-015b-1000-2a22-714f84689191","version":'$version'},"component":{"id":"62c34df2-015b-1000-89e3-e2e77533331e","state":"RUNNING"}}' --insecure --tlsv1.2
# Echo a blank line
echo

avatar
Contributor

thnank you

avatar
@sally sally

If the answer helped, please accept it.

Thanks