• Products
  • Solutions
  • Customers
  • Resources
  • Company
  • Pricing
  • Download
Try Playground
Show / Hide Table of Contents

Implement MapReduce Interface [Deprecated]

Users can implement the following MapReduce interfaces such as the IMapper, ICombinerFactory, ICombiner, IReducerFactory, IReducer, IKeyFilter, and more as detailed below.

Note

This feature is only available in NCache Enterprise.

Implement MapReduce Interface 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.
  • This should be a class library project using Microsoft Visual Studio.
  • All assemblies including dependent assemblies must be deployed.
  • For API details, refer to: ICache, Add, IMapper, Map, IOutputMap, ICombinerFactory, ICombiner, Combine, BeginCombine, FinishChunk, IReducer, IReducerFactory, BeginReduce, FinishReduce, Reduce(Object), Create(Object), IKeyFilter, FilterKey.
  • Create a new Console Application.
  • 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.Mapreduce namespace in your application.

Mapper

Implement the MapReduce interface IMapper and provide the implementation for the Map method. This method will contain the logic to map the input from the cache into more meaningful and goal-specific key-value pairs which can be sent to the Reducer or the optional Combiner. Referring to the workflow diagram, the string input is transformed by emitting each word with a key-value pair like <word, count> in the Mapper.

  • .NET
  • Legacy API
[Serializable]
public class ProductCountMapper : IMapper
{

    public void Map(object key, object value, IOutputMap context)
    {
        if (value is IList<string>)
        {
            IList<string> order = value as IList<string>;

            foreach (var product in order)
            {
                context.Emit(product, 1);
            }
        }
    }

    public void Dispose()
    {
        // Dispose resources
    }
}
public class WordCountMapper : IMapper
{
    string[] parsedline;
    string line;

    public void Map(Object key, Object value, IOutputMap outputMap)
    {
        line = value.ToString();
        parsedline = line.Split(' ');
        for (int i = 0; i < parsedline.Length; i++)
        {
            outputMap.Emit(parsedline[i], 1);
        }
    }

    public void Dispose()
    {
        //dispose resources
    }
}

Combiner Factory

Implement the MapReduce interface ICombinerFactory and provide the implementation for its Create(key) method. Providing the Combiner Factory or a Combiner is optional. This method will provide the incoming element with a new instance of the Combiner so that it merges the intermediate key-value pairs from the Mapper.

  • .NET
  • Legacy API
[Serializable]
public class ProductCountCombinerFactory : ICombinerFactory
{
    public IDictionary<string, ICombiner> combiners = new Dictionary<string, ICombiner>(StringComparer.CurrentCultureIgnoreCase);

    public ICombiner Create(object key)
    {
        if (!combiners.ContainsKey(key as string))
        {
            combiners.Add(key as string, new ProductCountCombiner());
        }

        return combiners[key as string];
    }
}
public class WordCountCombinerFactory : ICombinerFactory
{
    public ICombiner Create(object key)
    {
        WordCountCombiner wcCombiner = new WordCountCombiner();
        return wcCombiner;
    }
}
Note

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

Combiner

Implement the MapReduce interface ICombiner. This method reduces the task result locally in chunks so that the Reducer is not burdened with excessive processing. If the workflow diagram is referred to, it means that the elements with the same keys are grouped before being sent to the Reducer for counting the word occurrence.

  • .NET
  • Legacy API
[Serializable]
public class ProductCountCombiner : ICombiner
{
    int count;

    public void BeginCombine()
    {
        //Any initialization related tasks

        count = 0;
    }

    public void Combine(object value)
    {
        if (value is int)
        {
            count += (int)value;
        }
    }

    public object FinishChunk()
    {
        // Finalize current chunk and
        // re -initialize resources here

        int countToBeReturned = count;
        count = 0;
        return countToBeReturned;
    }

    public void Dispose()
    {
        // Dispose resources
    }
}
public class WordCountCombiner : ICombiner
{
    int count = 0;

    public void BeginCombine()
    {
        //any initialization
    }

    public void Combine(object value)
    {
        count += int.Parse(value.ToString());
    }

    public object FinishChunk()
    {
        return count;
    }

    public void Dispose()
    {
        //dispose resources
    }
}

Reducer Factory

Implement the MaoReduce interface IReducerFactory and provide the implementation for its Create(key) method. Providing the Reducer Factory or a Reducer is optional. This method will provide the incoming element with a new instance of the Reducer so that it merges the intermediate key-value pairs from the Combiner.

  • .NET
  • Legacy API
[Serializable]
public class ProductCountReducerFactory : IReducerFactory
{
    IDictionary<string, IReducer> reducers = new Dictionary<string, IReducer>(StringComparer.CurrentCultureIgnoreCase);

    public IReducer Create(object key)
    {
        if (!reducers.ContainsKey(key as string))
        {
            // If you want to map output against a different key,
            // specify a different key in constructor

            reducers.Add(key as string, new ProductCountReducer(key as string));
        }

        return reducers[key as string];
    }
}
public class WordCountReducerFactory : IReducerFactory
{
    public IReducer Create(object key)
    {
        WordCountReducer wcReducer = new WordCountReducer(key);
        return wcReducer;
    }

}

Reducer

Implement the MapReduce interface IReducer. This method will reduce (process) the intermediate key-value pairs into further meaningful pairs. If the workflow diagram is referred to, it means that the values of the grouped elements from the Combiner are summed up to find the actual word count.

  • .NET
  • Legacy API
[Serializable]
public class ProductCountReducer : IReducer
{
    int count;
    string outputKey;
    KeyValuePair outputMapEntry;

    public ProductCountReducer(string key)
    {
        outputKey = key;
    }

    public void BeginReduce()
    {
        //All initialization related tasks

        count = 0;

        outputMapEntry = new KeyValuePair();

        // If you want to change keys in output map,
        // Change outputKey to your desired key

        outputMapEntry.Key = outputKey;
    }

    public void Reduce(object value)
    {
        if (value is int)
        {
            count += (int)value;
        }
    }

    public KeyValuePair FinishReduce()
    {
        outputMapEntry.Value = count;

        return outputMapEntry;
    }

    public void Dispose()
    {
        // Dispose resources
    }
}
public class WordCountReducer : IReducer
{
    int count = 0;
    object reducerKey;

    public WordCountReducer(object key)
    {
        reducerKey = key;
    }

    public void BeginReduce()
    {
        //perform operations
    }

    public void Reduce(object value)
    {
        count += int.Parse(value.ToString());
    }

    public KeyValuePair FinishReduce()
    {
        KeyValuePair kvp = new KeyValuePair();
        kvp.Key = reducerKey;
        kvp.Value = count;
        return kvp;
    }

    public void Dispose()
    {
        //dispose resources
    }
}

Key Filter

Implement the MapReduce interface IKeyFilter to provide implementation for the FilterKey method. Providing this interface is optional. This method will allow the user to filter the input for Mapper by specifying the keys to be processed. If this option is not utilized, the whole cache data will be taken as input for the MapReduce tasks.

  • .NET
  • Legacy API
[Serializable]
public class ProductCountKeyFilter : IKeyFilter
{
    public bool FilterKey(object key)
    {
        // Execute map for keys containing word "Order"

        return (key as string).Contains("Order");
    }
}
public class WordCountKeyFilter : IKeyFilter
{
    public bool FilterKey(object key)
    {
        try
        {
            if (key.ToString().Contains("hungry"))
            {
                return true;
            }
        }
        catch (Exception exp)
        {
            //handle exception
        }
        return false;
    }
}
Important

Make sure to deploy the MapReduce task libraries after implementing the interfaces using the NCache Management Center as guided in Configuring MapReduce in the Administrators' Guide.

Warning

This feature is only supported till 5.3 SP4.

See Also

Using MapReduce in Cache
Aggregator
Entry Processor
Configure MapReduce

In This Article
  • Implement MapReduce Interface Prerequisites
  • Mapper
  • Combiner Factory
  • Combiner
  • Reducer Factory
  • Reducer
  • Key Filter
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • 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 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top