User`s guide

6 Using Database Toolbox Functions
6-86
Define the Mapper and Reducer Functions
You can write your own mapper function to process large data sets in chunks. This
example uses the mapper function meanArrivalDelayMapper.m. This mapper
function reads in arrival delay data from the DatabaseDatastore object, calculates
the number of delays and the total arrival delay in the chunk, and stores both values in
KeyValueStore. Display the code for this function using the command type.
type meanArrivalDelayMapper.m
function meanArrivalDelayMapper (data, info, intermKVStore)
% Mapper function for the MeanMapReduceExample.
% Copyright 2014 The MathWorks, Inc.
% Data is an n-by-1 table of the ArrDelay. Remove missing value first:
data(isnan(data.ArrDelay),:) = [];
% Record the partial counts and sums and the reducer will accumulate them.
partCountSum = [length(data.ArrDelay), sum(data.ArrDelay)];
add(intermKVStore, 'PartialCountSumDelay',partCountSum);
Add this code at the beginning of the mapper function if you are using the native ODBC
interface to connect to the database.
if iscell(data)
return
end
This code skips the final output of the read function. The final output is a cell array
containing the string 'No Data'.
You can write your own reducer function to process large data sets in chunks. This
example uses the reducer function meanArrivalDelayReducer.m. This reducer
function reads in intermediate values for the number of delays and the total arrival
delay. Then, this function calculates the overall mean arrival delay. mapreduce calls this
reducer function once since the mapper function only adds one key to KeyValueStore.
Display the code for this function by using the command type.
type meanArrivalDelayReducer.m
function meanArrivalDelayReducer(intermKey, intermValIter, outKVStore)
% Reducer function for the MeanMapReduceExample.
% Copyright 2014 The MathWorks, Inc.