高阶MapReduce_2_链接多个MapReduce作业实例

这篇博客是一个链接多个MapReduce作业的小案例,接下来就来看看具体是怎么是怎么实现的:

首先,本次的小案例操作了两个数据文件,分别是:

input1:

2012-3-1 a
2012-3-2 b
2012-3-3 c
2012-3-4 d
2012-3-5 a
2012-3-6 b
2012-3-7 c
2012-3-3 c


input2:

2012-3-1 b
2012-3-2 a
2012-3-3 b
2012-3-4 d
2012-3-5 a
2012-3-6 c
2012-3-7 d
2012-3-3 c

目标操作实现结果:

	2012年3月3日 c
	2012年3月7日 c
	2012年3月6日 b
	2012年3月5日 a
	2012年3月4日 d
	2012年3月3日 c
	2012年3月2日 b
	2012年3月1日 a
	2012年3月3日 c
	2012年3月7日 d
	2012年3月6日 c
	2012年3月5日 a
	2012年3月4日 d
	2012年3月3日 b
	2012年3月2日 a
	2012年3月1日 b


Mapper1类:

package com.demo.mappers;

import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class Mapper1 extends Mapper<Object,Text,Text,Text>  {
	    //map将输入中的value复制到输出数据的key上,并直接输出
	 public void map(Text key,Text value,Context context)
	                throws IOException,InterruptedException{
		 		context.write(value, new Text(""));
	 }
}

Mapper2类:

package com.demo.mappers;

import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;

public class Mapper2 extends Mapper<Object,Text,Text,Text> {
	public void map(Text key,Text value,Context context)
	                throws IOException,InterruptedException{
			context.write(key, new Text(value.toString().split(" ")[0]));
	}
}


Reduce1类:

package com.demo.reducers;

import java.io.IOException;

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

public class Reducer1 extends Reducer<Text,Text,Text,Text> {
		@Override
		protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
		            throws IOException, InterruptedException {
			for(Text value : values){
				String[] s = value.toString().split("-");
				StringBuffer disStr = new StringBuffer();
				disStr.append(s[0]).append("年").append(s[1]).append("月").append(s[2].split(" ")[0]).append("日").append(" "+s[2].split(" ")[1]);
				context.write(key, new Text(disStr.toString()));
			}
		}
}

程序入口:

package com.demo.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.demo.mappers.Mapper1;
import com.demo.mappers.Mapper2;
import com.demo.reducers.Reducer1;

public class FirstMain {

		public static void main(String[] args) {
			try {
					Configuration conf = new Configuration();
					Job job = Job.getInstance(conf, "MyJob");
					job.setJarByClass(FirstMain.class);
					     //设置Map和Reduce处理链
					JobConf mapper1Conf = new JobConf(false);
					ChainMapper.addMapper(job, Mapper1.class, LongWritable.class, Text.class,  Text.class, Text.class, mapper1Conf);
					JobConf mapper2Conf = new JobConf(false);
					ChainMapper.addMapper(job, Mapper2.class, Text.class, Text.class, Text.class, Text.class,  mapper2Conf); 
					JobConf reduceConf = new JobConf(false);
					ChainReducer.setReducer(job, Reducer1.class, LongWritable.class, Text.class, Text.class, Text.class, reduceConf);
					     //设置输出类型
					job.setOutputKeyClass(Text.class);
					job.setOutputValueClass(Text.class);
					     //设置输入和输出目录
					FileInputFormat.addInputPath(job, new Path("hdfs://localhost:9000/input/input*"));
					FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:9000/output/firstOutput3"));
					System.exit(job.waitForCompletion(true) ? 1 : 0);
			} catch (Exception e) {
					e.printStackTrace();
			}	
             }
}




运行结果:




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值