一、实验题目
编写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的顺序将句子排列好了。
五、总结
现在的任务越来越重了,不能按照原来一天做一周的安排来了。本周时间安排出了问题,这次文档代码有问题,多亏群友帮助才能完成实验,下周应尽早实验。