• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

MapReduce Aggregator Implementation and Usage [Deprecated]

This page provides step-by-step guide on implementing and using MapReduce Aggregator with NCache.

Prerequisites

  • .NET
  • Legacy API
  • To learn about the standard prerequisites required to work with all NCache server-side features, please refer to the given page Server-Side API Prerequisites.
  • The class must be implemented as a Class Library (.dll) in Visual Studio. This will be deployed on NCache cluster.
  • For API details, refer to: ICache, IAggregator, IValueExtractor, Aggregate, AggregateAll, Extract, IExecutionService.
  • Install either of the following NuGet packages in your .NET client application:
    • Enterprise: Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
    • Professional: Install-Package Alachisoft.NCache.Professional.SDK -Version 4.9.1.0
  • This should be a class library project using Microsoft Visual Studio.
  • Make sure that the data being added is serializable.
  • Add NCache References by locating %NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
  • Include the Alachisoft.NCache.Runtime.Aggregation namespace in your application.
  • To learn more about the NCache Legacy API, please download the NCache 4.9 documents available as a .zip file on the Alachisoft Website.

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

Your 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 the Combiner. If you wish to combine the values using an 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. The following sample implementation processes give values accordingly and return the result where needed.

Important

Once implemented, deploy this class on NCache by referring to Deploy Providers in the Administrator’s Guide.

  • .NET
  • Legacy API
[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
}
// Using NCache Enterprise 4.9.1

[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
}
Note

To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

Step 2: Implement IValueExtractor Interface

Your custom logic is provided in the Extract method, which will extract meaningful information/attributes from the object like the Mapper does in the MapReduce. The returned value may also be null. The following sample implementation processes give values accordingly and return the result where needed.

  • .NET
  • Legacy API
[Serializable]
public class BasicValueExtractor : IValueExtractor
{
    public object Extract(object value)
    {

            if (value.GetType() == typeof(int))
            {
                return value;
            }

            if (value.GetType() == typeof(float))
            {
                return 0.0;
            }

        return value;
    }
    // This class is to be deployed in cache
}
// Using NCache Enterprise 4.9.1

[Serializable]
public class BasicTypeExtractor : IValueExtractor
{
    public object Extract(object value)
    {
            if (value.GetType() == typeof(int))
            {
                return 0;
            }

            if (value.GetType() == typeof(float))
            {
                return 0.0;
            }
        return value;
    }
    // This class is to be deployed on cache
}

Step 3: Deploy Implementations on the Cache

Deploy these custom implementations on NCache by referring to Deploy Providers in the Administrator’s Guide for help.

Step 4: Use Aggregator in the Application

Once the interfaces are implemented and deployed on the cache, you can execute the Aggregator using the Aggregate method in your client application.

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.

  • .NET
  • Legacy API
// Data exists in cache
// Precondition:Cache is already connected

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());
// Using NCache Enterprise 4.9.1
// Data exists in cache
// Precondition:Cache is already connected

int value;

// Using the built-in aggregator
value = (int)cache.Aggregate(new BasicTypeExtractor(),BuiltInAggregator.IntegerSum());

// Get single value by custom implemented aggregator
value = (int)cache.Aggregate(new BasicTypeExtractor(), new IntAggregator("MIN"));

// Get a single value by custom implemented aggregator
value = (int)cache.Aggregate(new BasicTypeExtractor(), new IntAggregator("MAX"));

See Also

Aggregator Components and Working
MapReduce
WAN Replication across Multi Datacenters through Bridge
Deploy Providers

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Community
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top