/// <summary> /// This method executes the map reduce task /// </summary> /// <returns> Returns the tracker through which task can be monitored and result can be fetched </returns> private ITrackableTask ExecuteMapReduceTask() { // Create InitParams for the MapReduce task, i.e., set mapper, combiner and reducer // Mapper is a must, other two are optional. MapReduceInitParams initParams = new MapReduceInitParams("NosDB.Samples.MapReduceImplementation.Mapper, NosDB.Samples.MapReduceImplementation, Version=1.0.0.0, Culture=neutral", "mapreduceimpl"); initParams.Combiner = "NosDB.Samples.MapReduceImplementation.CombinerFactory, NosDB.Samples.MapReduceImplementation, Version=1.0.0.0, Culture=neutral"; initParams.Reducer = "NosDB.Samples.MapReduceImplementation.ReducerFactory, NosDB.Samples.MapReduceImplementation, Version=1.0.0.0, Culture=neutral"; // Specify the output option, It will create the collection if it does not exist. initParams.OutputOption = NosDB.Common.MapReduce.MROutputOption.CUSTOM_COLLECTION; // You have to specify the custom collection name if the MROutputOption is set to CUSTOM_COLLECTION, // For DEFAULT_COLLECTION, collection will be created as Collection_<Some_GUID> initParams.CustomCollectionName = OUTPUT_COLLECTION_NAME; // Specify the collections, on which you want to execute the map reduce task. List <string> collections = new List <string>(); collections.Add("orders"); // The call which sends the task to the NosDB Server, and returns a trackable object. // BEFORE EXECUTION: // Make sure that you have deployed the implementation of mapreduce task (Mapper, Combiner and Reducer). ITrackableTask trackableTask = _database.ExecuteTask(initParams, collections); return(trackableTask); }
/// <summary> /// This method fetches the map reduce task result from the database and iterates the result /// </summary> /// <param name="trackableTask"> TrackableTask whos result will be fetched and iterated </param> private void FetchAndReadTaskResult(ITrackableTask trackableTask) { // Call to get the result, this is a BLOCKING call... (it waits until task completes and returns the result...) ITaskResult taskResult = trackableTask.GetResult(); //NOTE: There are other ways you can get the task result, Read more on the documentation... IDBCollectionReader resultEnumerator = taskResult.GetEnumerator(); // Get and enumerate the result. while (resultEnumerator.ReadNext()) { //Reading the output IJSONDocument document = resultEnumerator.GetDocument(); Console.WriteLine("Name: " + document["Key"] + " and Count available: " + document["Value"]); } // Now that we have gathered the data // we can query the top results. IDBCollectionReader rdr = _database.ExecuteReader("SELECT TOP 5 * FROM \"" + OUTPUT_COLLECTION_NAME + "\" ORDER BY Value DESC"); while (rdr.ReadNext()) { //Reading the output IJSONDocument document = rdr.GetDocument(); Console.WriteLine("Name: " + document["Key"] + " and Count available: " + document["Value"]); } }
/// <summary> /// Executing this method will perform all the operations of the sample /// </summary> public void Run() { // Generate sample orders data to be used in this sample ICollection <Order> orders = GenerateOrders(); // Initialize database and collection InitializeDatabaseAndCollection(); // Insert some documents in the collection so that map reduce can run on it InsertDocuments(orders); // Execute map reduce task and get tracker ITrackableTask trackableTask = ExecuteMapReduceTask(); // Fetch result and iterate the result FetchAndReadTaskResult(trackableTask); // Releasing the resources _database.Dispose(); }