Use the Cook Processor to run custom logic on the Hello World pipeline, as an example of data normalization from different data sources that showcases edge computing.

Objectives

In this tutorial you will learn: 

  • How to craft your data pipeline using the cook processor.
  • Execute a simple business logic on the edge.



Even/odd business logic in the pipeline

We start from the Hello World pipeline explained in the Hello World Tutorial. We will add some simple Groovy code to:

  • Add key message to input from hello source.
  • Get the current date.
  • Extract the second.
  • Return odd/even based on the second.

The table below summarizes the components used in the pipeline.

Component TypeComponentDescription
SourceHello SourceProduces a text string (HelloWorld) upon every iteration. 
ProcessorCook ProcessorApply custom script to your pipeline.
SinkLogger SinkLogs the record into the info log, using the toString() method.

Create the pipeline.

We will use the code editor of the cook processor to modify the operation field (no other fields need to be set).

Date date = new Date()

output.message = input
output.theDate = date.format("dd/MM/yyyy")
output.theSecond = date.format("ss")

if (output.theSecond.toInteger() % 2 == 0)
    output.oddEven = 'even'
 else
    output.oddEven = 'odd'
CODE





Debug the pipeline 

We will not deploy this pipeline as the debug console will show the output from the cook processor.

  • Save your pipeline.
  • Click on the Discover tab in the bottom of the page.
  • Just click on Debugger Start.
  • this will run the entire pipeline and you will see the log in the console tab.

  • You can also view the output of each component with the discovery tab and next button.
  • This is the output from the cook processor.



Prime number business logic in the pipeline

Setup the pipeline as you did in Tutorial 1

We will add some simple Groovy code to:

  • Add key message to input from hello source.
  • Get the current date.
  • Extract the second.
  • Return true if the current second is a prime number.

Create an operation parameter with key primeNumbers and add all prime numbers < 60 

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53

To access operation parameters inside your cook processor use the prefix conf in this example we will access the prime numbers with conf.primeNumbers

We will use the code editor of the cook processor to modify the operation field.

Date date = new Date()

output.message = input
output.theDate = date.format("dd/MM/yyyy")
output.theSecond = date.format("ss")

if (conf.primeNumbers.contains(output.theSecond))
    output.prime = true
 else
    output.prime = false
CODE

If you debug the Pipeline you will see the output containing the attribute prime.