Support Questions
Find answers, ask questions, and share your expertise

Apache Nifi: Dynamic topic for Publish MQTT

Explorer

Hi, is there anyway I can configure the publisher to publish to a certain topic based on the content of the message? It is not practical that I create 100 publishers. Also, for ConsumeMQTT, is there no way for me to get the topic that came in with the message? Currently, I subscribe to test/+/+.

3 REPLIES 3

Re: Apache Nifi: Dynamic topic for Publish MQTT

Hi @Adrian sss,

Regarding PublishMQTT, the property "topic" supports the Expression Language [1]. It means that you can set the value of this property based on the attributes of the incoming FlowFile. You could use an UpdateAttribute processor to set the "topic" name based on your custom conditions into a flow file attribute named (for example) "mqttTopic", and then set the value of the topic property in the processor to "${mqttTopic}"

Regarding the ConsumeMQTT processor, the documentation [2] says that each produced flow file contains an attribute with the name of the topic. This attribute is "mqtt.topic".

I'd recommend you to have a look to processor documentation by right-clicking on it and going into 'Usage' tab. That will give you useful information. Otherwise documentation of all processors is also available here [3].

Hope this helps.

[1] https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html

[2] https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi.processors.mqtt.ConsumeMQTT/index....

[3] https://nifi.apache.org/docs/nifi-docs

Re: Apache Nifi: Dynamic topic for Publish MQTT

Explorer

For the consumeMQTT, I passed the flow to the ExecuteStreamCommand, and I only see the payload's content not the topic. Please advice.

Re: Apache Nifi: Dynamic topic for Publish MQTT

A flow file is composed of two parts : the content and a set of attributes (kind of flow file metadata). To see the attributes, you could use a LogAttribute processor for example to have all the attributes displayed in the logs, or directly go in the provenance tabs to check flow files individually. Then, as I said, all the attributes of the flow file can be referenced, using Expression Language, in processor properties that accept EL.