在实践中应用Hadoop MapReduce 实验1 dictionary


一、实验题目

编写MapReduce程序Dictionary。

二、实验目的

Dictionary遍历dictionary.txt文件,读取数据,并把其中的英文词汇转化为法语或意大利语。
文档格式:每行空格前为英语,空格后为法语或意大利语,中括号中为词性。

三、任务分解

今天换一种方式来写实验报告。
首先,先观察待处理文档,由于windows下与linux中回车符的表示不同(一个为\r\n,一个为\n)。
所以同一个文档在不同系统中是不同的,linux如下图。

windows如下图:

所以要在linux中查看待处理文档,这一步很关键。

从文档中可以看出,该文档实际上是一个字典,每一行对应一个词条
即每一行都是一个英文单词对应一个或是多个翻译,通过逗号分隔开。
因此负责提取感兴趣信息的mapper函数就可以写出了:
package net.pascalalma.hadoop;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import java.util.StringTokenizer;

public class WordMapper extends Mapper<Text, Text, Text, Text> {
	private Text word = new Text();

	public void map(Text key, Text value, Context context) throws IOException,
			InterruptedException {
		StringTokenizer itr = new StringTokenizer(value.toString(), ",");
		while (itr.hasMoreTokens()) {
			word.set(itr.nextToken());
			context.write(key, word);
		}
	}
}
在mapreduce中,数据的获取是按行获取的。获取之后转为String格式,再通过逗号来分离,最终将获得的值按照(key,word)的方式打包。
以linux第三行为例,aardvark orycte/rope[Noun]经过处理之后的结果应当为(aardvark,orycte)及(aardvark ,rope[Noun])

reduce部分则是要把这样相同的值整合,并且输出。因此reduce部分的代码为:
package net.pascalalma.hadoop;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;

public class AllTranslationsReducer extends Reducer<Text, Text, Text, Text> {
	private Text result = new Text();

	@Override
	protected void reduce(Text key, Iterable<Text> values, Context context)
			throws IOException, InterruptedException {
		String translations = "";
		for (Text val : values) {
			translations += "|" + val.toString();
		}
		result.set(translations);
		context.write(key, result);
	}
}
其中for(Text val:values)的意思为,map中相同key值的不同values,然后将这些value以|为分割连接起来,传给translations值。
最后将值打包。
接着写一下driver部分,整个程序就可以运行了。
package net.pascalalma.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class Dictionary {
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = new Job(conf, "dictionary");
		job.setJarByClass(Dictionary.class);
		job.setMapperClass(WordMapper.class);
		job.setCombinerClass(AllTranslationsReducer.class);
		job.setReducerClass(AllTranslationsReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setInputFormatClass(KeyValueTextInputFormat.class);
		job.setOutputFormatClass(TextOutputFormat.class);
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		boolean result = job.waitForCompletion(true);
		System.exit(result ? 0 : 1);
	}
}

四、实验步骤

1.将Hadoop的安全模式关闭,命令为:
hadoop dfsadmin -safemode leave
2.将待处理文件导入到hdfs文件中,命令为:
bin/hadoop dfs -copyFromLocal 源文件位置 hdfs:/
3.启动eclipse,建立Java project,导入相关jar文件,开始编码。
4.编码完毕后export成jar文件
5.执行mapreduce

6.查看运行结果。

五、总结

尝试着分析了一下mapper,reducer函数究竟是怎样写成的。
由于对java以及hadoop内的类不够熟悉,虽然搜索了相关资料,但错误难以避免,希望各位看官能指出不足。
我接下来的实验也将采用这种方式,下周的实验要多查官方文档,希望能够写出更加详细的实验报告。



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值