如何在Apache NiFi中对API获取的数值执行存入前数学运算?
Got it, let's tackle this problem step by step! You're already using InvokeHTTP to fetch numeric data and PutFile to store it—now you just need to plug in the math operations in between. Here are the most straightforward, practical ways to do this in Apache NiFi:
ExecuteScript (Flexible, Supports Custom Logic) This is the most versatile choice if you need complex math or want full control over data manipulation. NiFi supports multiple scripting languages like Groovy, Python, and JavaScript—Groovy is particularly well-integrated.
- Drop an
ExecuteScriptprocessor betweenInvokeHTTPandPutFile. - Configure the processor:
- Set Script Language to
Groovy(or your preferred language). - Paste a script tailored to your data format. For example, if your fetched data is JSON like
{"rawValue": 10}:import groovy.json.JsonSlurper import groovy.json.JsonBuilder def flowFile = session.get() if (!flowFile) return flowFile = session.write(flowFile, { inputStream, outputStream -> // Parse incoming JSON data def slurper = new JsonSlurper() def data = slurper.parse(inputStream) // Perform your math operation (e.g., rawValue * 3 + 7) data.calculatedValue = data.rawValue * 3 + 7 // Write the modified data back to the flow file def builder = new JsonBuilder(data) outputStream.write(builder.toPrettyString().getBytes('UTF-8')) } as StreamCallback) // Optional: Add an attribute to track the operation performed flowFile = session.putAttribute(flowFile, 'math_operation', 'rawValue*3+7') session.transfer(flowFile, REL_SUCCESS)
- Set Script Language to
- Connect
ExecuteScript's REL_SUCCESS relationship to yourPutFileprocessor.
UpdateRecord (No-Code for Structured Data) If your data is structured (JSON, CSV, Avro, etc.), UpdateRecord lets you use NiFi's Expression Language to do math without writing scripts.
- Add an
UpdateRecordprocessor in your flow. - Configure core settings:
- Set Record Reader to match your input format (e.g.,
JsonTreeReaderfor JSON). - Set Record Writer to your desired output format (e.g.,
JsonRecordSetWriter).
- Set Record Reader to match your input format (e.g.,
- Add a property to define your math operation:
- Property Name: The path to your new/updated field (e.g.,
/calculatedValuefor JSON). - Property Value: Use NiFi's math functions. For example, if your raw value is in
/rawValue:
NiFi's Expression Language supports common operations:${field.rawValue:multiply(2):minus(1)}multiply(),divide(),plus(),minus(),mod(),abs(), etc.
- Property Name: The path to your new/updated field (e.g.,
- Link
UpdateRecord's REL_SUCCESS toPutFile.
ReplaceText (For Simple Plain-Text Numeric Data) If your fetched data is just a raw number (e.g., 42 with no JSON/CSV structure), ReplaceText is the quickest fix.
- Insert a
ReplaceTextprocessor. - Configure these settings:
- Search Value:
(.*)(matches the entire text content). - Replacement Value: Use the Expression Language to compute the result. For example, to multiply the number by 5:
${$1:multiply(5)} - Evaluation Mode: Set to
Entire textto apply the operation to the whole value.
- Search Value:
- Connect REL_SUCCESS to
PutFile.
- Always use
LogAttributebefore/after your math processor to verify that the data is being modified correctly. - For
ExecuteScript, add error handling (e.g., check if the input value is a valid number) to avoid breaking your flow. - Test with small datasets first to ensure the operations behave as expected.
内容的提问来源于stack exchange,提问作者cagridemir

