DEV Community

Praneeth
Praneeth

Posted on

Decimal Point Precision in Apache NiFi using groovy Script

Apache NiFi Doesn't support setting up of Decimal point precision in Expression Language. Thanks to groovy Script Language Support which has the capability to extract the Attributes from flowFiles and modify the values to a particular precision points.

Here are the following steps which help you to accomplish maintaining precision points in the Decimal Values:

  1. Drag and Drop an ExecuteScript Processor.
  2. Replace the Body of the Script with the following script:
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
import java.text.DecimalFormat

flowFile = session.get()

Double attr1 = Double.parseDouble(flowFile.getAttribute("<NAME OF THE ATTRIBUTE>"))
def df = new DecimalFormat("#0.00")

def attr1_decimal = (df.format(attr1))

flowFile = session.putAttribute(flowFile, "attr1", attr1_decimal)
session.transfer(flowFile, REL_SUCCESS)
session.commit()
Enter fullscreen mode Exit fullscreen mode

3.Finally connect the Success relationship to a Pipeline to view the

Top comments (0)