Skip to content
edwardcapriolo edited this page Jan 22, 2013 · 2 revisions

Terminology

CREATEPROCESSOR: Creates and loads a processor PROCESS: Instructs a processor to operate on the result of an operation

Processor example

GET and SLICE verbs both return List. A user wishes to modify the results on the server side before they are returned to the client. For this example the value of the columns are String and the user wishes to upper-case those values.

API

Processor takes the result of another step as input. It's input is List and List is it's output output.

package org.usergrid.vx.experimental;

import java.util.List;
import java.util.Map;

public interface Processor {
  public List<Map> process(List<Map> input);
}

Example

We will insert some sample data. Notice we read the data back in step 7.

 req.add( Operations.setKeyspaceOp("procks") ); //0
 req.add( Operations.createKsOp("procks", 1)); //1
 req.add( Operations.createCfOp("proccf")); //2
 req.add( Operations.setColumnFamilyOp("proccf") ); //3
 req.add( Operations.setAutotimestampOp() ); //4
 req.add( Operations.assumeOp("procks", "proccf", "value", "UTF-8"));//5
 req.add( Operations.setOp("rowa", "col1", "wow")); //6
 req.add( Operations.getOp("rowa", "col1")); //7

The user uses the createProcessorOp to define a class Capitalize that implements Handler.

 req.add( Operations.createProcessorOp("capitalize", "groovy", 
     "public class Capitalize implements org.usergrid.vx.experimental.Processor { \n"+
     "  public List<Map> process(List<Map> input){" +
     "    List<Map> results = new ArrayList<HashMap>();"+
     "    for (Map row: input){" +
     "      Map newRow = new HashMap(); "+
     "      newRow.put(\"value\",row.get(\"value\").toString().toUpperCase());" +
     "      results.add(newRow); "+
     "    } \n" +
     "    return results;"+
     "  }"+
     "}\n"
 ));//8

Call the processor and instruct referencing the get from operation 7 as input.

 req.add( IntraOp.processOp("capitalize", new HashMap(), 7));//9

The value of the get from step 7 was "wow".

 List<Map> x = (List<Map>) res.getOpsRes().get(7);
 Assert.assertEquals( "wow",  x.get(0).get("value") );
 Assert.assertNull( res.getException() );
 x = (List<Map>) res.getOpsRes().get(9);

The result of step 9 has converted the value to upper case

 Assert.assertEquals( "WOW",  x.get(0).get("value") );

Wrap Up

Processor is best used for operations like group by. Remember that the Processor retains both the input and output result sets in memory for the length of the request. If you wish to process multiple results at once a MultiProcessor is a better option.