在实践中应用Hadoop MapReduce 实验2 以tab space分隔的文本排序

一、实验题目

编写MapReduce程序给以tab space分割的文本排序。

二、实验目的

遍历整个文本,搜索带tab space的句子并对它们进行排序。

三、任务分析

同上一个实验一样,处理文本,必然要先观察待处理文档,由于回车符的表示不同,需要在linux中查看,如下图:

可以看到文档中一共有21句话,并且通过tab space分开了。实验目的是将这21句话分开,然后排序。
因此mapper部分就很好写了,就是按行读取文件中的内容,空的部分不读。代码如下:(代码来自于群友广州-Carl的分享)
package com.apress.hadoop.examples.ch2;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;


public class SortingMapper extends Mapper<LongWritable, Text, Text, Text> {
	@Override
	public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
		String line = value.toString();
		if (!line.isEmpty()) {
			context.write(new Text(line), new Text("temp"));
		}
	}
}
其中字符串的值保存到了键值key中,因此在reducer中操作时,操作key值就可以了。
而由于MapReduce程序是自带排序功能的,因此reducer程序十分简单,如下:
package com.apress.hadoop.examples.ch2;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class SortingReducer extends Reducer<Text, Text, IntWritable, Text> {
	int index = 0;
	@Override
	protected void reduce(Text key, Iterable<Text> values, Context context)
			throws IOException, InterruptedException {
		index ++;
		context.write(new IntWritable(index), key);
	}
}
自定义一个变量index作为键值,而后面的键值key为mapper中的key,也就是句子本身,而且已经排好序,因此按顺序来就好了。
最后是driver程序:
package com.apress.hadoop.examples.ch2;
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.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Job;

public class SortingDriver {
	public static void main(String[] args) throws Exception {
		Configuration conf =  new Configuration();
		Job job = new Job(conf, "sortingdata");
		job.setJarByClass(SortingDriver.class);
		job.setMapperClass(SortingMapper.class);
		job.setReducerClass(SortingReducer.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(IntWritable.class);
		job.setOutputValueClass(Text.class);
		job.setInputFormatClass(TextInputFormat.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.查看结果

可以看到该文件已经按照A-Z的顺序将句子排列好了。

五、总结

现在的任务越来越重了,不能按照原来一天做一周的安排来了。本周时间安排出了问题,这次文档代码有问题,多亏群友帮助才能完成实验,下周应尽早实验。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值