How to Implement and Execute MapReduce Tasks [Deprecated]
NCache enables you to utilize MapReduce for distributed data processing, allowing the execution of complex analytical tasks on the cache server. By moving the computation to where the data resides, NCache MapReduce minimizes network latency and improves performance for large-scale data aggregation.To use the MapReduce, initialize the MapReduce Task. Set the Mapper, Combiner, and the Reducer factory to the task and then execute the task on the cache.
Prerequisites
Before using the NCache MapReduce, ensure that the following prerequisites are fulfilled:
- Install either of the following NuGet packages in your application:
- Enterprise: Alachisoft.NCache.SDK
- OpenSource: Alachisoft.NCache.Opensource.SDK
- Include the following namespaces in your application based on the feature you intend to use:
- The cache must be running.
- Make sure that the data being added is serializable.
- All assemblies including dependent assemblies must be deployed.
- For API details, refer to: ICache, MapReduceTask, Combiner, Mapper, Reducer, ITrackableTask, GetResult, ITaskResult, IExecutionService, ExecuteTask, GetEnumerator.
The following example shows the sample usage of the MapReduce.
try
{
// Precondition: 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
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Warning
This feature is only supported till 5.3 SP4.
See Also
.NET: Alachisoft.NCache.Runtime.MapReduce namespace.