Using MapReduce in Cache
Note
This feature is only available in NCache Enterprise Edition.
In order to use MapReduce, initialize MapReduce Task. Set mapper, combiner and reducer factory to the task and then execute task on cache.
Pre-Requisites
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Mapreduce
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
The following example shows the sample usage of MapReduce.
try
{
// Pre-condition: Cache is already connected
// Initialize MapReduce Task
MapReduceTask task = new MapReduceTask();
// Set Mapper, Combiner factory and Reducer factory
task.Mapper = new ProductCountMapper();
task.Combiner = new ProductCountCombinerFactory();
task.Reducer = new ProductCountReducerFactory();
// Execute task on cache
ITrackableTask wordCount = cache.ExecutionService.ExecuteTask(task, new ProductCountKeyFilter());
// Get result
ITaskResult result = wordCount.GetResult();
IDictionaryEnumerator enumResult = result.GetEnumerator();
while (enumResult.MoveNext())
{
// Perform operations
}
}
catch(OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.TASK_IN_SUBMISSION_FAILED_NODE)
{
// Task is failed during submission
// Make sure that the mapper is defined
}
else if (ex.ErrorCode == NCacheErrorCodes.TASK_STARTING_FAILED)
{
// Task is failed while starting
// Make sure that the mapper is defined
}
else if (ex.ErrorCode == NCacheErrorCodes.TASK_CANT_BE_SUBMITTED)
{
// The limit of tasks to be submitted has been reached
// No more task can be submitted
}
else if (ex.ErrorCode == NCacheErrorCodes.TASK_CANCELLED_REASON)
{
// Task was cacelled and could not be submitted
}
else if (ex.ErrorCode == NCacheErrorCodes.TASK_SUBMISSION_FAILED)
{
// Task has failed to be submitted
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
See Also
Sample Implementation of MapReduce
Aggregator
Entry Processor
Configure MapReduce