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.

Increment ordered xml values with Nifi

Increment ordered xml values with Nifi

New Contributor

I have a xml file containing this data:

<Load>
        <IntervalLength>5</IntervalLength>
        <TimeData>2016-04-28T04:00:00Z</TimeData>
        <LoadValue>814</LoadValue>
        <LoadValue>816</LoadValue>
        <LoadValue>819</LoadValue>
</Load>

I am able to use SplitXml + EvaluateXPath to populate attributes and then ReplaceText to get the data into this output format:

IntervalLength, LoadValue, TimeData
5,814,2016-04-28T04:00:00Z
5,816,2016-04-28T04:00:00Z
5,819,2016-04-28T04:00:00Z

Like a boss!

Now I have a requirement to increment the TimeData value by applying the IntervalLength value. So for each LoadValue encountered in the xml I need to add 5 mins (or whatever value is present in IntervalLength) to the TimeData value. The order of the LoadValues is important. The desired output is:

IntervalLength, LoadValue, TimeData
5,814,2016-04-28T04:00:00Z
5,816,2016-04-28T04:05:00Z
5,819,2016-04-28T04:10:00Z

Notice the minutes in the last column increasing by 5 for each record.

Any suggestions? The ideal solution would not use any custom coding (processor or scripts). I need to replace a legacy custom code solution which is unsupportable so replacing it with more custom code is not desirable.

2 REPLIES 2

Re: Increment ordered xml values with Nifi

Rising Star

@jtrite One thought I had was to use the UpdateAttribute processor with expression language, however it may be helpful to add a bit more data to the row beforehand (e.g. an Index column starting at 0 for the first row, 1 for second, etc). In the UpdateAttribute processor you could convert the TimeData to a number and do some math (e.g. TimeData + (Index * IntervalLength)). Check out this doc's example that uses now() function to get the current date time and subtracts 24 hours from it . I think you could do something similar like ${timeData:toNumber():plus(index:multiply(intervalLength)} , but you probably need to convert intervalLength to milliseconds first and then do a final conversion of the result back to a date format .

Now the above is assuming that each row represents a flow file that has these values as attributes. If that's not the case perhaps splitting the rows in advance and merging them later would help.

I hope this is helpful!

Re: Increment ordered xml values with Nifi

New Contributor

Hi sir, how did you make to get all the LoadValue Node. In my case i can only get the first LoadValue. Thank you.

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