hadoop笔记 (4):streaming

hadoop 提供了 3 种方式来实现自己的功能,一种是 java 接口,另一种是 pipes,还有一种是 streaming。streaming 允许用任何语言来开发 mapper 和 reducer,它们和 hadoop 之间的交互通过标准输入/输出来进行。这种方式的好处是调试很方便。

先来看一个使用 bash 编写的 wordcount 的例子:

#!/bin/bash

while read line; do
    arr=($line)
    let i=0
    while ! [ -z ${arr[$i]} ]; do
        echo -e "${arr[$i]}\t1"
        let i=$i+1
    done

阅读全文…

hadoop笔记 (3):pipes例子分析 (2)

使用 partitioner 和 combiner

下面的程序是对 hadoop 1.0.3 自带例子(src/examples/pipes/impl/wordcount-part.cc)的一个修改版:

#include <string>
#include <vector>
using namespace std;

#include <hadoop/Pipes.hh>
#include <hadoop/StringUtils.hh>
#include <hadoop/TemplateFactory.hh>

class WordCountMapper : public HadoopPipes::Mapper {

    public:

        WordCountMapper(HadoopPipes::TaskContext& context) {}

        void map(HadoopPipes::MapContext& context)
        {
            vector<string> words = HadoopUtils::splitString(context.getInputValue(), " 

阅读全文…

hadoop笔记 (2):pipes例子分析 (1)

Pipes 是 hadoop 提供的 c++ 接口,但是在官网上找不到 pipes 的文档,只能从例子开始一点点摸索。实验环境是 debian 6 amd64,hadoop 1.0.3。hadoop 的安装目录是 $HOME/hadoop,安装和配置过程在上一篇安装笔记中有提到。

为了少敲些字符,给 hadoop 命令做了一个 alias:

alias hadoop='$HOME/hadoop/bin/hadoop'

单词统计程序

下面的程序是对 hadoop 1.0.3 自带的单词统计程序(src/examples/pipes/impl/wordcount-simple.cc)的一个修改版:

#include <string>
#include <vector>
using namespace std;

#include <hadoop/Pipes.hh>
#include <hadoop/StringUtils.hh>
#include <hadoop/TemplateFactory.hh>

class WordCountMapper 

阅读全文…