16.2.3. Mapper and CDI

The Mapper is invoked with appropriate input key/value pairs on an executing node, however JBoss Data Grid also provides a CDI injection for an input cache. The CDI injection can be used where additional data from the input cache is required in order to complete map transformation.
When the Mapper is executed on a JBoss Data Grid executing node, the JBoss Data Grid CDI module provides an appropriate cache reference, which is injected to the executing Mapper. To use the JBoss Data Grid CDI module with Mapper:
  1. Declare a cache field in Mapper.
  2. Annotate the cache field Mapper with @org.infinispan.cdi.Input.
  3. Annotate with mandatory @Inject annotation.
For example:
				
public class WordCountCacheInjectedMapper implements Mapper<String, String, String, Integer> {
 
      @Inject
      @Input
      private Cache<String, String> cache;
 
      @Override
      public void map(String key, String value, Collector<String, Integer> collector) {
 
         //use injected cache if needed
         StringTokenizer tokens = new StringTokenizer(value);
         while (tokens.hasMoreElements()) {
		 for(String token : value.split("\\w")) {
             collector.emit(token, 1);
             }
      }
}