Aggregator Implementation and Usage
Note
This feature is only available in NCache Enterprise Edition.
The Aggregator and Value Extractor logic can be implemented in classes implementing the interfaces IAggregator and IValueExtractor respectively. These implementations will contain the logic to be executed over the cache item(s) on the server side. Once implemented, this implementation will be deployed on NCache. You can then invoke the cache from your client application to perform the specified Entry Processor logic over the server.
Step 1: Implement IAggregator Interface
You custom logic is provided in Aggregate and AggregateAll methods in the IAggregator implementation.
Aggregate()
contains the logic of applying the aggregation operation on the same node (locally) as is with Combiner. If you wish to combine the values using a aggregator before being sent for further processing in the Reducer, you can use the Aggregate()
call.
AggregateAll()
contains the logic of applying the aggregation operation in the Reduce Phase. If you wish to combine the values using an aggregator, you can use the AggregateAll()
call.
Important
Once implemented, deploy this class on NCache by referring to Deploy Providers in Administrator’s Guide.
Pre-requisites
- The class must be implemented as a Class Library (.dll) in Visual Studio. This will be deployed on NCache cluster.
- Include the following namespace in your application:
Alachisoft.NCache.Runtime.Aggregation
Alachisoft.NCache.Runtime.Exceptions
- The class implementing
IAggregator
interface must be marked as Serializable, along with any parameters the process code might be taking in.
The following sample implementation processes given values accordingly and returns the result where needed.
[Serializable]
public class IntAggregator : IAggregator
{
string function;
public IntAggregator(string function)
{
this.function = function;
}
public object Aggregate(object value)
{
return Calculate(value);
}
public object AggregateAll(object value)
{
return Calculate(value);
}
private object Calculate(object value)
{
switch (function)
{
case "MIN":
value = int.MinValue;
return value;
case "MAX":
value = int.MaxValue;
return value;
default:
return 0;
}
}
// This class is to be deployed on cache
}
Step 2: Implement IValueExtractor Interface
You custom logic is provided in the Extract method, which will extract meaningful information/ attributes from the object like the Mapper does in MapReduce. The returned value may also be null.
Important
Once implemented, deploy this class on NCache by referring to Deploy Providers in Administrator’s Guide.
Pre-requisites
- The class must be implemented as a Class Library (.dll) in Visual Studio. This will be deployed on NCache cluster.
- Include the following namespace in your application:
Alachisoft.NCache.Runtime.Aggregation
- The class implementing
IValueExtractor
interface must be marked as Serializable, along with any parameters the process code might be taking in.
The following sample implementation processes given values accordingly and returns the result where needed.
[Serializable]
public class BasicValueExtractor : IValueExtractor
{
public object Extract(object value)
{
try
{
if (value.GetType() == typeof(int))
{
return value;
}
if (value.GetType() == typeof(float))
{
return 0.0;
}
}
catch (Exception e)
{
// Handle exceptions
}
return value;
}
// This class is to be deployed in cache
}
Step 3: Deploy Implementations on Cache
Deploy these custom implementations and any dependent assemblies on NCache by referring to Deploy Providers in Administrator’s Guide for help.
Step 4: Use Aggregator in Application
Once the interfaces are implemented and deployed on cache, you can execute the Aggregator using the Aggregate method in your client application.
Pre-requisites
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Processor
- The classes being used must be deployed on cache.
- The application must be connected to cache before performing the operation.
- Cache must be running, and must contain data.
- 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 code sample adds a bulk of items into the cache and then invokes the Aggregator using the keys added to the cache, for which implementation has been provided in IAggregator class.
try
{
// Pre-conditions: Cache is already connected
// Data exists in cache
int value;
// Get single value by custom implemented aggregator
value = (int)cache.ExecutionService.Aggregate(new BasicValueExtractor(), new IntAggregator("MIN"));
// Get single value by custom implemented aggregator
value = (int)cache.ExecutionService.Aggregate(new BasicValueExtractor(), new IntAggregator("MAX"));
// Using the built-in aggregator
value = (int)cache.ExecutionService.Aggregate(new BasicValueExtractor(), BuiltInAggregator.IntegerSum());
}
catch (OperationFailedException ex)
{
// 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
Aggregator (MapReduce) Components and Working
MapReduce
WAN Replication across Multi Datacenters through Bridge
Deploy Providers