MapReduce 初级编程实践

(一)编程实现文件合并和去重操作**

对于两个输入文件,即文件 A 和文件 B,请编写 MapReduce 程序,对两个文件进行合并, 并剔除其中重复的内容,得到一个新的输出文件 C。下面是输入文件和输出文件的一个样例供参考。

输入文件 A 的样例如下:

20170101 x 
20170102 y
20170103 x
20170104 y
20170105 z
20170106 x

输入文件 B的样例如下:

20170101 y
20170102 y
20170103 x
20170104 z
20170105 y

根据输入文件 A 和 B 合并得到的输出文件 C 的样例如下:

20170101 x
20170101 y
20170102 y
20170103 x
20170104 y
20170104 z
20170105 y
20170105 z
20170106 x

启动hadoop:

cd /usr/local/hadoop
./sbin/start-dfs.sh

新建input文件夹,向hdfs上传文件,将家目录下的A.txt和B.txt上传到hdfs的/user/hadoop/input下

./bin/hdfs dfs -mkdir input
./bin/hdfs dfs -ls
./bin/hdfs dfs -put ~/A.txt input
./bin/hdfs dfs -put ~/B.txt input
./bin/hdfs dfs -ls input

在这里插入图片描述

启动eclipse,编程实现文件合并和去重操作:

package mapReduce;

import java.io.IOException;

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

public class MergeHeavy {
   
	
	public static class Map extends Mapper<Object, Text, Text, Text>{
   
		private static Text text = new Text();
		public void map(Object key, Text value, Context context) throws IOException,InterruptedException{
   
			text = value;
			context.write(text, new Text(""));
		}
	}
	
	public static class Reduce extends Reducer<Text, Text, Text, Text>{
   
		public void reduce(Text key, Iterable<Text> values, Context context ) throws IOException,InterruptedException{
   
			context.write(key, new Text(""));
		}
	}
	
	public static void main(String[] args) throws Exception{
   
		
		// TODO Auto-generated method stub
		Configuration conf = new Configuration();
		conf.set("fs.default.name","hdfs://localhost:9000");
		String[] otherArgs = new String[]{
   "input","output"}; 
		if (otherArgs.length != 2) {
   
			System.err.println("Usage: wordcount <in><out>");
			System.exit(2);
			}
		Job job = Job.getInstance(conf,"Merge and duplicate removal");//设置环境参数
		job.setJarByClass(MergeHeavy.class);
		job.setMapperClass(Map.class);
		job.setCombinerClass(Reduce.class);
		job.setReducerClass(Reduce.class);
		job.setOutputKeyClass(Text.class);//设置输出类型
		job.setOutputValue
  • 36
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cwn_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值