MapReduce

MapReduce实验报告
一 实验目的:
使用hadoop进行数据统计,并做去重处理,该实验由于采用高可用避免了集群的单点故障,可以有效避免由于namenode单点故障引起的集群崩溃
二 实验要求:
能运行MapReduce程序。
三 实验步骤:
1、数据准备
image
呼出终端,输入下面指令:
bin/hadoop fs -mkdir hdfsInput
执行这个命令时可能会提示类似安全的问题,如果提示了,请使用
bin/hadoop dfsadmin -safemode leave
来退出安全模式。
意思是在HDFS远程创建一个输入目录,我们以后的文件需要上载到这个目录里面才能执行。
在终端依次输入下面指令:
cd hadoop-1.2.1
bin/hadoop fs -put file/myTest*.txt hdfsInput
image

2、设计思路
1)将文件拆分成splits,由于测试用的文件较小,所以每个文件为一个split,并将文件按行分割形成<key,value>对,key为偏移量(包括了回车符),value为文本行。这一步由MapReduce框架自动完成,如下图:
在这里插入图片描述
2)将分割好的<key,value>对交给用户定义的map方法进行处理,生成新的<key,value>对,如下图所示:
在这里插入图片描述
3)得到map方法输出的<key,value>对后,Mapper会将它们按照key值进行排序,并执行Combine过程,将key值相同的value值累加,得到Mapper的最终输出结果。如下图:
在这里插入图片描述
4)Reducer先对从Mapper接收的数据进行排序,再交由用户自定义的reduce方法进行处理,得到新的<key,value>对,并作为WordCount的输出结果,如下图:
在这里插入图片描述
3、程序代码

package com.felix;

import java.io.IOException;

import java.util.Iterator;

import java.util.StringTokenizer;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapred.FileInputFormat;

import org.apache.hadoop.mapred.FileOutputFormat;

import org.apache.hadoop.mapred.JobClient;

import org.apache.hadoop.mapred.JobConf;

import org.apache.hadoop.mapred.MapReduceBase;

import org.apache.hadoop.mapred.Mapper;

import org.apache.hadoop.mapred.OutputCollector;

import org.apache.hadoop.mapred.Reducer;

import org.apache.hadoop.mapred.Reporter;

import org.apache.hadoop.mapred.TextInputFormat;

import org.apache.hadoop.mapred.TextOutputFormat;

/**

  • 描述:WordCount explains by Felix

  • @author Hadoop Dev Group

*/

public class WordCount

{

/**

  • MapReduceBase类:实现了Mapper和Reducer接口的基类(其中的方法只是实现接口,而未作任何事情)

  • Mapper接口:

  • WritableComparable接口:实现WritableComparable的类可以相互比较。所有被用作key的类应该实现此接口。

  • Reporter 则可用于报告整个应用的运行进度,本例中未使用。

*/

public static class Map extends MapReduceBase implements

Mapper<LongWritable, Text, Text, IntWritable>

{

/**

  • LongWritable, IntWritable, Text 均是 Hadoop 中实现的用于封装 Java 数据类型的类,这些类实现了WritableComparable接口,

  • 都能够被串行化从而便于在分布式环境中进行数据交换,你可以将它们分别视为long,int,String 的替代品。

*/

private final static IntWritable one = new IntWritable(1);

private Text word = new Text();

/**

  • Mapper接口中的map方法:

  • void map(K1 key, V1 value, OutputCollector<K2,V2> output, Reporter reporter)

  • 映射一个单个的输入k/v对到一个中间的k/v对

  • 输出对不需要和输入对是相同的类型,输入对可以映射到0个或多个输出对。

  • OutputCollector接口:收集Mapper和Reducer输出的<k,v>对。

  • OutputCollector接口的collect(k, v)方法:增加一个(k,v)对到output

*/

public void map(LongWritable key, Text value,

OutputCollector<Text, IntWritable> 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<Text, IntWritable, Text, IntWritable>

{

public void reduce(Text key, Iterator values,

OutputCollector<Text, IntWritable> output, Reporter reporter)

throws IOException

{

int sum = 0;

while (values.hasNext())

{

sum += values.next().get();

}

output.collect(key, new IntWritable(sum));

}

}

public static void main(String[] args) throws Exception

{

/**

  • JobConf:map/reduce的job配置类,向hadoop框架描述map-reduce执行的工作

  • 构造方法:JobConf()、JobConf(Class exampleClass)、JobConf(Configuration conf)等

*/

JobConf conf = new JobConf(WordCount.class);

conf.setJobName(“wordcount”); //设置一个用户定义的job名称

conf.setOutputKeyClass(Text.class); //为job的输出数据设置Key类

conf.setOutputValueClass(IntWritable.class); //为job输出设置value类

conf.setMapperClass(Map.class); //为job设置Mapper类

conf.setCombinerClass(Reduce.class); //为job设置Combiner类

conf.setReducerClass(Reduce.class); //为job设置Reduce类

conf.setInputFormat(TextInputFormat.class); //为map-reduce任务设置InputFormat实现类

conf.setOutputFormat(TextOutputFormat.class); //为map-reduce任务设置OutputFormat实现类

/**

  • InputFormat描述map-reduce中对job的输入定义

  • setInputPaths():为map-reduce job设置路径数组作为输入列表

  • setInputPath():为map-reduce job设置路径数组作为输出列表

*/

FileInputFormat.setInputPaths(conf, new Path(args[0]));

FileOutputFormat.setOutputPath(conf, new Path(args[1]));

JobClient.runJob(conf); //运行一个job

}

}

package com.hadoop.sample;

import java.io.IOException;

import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

//继承mapper接口,设置map的输入类型为<Object,Text>

//输出类型为<Text,IntWritable>

public static class Map extends Mapper<Object,Text,Text,IntWritable>{

//one表示单词出现一次

private static IntWritable one = new IntWritable(1);

//word存储切下的单词

private Text word = new Text();

public void map(Object key,Text value,Context context) throws IOException,InterruptedException{

//对输入的行切词

StringTokenizer st = new StringTokenizer(value.toString());

while(st.hasMoreTokens()){

word.set(st.nextToken());//切下的单词存入word

context.write(word, one);

}

}

}

//继承reducer接口,设置reduce的输入类型<Text,IntWritable>

//输出类型为<Text,IntWritable>

public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{

//result记录单词的频数

private static IntWritable result = new IntWritable();

public void reduce(Text key,Iterable values,Context context) throws IOException,InterruptedException{

int sum = 0;

//对获取的<key,value-list>计算value的和

for(IntWritable val:values){

sum += val.get();

}

//将频数设置到result

result.set(sum);

//收集结果

context.write(key, result);

}

}

/**

  • @param args

*/

public static void main(String[] args) throws Exception{

// TODO Auto-generated method stub

Configuration conf = new Configuration();

//检查运行命令

String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();

if(otherArgs.length != 2){

System.err.println("Usage WordCount ");

System.exit(2);

}

//配置作业名

Job job = new Job(conf,“word count”);

//配置作业各个类

job.setJarByClass(WordCount.class);

job.setMapperClass(Map.class);

job.setCombinerClass(Reduce.class);

job.setReducerClass(Reduce.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job, new Path(otherArgs[0]));

FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

System.exit(job.waitForCompletion(true) ? 0 : 1);

}

}

4、运行结果用户登录

在终端输入下面指令:

bin/hadoop jar hadoop-examples-1.2.1.jar wordcount hdfsInput hdfsOutput

注意,这里的示例程序是1.2.1版本的,可能每个机器有所不一致,那么请用*通配符代替版本号

bin/hadoop jar hadoop-examples-*.jar wordcount hdfsInput hdfsOutput

应该出现下面结果:
在这里插入图片描述
Hadoop命令会启动一个JVM来运行这个MapReduce程序,并自动获得Hadoop的配置,同时把类的路径(及其依赖关系)加入到Hadoop的库中。以上就是Hadoop Job的运行记录,从这里可以看到,这个Job被赋予了一个ID号:job_201202292213_0002,而且得知输入文件有两个(Total input paths to process : 2),同时还可以了解map的输入输出记录(record数及字节数),以及reduce输入输出记录。

查看HDFS上hdfsOutput目录内容:

在终端输入下面指令:

bin/hadoop fs -ls hdfsOutput
在这里插入图片描述
从上图中知道生成了三个文件,我们的结果在"part-r-00000"中。

使用下面指令查看结果输出文件内容

bin/hadoop fs -cat output/part-r-00000
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值