mapreduce英语单词频次统计与单词个数统计(云计算实验作业20180517)

课程原地址:http://hbust.shiyanbar.com/course/91079

上课老师:李歆

实验时间:20180517

地点:云桌面

实验人:郭畅

【实验目的】

1) 理解mapreduce执行原理

2) 理解map,reduce阶段

3) 熟悉mapreduce代码的编写

【实验原理】

需求:

对下面原始数据进行处理,把文档中所有的英文单词进行统计相同单词的个数。

原始数据:

The ASF provides an established framework

for intellectual property and financial

contributions that simultaneously limits

potential legal exposure for

our project committers

The ASF provides an established framework

for intellectual property and financial

contributions that simultaneously limits

potential legal exposure for

our project committers

输出结果:

ASF 2

The 2

an 2

and 2

committers 2

contributions 2

established 2

exposure 2

financial 2

for 4

framework 2

intellectual 2

legal 2

limits 2

our 2

potential 2

project 2

property 2

provides 2

simultaneously 2

that 2

原理:

   首先对待处理的信息进行拆分,拆分之后在map阶段,把拆分的每个单词作为map方法的输出键,而map的方法输出的值设置为1,最后在reduce阶段对每个键的值集合进行遍历并把遍历的值进行相加,输出结果即可。

【实验环境】

本次环境是:centos6.5 + jdk1.7.0_79 + hadoop2.4.1 + eclipse

日志文件source.txt存放在桌面名为`分布式计算MapReduce开发基础`目录下的相应章节中对应的实验名下的文件夹中找寻。

jar包在桌面名为`lib`文件夹下。

【实验步骤】

一、项目准备阶段

1.1 linux系统的命令终端上切换到/simple目录,执行命令:touch source.txt创建一个文件。如图1所示

 

1

1.2 simple目录下,执行命令:vim source.txt编辑该文件,并把数据的信息内容拷贝到该文件中(拷贝时出现乱码将乱码部分直接删除即可),然后在simple目录可以查看到source.txt文件。如图2所示

 

2

1.3 本案例如果在集群中需要用到hadoop的存储和计算,所以在编写程序之前需要先启动yarn服务,可以在命令终端执行命令:start-all.sh hdfsyarn服务启动(查看服务启动共有6项,如果缺少请执行stop-all.sh关闭,重新启动)。如图3所示

 

3

二 程序编写

2.1 eclipse中的项目列表中,右键点击,选择“new“—>Java Project…”新建一个项目“EnglishWordsCount” 。 如图4所示

 

4

2.2 在项目src目录下,右键点击,选择“新建”创建一个类文件名称为“WordCountMapper”并指定包名” com.wcount” 。如图5所示

 

5

2.3 在编写“WordCountMapper”类之前需要把hadoop相关的jar包导入,首先在项目根目录下创建一个文件夹lib并把桌面 “实验资料” 里 “lib” 文件夹的jar全部拷贝到该文件中 。如图6所示

 

6

2.4 lib下所有的jar包导入到环境变量,首先全选lib文件夹下的jar包文件,右键点击,选择“build path-->add to build path,添加后,发现在项目下很多奶瓶图标的jar包。如图7所示

 

7

 

2.5  让类“WordCountMapper”继承类Mapper同时指定需要的参数类型,根据业务逻辑修改map类的内容如下。

package com.wcount;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable>{

@Override

protected void map(LongWritable key, Text value,

Mapper<LongWritable, Text, Text, LongWritable>.Context context)

throws IOException, InterruptedException {

//get values string

String valueString = value.toString();

//spile string

String wArr[] = valueString.split(" ");

//for iterator

for(int i=0;i<wArr.length;i++){

//map out key/value

context.write(new Text(wArr[i]), new LongWritable(1));

}

}

}

 

2.6 在项目src目录下指定的包名” com.wcount”下右键点击,新建一个类名为“WordCountReducer “并继承Reducer类,然后添加该类中的代码内容如下所示。

package com.wcount;
	import java.io.IOException;
	import java.util.Iterator;
	import org.apache.hadoop.io.LongWritable;
	import org.apache.hadoop.io.Text;
	import org.apache.hadoop.mapreduce.Reducer;
	public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
	@Override
	protected void reduce(Text key, Iterable<LongWritable> v2s,
			Reducer<Text, LongWritable, Text, LongWritable>.Context context)
			throws IOException, InterruptedException {
		Iterator<LongWritable> it = v2s.iterator();
		//define var sum
		long sum = 0;
		// iterator count arr
		while(it.hasNext()){
			sum += it.next().get();
		}
		context.write(key, new LongWritable(sum));
	}
	}

2.7 在项目src目录下指定的包名” com.wcount”下右键点击,新建一个测试主类名为” TestMapReducer”并指定main主方法。如图8所示

 

8

2.8 最后在项目src目录下指定的包名” com.wcount”下右键点击,新建一个测试主类名为” TestMapReducer “添加测试代码如下所示。

package com.wcount;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

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

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

public class TestMapReducer {

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

Configuration conf = new Configuration();

//conf.set("fs.default.name", "hdfs://192.168.0.202:9000");

// step1 : get a job

Job job = Job.getInstance(conf);

//step2: set jar main class

job.setJarByClass(TestMapReducer.class);

//step3: set map class and redcer class

job.setMapperClass(WordCountMapper.class);

job.setReducerClass(WordCountReducer.class);

//step4: set map reduce output type

job.setMapOutputKeyClass(Text.class);

job.setMapOutputValueClass(LongWritable.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(LongWritable.class);

//step5: set key/value output file format and input/output path

FileInputFormat.setInputPaths(job, new Path("file:///simple/source.txt"));

FileOutputFormat.setOutputPath(job, new Path("file:///simple/output"));

//step6: commit job

job.waitForCompletion(true);

}

}

 


2.9 按照以上的步骤,把mapperreducer阶段以及测试代码编写完毕之后,选中测试类” TestMapReducer “,右键点击选择`Run as--->Java Application`,查看控制台显示内容查看是否正确执行。如图9所示

 

图9

猜你喜欢

转载自blog.csdn.net/qq_40276310/article/details/80348795