Advertisement
josephxsxn

nifi scripted processor example

Aug 14th, 2018 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.61 KB | None | 0 0
  1. flowFile = session.get();
  2.  
  3. function getRandomInt(min, max) {
  4.   min = Math.ceil(min);
  5.   max = Math.floor(max);
  6.   return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
  7. }
  8.  
  9. if (flowFile != null) {
  10.  
  11.     var StreamCallback = Java.type("org.apache.nifi.processor.io.StreamCallback");
  12.     var IOUtils = Java.type("org.apache.commons.io.IOUtils");
  13.     var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
  14.     var transformed_message = {};
  15.     var error = false;
  16.     var line = influxdbmeasurement.evaluateAttributeExpressions().getValue();
  17.  
  18.  
  19.  
  20.     // Get attributes
  21.     flowFile = session.write(flowFile, new StreamCallback(function (inputStream, outputStream) {
  22.         // Read input FlowFile content
  23.         var content = IOUtils.toString(inputStream, StandardCharsets.UTF_8); // message or content
  24.  
  25.         try {
  26.  
  27.             line = line + ",providerId=" + providerid.evaluateAttributeExpressions().getValue() +
  28.                 ",type=" + type.evaluateAttributeExpressions().getValue() +
  29.                 " value=" + "1"
  30.                 + " " + (Date.now() * 1000000 + getRandomInt(0,1000));
  31.  
  32.             // Write output content
  33.             if (transformed_message) {
  34.                 outputStream.write(line.getBytes(StandardCharsets.UTF_8));
  35.             }
  36.         } catch (e) {
  37.             error = true;
  38.             outputStream.write(content.getBytes(StandardCharsets.UTF_8));
  39.         }
  40.     }));
  41.  
  42.     if (error) {
  43.         session.transfer(flowFile, REL_FAILURE)
  44.     } else {
  45.         session.transfer(flowFile, REL_SUCCESS)
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement