Skip to content
Advertisement

Use pattern in getAttribute in NiFi

How can I use a pattern in getAttribute of a FlowFile?

I’m going to write a processor that receives flowfiles from ListenTCP and ListenUDP processors. ListenTCP has tcp.sender property and ListenUDP has udp.sender property. How can I get the sender property of a FlowFile? The current solution is:

String sender = flowfile.getAttribute("tcp.sender");
if(sender!=null && !sender.isEmpty()) {
     // do something
}
else {
    sender = flowfile.getAttribute("udp.sender");
    if(sender!=null && !sender.isEmpty()) {
        //do something
    }
}

How can I avoid using if? I need something like this:

String sender = flowfile.getAttribute("*.sender");

Advertisement

Answer

There currently isn’t a way to get an attribute based on a pattern. If there was, it would return a list of multiple attribute values, and you will still have to go through the list and find the one you are interested in.

You could make your custom processor require an attribute like “network.sender” and after ListenTCP and ListenUDP, have an UpdateAttribute processor for each of them that renames “tcp.sender” to “network.sender” and “udp.sender” to “network.sender”.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement