Home  >  Article  >  Database  >  第一个Map/Reduce程序

第一个Map/Reduce程序

WBOY
WBOYOriginal
2016-06-07 16:31:021206browse

当在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这个大家族啦!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn