当在mac上搭建好开发环境之后,第一件事肯定是找一个hello world程序练习下。而hadoop世界的hello word程序就是下面的这个word count程序。 1. 新建项目 步骤:filenewothermap/reduce project 项目名可以随便取,如mapreducesample。然后新建类wordcount.ja
当在mac上搭建好开发环境之后,第一件事肯定是找一个hello world程序练习下。而hadoop世界的hello word程序就是下面的这个word count程序。
1. 新建项目步骤:file–>new–>other–>map/reduce project
项目名可以随便取,如mapreducesample。然后新建类wordcount.java,其代码如下:
package com.lifeware.test;
import java.io.ioexception;
import java.util.*;
import org.apache.hadoop.fs.path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class wordcount {
public static class map extends mapreducebase implements mapper {
private final static intwritable one = new intwritable(1);
private text word = new text();
? ? ? ? public void map(longwritable key, text value, outputcollector output, reporter reporter) throws ioexception {
string line = value.tostring();
stringtokenizer tokenizer = new stringtokenizer(line);
while (tokenizer.hasmoretokens()) {
word.set(tokenizer.nexttoken());
output.collect(word, one);
}
}
}
public static class reduce extends mapreducebase implements reducer {
public void reduce(text key, iterator values, outputcollector output, reporter reporter) throws ioexception {
int sum = 0;
while (values.hasnext()) {
sum += values.next().get();
}
output.collect(key, new intwritable(sum));
}
}
/**
* @param args
* @throws ioexception
*/
public static void main(string[] args) throws ioexception {
// todo auto-generated method stub
jobconf conf = new jobconf(wordcount.class);
conf.setjobname(“wordcount”);
? ?conf.setoutputkeyclass(text.class);
conf.setoutputvalueclass(intwritable.class);
conf.setmapperclass(map.class);
conf.setcombinerclass(reduce.class);
conf.setreducerclass(reduce.class);
? ?conf.setinputformat(textinputformat.class);
conf.setoutputformat(textoutputformat.class);
? ?fileinputformat.setinputpaths(conf, new path(args[0]));
fileoutputformat.setoutputpath(conf, new path(args[1]));
? ?jobclient.runjob(conf);
}
}
2. 数据准备为了运行程序,我们分别需要一个输入和输出的文件夹。输出文件夹,在程序运行完成后会自动生成。我们需要给程序传人一个输入文件夹。
2.1. ?准备本地文件
在当前项目目录下新建文件夹input,并在文件夹下新建两个文件file1、file2,这两个文件内容分别如下:
?file1: ? ?hello world bye world
file2: ? ? ?hello hadoop goodbye hadoop
2.2. 将文件夹input上传到分布式文件系统中?
在已经启动hadoop守护进程终端中cd 到hadoop安装目录,运行下面命令:
bin/hadoop fs -put ../test/input input
将input文件夹上传到了hadoop文件系统后,在该系统下就多了一个input文件夹,你可以使用下面命令查看:
bin/hadoop fs -ls
或者直接通过eclipse插件,查看dfs locations显示:
3. 运行项目3.1. ?在新建的项目mapreducesample,点击wordcount.java,右键–>run as–>run configurations
3.2. 在弹出的run configurations对话框中,点java application,右键–>new,这时会新建一个application名为wordcount
3.3. ?配置运行参数,点arguments,在program arguments中输入“你要传给程序的输入文件夹和你要求程序将计算结果保存的文件夹”,如:
hdfs://localhost:9000/user/metaboy/input hdfs://localhost:9000/user/metaboy/output
这里面的input就是你刚传上去文件夹。文件夹地址你可以根据自己具体情况填写。
4.运行程序点击run,运行程序,过段时间将运行完成,等运行结束后,可以在终端中用命令:
? ? ?bin/hadoop fs -ls
或者使用插件hadoop eclipse插件查看是否生成文件夹output。
?5. 结果查看用下面命令查看生成的文件内容:
? ? bin/hadoop fs -cat output/*
运行完这个程序之后,基本上就算是步入到hadoop这个大家族啦!
原文地址:第一个map/reduce程序, 感谢原作者分享。