Method ExecuteTask
ExecuteTask(MapReduceTask)
Executes the MapReduce task on the cache.
Declaration
public virtual ITrackableTask ExecuteTask(MapReduceTask task)
Parameters
Type | Name | Description |
---|---|---|
MapReduceTask | task | Instance of a MapReduceTask. |
Returns
Type | Description |
---|---|
ITrackableTask | Returns an instance to track the submitted task for result and status |
Examples
Note: Implementing IReducerFactory,ICombinerFactory and ICmobiner is optional.
Cache _cache= NCache.InitializeCache("myCache");
product1 = new Product(3, "Tunnbr?d", "", 4, 2);
product2 = new Product(4, "Tunnbr?d", "", 5, 9);
_cache.Add("2202", product1);
_cache.Add("2203", product2);
MapReduceTask mapReduceTask = new MapReduceTask();
mapReduceTask.Mapper = new WordCountMapper();
mapReduceTask.Combiner = new WordCountCombinerFactory();
mapReduceTask.Reducer = new WordCountReducerFactory();
ITrackableTask trackableInstance = _cache.ExecuteTask(mapReduceTask);
//Mapper implementation
public class WordCountMapper : IMapper
{
string[] parsedline;
string line;
public void Map(Object key, Object value, IOutputMap context)
{
line = value.ToString();
parsedline = line.Split(' ');
for (int i = parsedline.Length; i>=0; i++)
{
context.Emit(parsedline[i], 1);
}
}
public void Dispose()
{
// Release Resources
}
}
//CombinerFactory Implementation
public class WordCountCombinerFactory : ICombinerFactory
{
public ICombiner Create(object key)
{
WordCountCombiner wcCombiner = new WordCountCombiner();
return wcCombiner;
}
}
//ICombiner Implementation
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()
{
// Release Resources
}
}
//IReducerFactory Implementation
public class WordCountReducerFactory : IReducerFactory
{
public IReducer Create(object key)
{
WordCountReducer wcReducer = new WordCountReducer(key);
return wcReducer;
}
}
//IReducer Implementation
public class WordCountReducer : IReducer
{
int count = 0;
object key;
public WordCountReducer(object value)
{
key = value;
}
public void BeginReduce()
{
//perform operations
}
public void Reduce(object value)
{
count += int.Parse(value.ToString());
}
public KeyValuePair FinishReduce()
{
KeyValuePair kvp = null;
kvp.Key = key;
kvp.Value = count;
return kvp;
}
public void Dispose()
{
// Release Resources
}
}
ExecuteTask(MapReduceTask, IKeyFilter)
Executes the MapReduce task on the cache using specified KeyFilter implementation.
Declaration
public virtual ITrackableTask ExecuteTask(MapReduceTask task, IKeyFilter keyFilter)
Parameters
Type | Name | Description |
---|---|---|
MapReduceTask | task | Instance of a MapReduceTask. |
IKeyFilter | keyFilter | Instance of IKeyFilter implementation. |
Returns
Type | Description |
---|---|
ITrackableTask | Returns an instance to track the submitted task for result and status. |
Examples
Note: Implemeting IReducerFactory,ICombinerFactory and ICmobiner is optional.
Cache _cache= NCache.InitializeCache("myCache");
product1 = new Product(3, "Tunnbr?d", "", 4, 2);
product2 = new Product(4, "Tunnbr?d", "", 5, 9);
_cache.Add("2202", product1);
_cache.Add("2203", product2);
MapReduceKeyFilter keyFilter= new MapReduceKeyFilter();
MapReduceTask mapReduceTask = new MapReduceTask();
mapReduceTask.Mapper = new WordCountMapper();
mapReduceTask.Combiner = new WordCountCombinerFactory();
mapReduceTask.Reducer = new WordCountReducerFactory();
ITrackableTask trackableInstance = _cache.ExecuteTask(mapReduceTask,keyFilter);
//implementation of IKeyFilter
public class MapReduceKeyFilter : IKeyFilter
{
public bool FilterKey(object key)
{
try
{
if (key.ToString().Contains("hungry"))
{
return true;
}
}
catch (Exception exp)
{
//handle exception
}
return false;
}
}
//Mapper implementation
public class WordCountMapper : IMapper
{
string[] parsedline;
string line;
public void Map(Object key, Object value, IOutputMap context)
{
line = value.ToString();
parsedline = line.Split(' ');
for (int i = parsedline.Length; i>=0; i++)
{
context.Emit(parsedline[i], 1);
}
}
public void Dispose()
{
// Release Resources
}
}
//CombinerFactory Implementation
public class WordCountCombinerFactory : ICombinerFactory
{
public ICombiner Create(object key)
{
WordCountCombiner wcCombiner = new WordCountCombiner();
return wcCombiner;
}
}
//ICombiner Implementation
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()
{
// Release Resources
}
}
//IReducerFactory Implementation
public class WordCountReducerFactory : IReducerFactory
{
public IReducer Create(object key)
{
WordCountReducer wcReducer = new WordCountReducer(key);
return wcReducer;
}
}
//IReducer Implementation
public class WordCountReducer : IReducer
{
int count = 0;
object key;
public WordCountReducer(object value)
{
key = value;
}
public void BeginReduce()
{
//perform operations
}
public void Reduce(object value)
{
count += int.Parse(value.ToString());
}
public KeyValuePair FinishReduce()
{
KeyValuePair kvp = null;
kvp.Key = key;
kvp.Value = count;
return kvp;
}
public void Dispose()
{
// Release Resources
}
}
ExecuteTask(MapReduceTask, String, Hashtable)
Executes the MapReduce task on the resultant items with specified query.
Declaration
public virtual ITrackableTask ExecuteTask(MapReduceTask task, string query, Hashtable parameters)
Parameters
Type | Name | Description |
---|---|---|
MapReduceTask | task | Instance of a MapReduceTask. |
System.String | query | Simple sql like NCache query string |
System.Collections.Hashtable | parameters | NCache query string searchable parameters. |
Returns
Type | Description |
---|---|
ITrackableTask | Returns an instance to track the submitted task for result and status. |
Examples
Note: Implemeting IReducerFactory,ICombinerFactory and ICmobiner is optional.
Cache _cache= NCache.InitializeCache("myCache");
product1 = new Product(3, "Clothes", "", 4, 2);
product2 = new Product(4, ""Shoes", "", 5, 9);
_cache.Add("2202", product1);
_cache.Add("2203", product2);
MapReduceTask mapReduceTask = new MapReduceTask();
mapReduceTask.Mapper = new WordCountMapper();
mapReduceTask.Combiner = new WordCountCombinerFactory();
mapReduceTask.Reducer = new WordCountReducerFactory();
Hashtable values = new Hashtable();
values.Add("Category", "Clothes");
string query = "SELECT Product WHERE this.Category IN (?)";
ITrackableTask trackableInstance = _cache.ExecuteTask(mapReduceTask,query,values);
//Mapper implementation
public class WordCountMapper : IMapper
{
string[] parsedline;
string line;
public void Map(Object key, Object value, IOutputMap context)
{
line = value.ToString();
parsedline = line.Split(' ');
for (int i = parsedline.Length; i>=0; i++)
{
context.Emit(parsedline[i], 1);
}
}
public void Dispose()
{
// Release Resources
}
}
//CombinerFactory Implementation
public class WordCountCombinerFactory : ICombinerFactory
{
public ICombiner Create(object key)
{
WordCountCombiner wcCombiner = new WordCountCombiner();
return wcCombiner;
}
}
//ICombiner Implementation
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()
{
// Release Resources
}
}
//IReducerFactory Implementation
public class WordCountReducerFactory : IReducerFactory
{
public IReducer Create(object key)
{
WordCountReducer wcReducer = new WordCountReducer(key);
return wcReducer;
}
}
//IReducer Implementation
public class WordCountReducer : IReducer
{
int count = 0;
object key;
public WordCountReducer(object value)
{
key = value;
}
public void BeginReduce()
{
//perform operations
}
public void Reduce(object value)
{
count += int.Parse(value.ToString());
}
public KeyValuePair FinishReduce()
{
KeyValuePair kvp = null;
kvp.Key = key;
kvp.Value = count;
return kvp;
}
public void Dispose()
{
// Release Resources
}
}