# KeyValueStore

## 说明

`mapreduce` 函数在执行期间自动创建 `KeyValueStore` 对象，并将其用于存储由 map 和 reduce 函数添加的键-值对组。尽管您绝不需要显式创建 `KeyValueStore` 对象以使用 `mapreduce`，但您需要使用 `add``addmulti` 对象函数在 map 和 reduce 函数中与此对象交互。

## 创建对象

`mapreduce` 函数在执行期间自动创建 `KeyValueStore` 对象。

## 对象函数

 `add` 向 KeyValueStore 中添加单个键-值对组 `addmulti` 向 KeyValueStore 中添加多个键-值对组

## 示例

```function MeanDistMapFun(data, info, intermKVStore) distances = data.Distance(~isnan(data.Distance)); sumLenKey = 'sumAndLength'; sumLenValue = [sum(distances), length(distances)]; add(intermKVStore, sumLenKey, sumLenValue); end```

```function meanArrivalDelayByDayMapper(data, ~, intermKVStore) % Mapper function for the MeanByGroupMapReduceExample. % Copyright 2014 The MathWorks, Inc. % Data is an n-by-2 table: first column is the DayOfWeek and the second % is the ArrDelay. Remove missing values first. delays = data.ArrDelay; day = data.DayOfWeek; notNaN =~isnan(delays); day = day(notNaN); delays = delays(notNaN); % find the unique days in this chunk [intermKeys,~,idx] = unique(day, 'stable'); % group delays by idx and apply @grpstatsfun function to each group intermVals = accumarray(idx,delays,size(intermKeys),@countsum); addmulti(intermKVStore,intermKeys,intermVals); function out = countsum(x) n = length(x); % count s = sum(x); % mean out = {[n, s]};```