目录
🌹前言
先看论文,再看视频,最后做实验
🦅2 Programming Model
🌼2.1 Example
0)伪代码
1)map
// MapReduce 库中的 Map 函数
void map(const string& key, const string& value) {
// key: document name
// value: document content
// ("document1", "hello world hello")
istringstream iss(value); // 字符串流, 从文本提取单词
string word;
while (iss >> word)
// 输出键值对
cout << "EmitIntermediate("
<< word << ", \"1\")" << endl;
}
EmitIntermediate(hello, "1")
EmitIntermediate(world, "1")
EmitIntermediate(hello, "1")
2)reduce
// MapReduce 库中的 Reduce 函数
void reduce(const string& key, const vector<string>& values) {
// key: a word
// value: a list of counts
// ("hello", {"1", "1"})
int result = 0; // 单词出现次数
for (size_t i = 0; i < values.size(); ++i)
// atoi("..."): "+211" --> 211
// 字符串转 int
// c_str 转 C风格字符串
result += atoi(values[i].c_str());
// 模拟发出最终结果
cout << "Emit(" << key << ", " << result << ")" << endl;
}
Emit(hello, 2)
🌼2.2 Types
"document1", "hello world hello" // 输入的键/值
"hello", "1" // 中间的键/值
"world", "1"
"hello", "1"
"hello", "2" // 输出的键/值
上述结构,允许 MapReduce 框架处理大规模数据集,通过分布式并行处理数据,提高数据处理效率和可扩展性
总而言之,字符串传递给用户所定义的函数,用户所定义的函数负责将字符串转化成合适的类型
🌼2.3 More Examples
🦅3 Implementation(实现)
🌼3.1 ~ 3.3
3.1 Execution Overview
3.2 Master Data Structures
3.3 Fault Tolerance
Semantics in the Presence of Failures
故障下的语义👇
🌼3.4 ~ 3.6
🦅4 Refinements(改进)
🌼4.1 ~ 4.5
🌼4.6 ~ 4.9
// 声明一个计数器对象指向uppercase
Counter* uppercase;
// 通过GetCounter函数获取名为"uppercase"的计数器
uppercase = GetCounter("uppercase");
// 定义Map函数处理输入
map(String name, String contents):
// 遍历输入内容中的每个单词
for each word w in contents:
// 如果单词是大写开头的
if (IsCapitalized(w)):
// 对应的计数器increment操作,增加计数
uppercase->Increment();
// 调用EmitIntermediate函数,发出中间键值对
EmitIntermediate(w, "1");