在实践中应用Hadoop MapReduce 实验2 以tab space分隔的文本排序
来源:互联网 发布:三只松鼠 淘宝 编辑:程序博客网 时间:2024/06/06 13:05
一、实验题目
编写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> {@Overridepublic 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;@Overrideprotected 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 0
- 在实践中应用Hadoop MapReduce 实验2 以tab space分隔的文本排序
- 在实践中应用Hadoop MapReduce 实验1 dictionary
- 【Mapreduce】以逗号为分隔符的WordCount词频统计
- Hadoop MapReduce 二次排序原理及其应用
- Hadoop MapReduce 二次排序原理及其应用
- Hadoop MapReduce 二次排序原理及其应用
- Hadoop MapReduce应用案例——排序
- 系统化交易在实践中的应用
- MapReduce/Hadoop的二次排序解决方案
- 重写Oracle的wm_concat函数,以自定义分隔符、排序
- MapReduce数据排序实验
- SOA案例:敏捷性在实践中的应用
- setTimeout()和setInterval()在实践中的应用
- Cascading:一个简单的Hadoop MapReduce应用
- Cascading:一个简单的Hadoop MapReduce应用
- 使用maven开发hadoop的mapreduce应用
- Java字节流数据逐行读取(readLine),处理以Tab分隔符划分的数据
- 使用Hadoop MapReduce 排序
- Struts2实现文件下载
- AngularJs的语法介绍
- c语言 大数据乘法
- Java中equals()和==的区别
- C++第七次作业
- 在实践中应用Hadoop MapReduce 实验2 以tab space分隔的文本排序
- Remove Nested Comments.
- 创建k8s集群(仅限于理论,无实践)
- AngulaJs中的简单数据绑定
- sublime3 Scala编译环境搭建
- 再论Epool
- JSON简介
- 连载shell(二):test判断,判断符号[ ],shell script的默认变量($0, $1...),shift参数变量偏移
- JSON格式规定