How can i increment variable value inside function mapobject datawave 2.0?

房东的猫 提交于 2020-01-06 02:42:43

问题


How can i increment variable value inside mapobject(I do not want to use index) in mule4 datawave2.0? My Code:


"Employees":{(inputData.*Employees.*Employee map{

  "Employee": ($) mapObject(v,k) -> {
 ---Increment variable value here
    (k):updateReferenceId(v,($$)+1,v)

  }

})}


回答1:


You can increment values in the accumulator of a reduce operation, and you can then add these to the structured data plan f the output. There are some examples in the MuleSoft DataWeave training course

flightsInput reduce ( ( flight, acc={ american: {price: 0, count: 0}, delta: {price: 0, count: 0}, united: {price: 0, count: 0} } ) -> flight.airlineName match { case american if(lower(american) contains "amer") -> acc - "american" ++ american: { price: acc.american.price + flight.price, count: acc."american".count + 1 } case delta if(lower(delta) contains "del") -> acc - "delta" ++ delta: { price: acc.delta.price + flight.price, count: acc."delta".count + 1 } case united if(lower(united) contains "uni") -> acc - "united" ++ united: { price: acc.united.price + flight.price, count: acc."united".count + 1 } else -> acc } )

This creates a data structure with the three different key performance indicators:




回答2:


As usual in functional languages, like DataWeave, you can not increment a variable. You can return an incremented value from an expression instead. For example you could do a recursive function that return its input incremented by one. Just assume that variables are immutable in DataWeave.

You should first try to think what problem are you trying to solve, why do you really need to increment that variable and why the index of mapObject() would not fit that need.



来源:https://stackoverflow.com/questions/59145117/how-can-i-increment-variable-value-inside-function-mapobject-datawave-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!