Block (or strip out) FlowFile Content - apache-nifi

I need to perform an HTTP Post from NiFi, but I don't want/need the request to carry all of the FlowFile's content.
Is there some way to pass attributes of a FlowFile but not the full content?

If the request body of your Http Post is JSON, you can use the AttributesToJSON Processor which allows you to pick which attributes you want to include in the resulting JSON. You can then configure the processor so the resulting JSON overwrites the existing flowfile content.
Keep in mind that the resulting JSON will be flat so you may need to transform it to the expected format. For that, you can use the JoltTransformJSON Processor.
Below is an example of what your dataflow might look like. I hope this helps!

Related

Apache NiFi: How to update an attribute of flowfile with a Http response from API?

I want to report to an API with some information with attributes of each flowfile. Sometimes the API return some important information in JSON. My goal is update the attributes of the original flowfile with the new data that return the API.
My sketch-strategy to update the FlowFile -> AttributeToJSON (But the entire content of the FlowFile is replaced by the JSON, 1°problem) -> HttpInvoke to send the information to the API -> The API return a JSON with some data -> Extract some data from the JSON with some Process and update the attributes of the Flowfile
1° problem: I can separate the flowfile, the original and another (to modify it with AttributeToJSON). But how can I merge them in the future? Which process I need to combine the original flowfile and the "new" attributes that I build with the response of the API?
Perhaps I can save the orginal file in a directory with PutFile, and by another way process the info, and some point use the FetchFile (with attributes know where is save the file), and then take the data and Attributes together.
extra Can I send with HttpInvoke POST Request with only the attributes(one of them written in JSON)?.
You may want to take a look at the lookup processors -- LookupAttribute and LookupRecord. These processors allow you to enrich the existing flowfile with additional information.
It looks like right now, the RestLookupService is available for record enrichment but not attribute enrichment. You may want to file a Jira requesting this, and in the meantime you can use SimpleScriptedLookupService to make an HTTP invocation from that processor.

problem while using RouteOnAttribute (cannot read json attribute and always sends flow to unmatch)

while using RouteOnAttribute nifi processor , i have input of json data
[{"dev":"xyz","detail":"abc"}] which i got from convertRecord processor
Routing Strategy :Route to Property name
ifmatch: ${dev:equals( "xyz" )}
I tried ${dev:matches( "xyz")} in both single quotes and double quotes still am not getting flowfile redirecting towards "ifmatch" . its redirecting to unmatched
is there anyway to resolve this i tried many other option
The flowfile content is different from attributes. Content is arbitrary -- could be empty, text, KB of XML, GB of video or binary. Each flowfile also has attributes which are key/value pairs of Strings kept in memory.
If you want to route on this piece of data, you have multiple options:
Use RouteOnText or RouteOnContent to use the actual flowfile content directly.
Extract it to an attribute using EvaluateJsonPath and then route on that attribute.
The Apache NiFi User Guide and In-Depth provide more information around this distinction.

In NiFi how do you send binary files to HTTP Rest?

I have following business need. Can anybody please suggest me NiFi WorkFlow I should create? Thanks
1) Through Kakfa I get metadata as JSON Object. This JSON Object has an image or video which is in binary format. This binary file is pretty huge.
2) I need to extract binary data and send it to HTTP rest (POST).
In my mind I have following workflow:
ConsumeKakfa==>EvaluateJsonPath==>UpdateAttributes=>InvokeHTTP
Explanation:
1) ConsumeKakfa will receives metadata as json object.
2) EvaluateJsonPath will extract content json attribute which has image or video data stored as base64.
3) UpdateAttribute will update the flowfile to insert POST payload.
4) InvokeHTTP will invoke POST HTTP rest call.
I am not sure whether huge data will be handle by InvokeHTTP.
your flow should be like this:
ConsumeKafka
EvaluateJsonPath (destination=content) stores evaluated base64 binary into flowfile content
Base64EncodeContent (decode) decodes base64 content into a binary
InvokeHTTP sends everything in content as a body

In NiFi processor 'InvokeHTTP' where do you write body of POST request?

Before posting this question about Apache NiFi InvokeHTTP I have gone through all other questions and their answersbut I am still unsure the best flow I should have. My situation is as below:
1) From Apache Kakfa, I get raw metadata.
2) Using EvaluateJSONPath I get attribute I want.
3) Using RouteOnAttribute I created 3 routes based on the attribute value I got from step-2 above.
4) Now based on the attribute value I want to decide whether I should go for GET or for POST or for Delete.
5) My question is where/how to set POST message? GET message? Delete Message body?
6) I am able to set the URL in configuration part provided by InvokeHTTP. But message body I don't know which is that property? or its in flow file using ReplaceText?
I read somewhere that before you divert your Restful POST HTTP request to InvokeHTTP you must have another processor before which changes the content of flow file.
Ref: Configuring HTTP POST request from Nifi
Please help. Thanks.
regards,
Yeshwant
Adding on to what Bryan had explained, POST will use the FlowFile content as the message body so if you have some other data which you want to wipe/transform into something and then sent as the message body, you can leverage the following processors :
ExtractText to read data from the existing FlowFile content
ReplaceText to erase the existing content of the FlowFile and replace it with different one
To set the headers for the REST calls, InvokeHTTP has the property Attributes to Send property which takes a regex which will scanned against the incoming FlowFiles' attributes and whichever attributes are matched are taken and sent as HTTP header.
To add new attribute to your existing FlowFile, you can use UpdateAttribute
For a POST, the body will be whatever is in the flow file content.
a GET and DELETE typically wouldn't have a body since the information would typically be provided in the URL or query params.

Using flowfile content

New to NiFi!
I've split a flowfile into a single line of text using splitJSON processor.
The NiFi flowfile contents are as follows:
abcdefg
I'd like to be able to take the text in the flowfile and either add it to a url to make a subsequent call using InvokeHTTP or add the contents of the flowfile as an attribute so I can make the subsequent call using InvokeHTTP like so
http://localhost/${my.newly.added.attribute}
How do i do this?
Any help would be appreciated!
Thanks in advance!
ExtractText will allow you to find sections of content and place in an attribute on the FlowFile. For your example, you could capture the entirety of the content and assign to an attribute my.newly.added.attribute. InvokeHTTP would then access it using Expression Language 2 as in your example.

Resources