剖解MapReduce

Hadoop数据类型
为了能让MapReduce的key/value对能够在集群中移动,MapReduce框架提供了一个序列化key/value对的方法
但MapReduce并不允许任意的类都能做为key,只有实现了WriableComparable或者Wirable接口(说明,Wriable也实现了WriableComparable接口)的类才能做为键,因为在reduce阶段要根据key来进行排序,并将相同key的值进行归并。
所以要想自己写的类能做为key的话,则此类必须实现Comparable接口。


下面我们来写一个这样的类代表两个城市之间的航
package csdn.jtlyuan;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;
public class Edge implements WritableComparable<Edge>{
	private String startNode;
	private String endNode;
	@Override
	public void readFields(DataInput in) throws IOException {
		this.startNode=in.readUTF();
		this.endNode=in.readUTF();	
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeUTF(this.startNode);
		out.writeUTF(this.endNode);
	}

	@Override
	public int compareTo(Edge o) {
		return (this.startNode.compareTo(o.startNode) != 0)?
				this.startNode.compareTo(o.startNode):
					this.endNode.compareTo(o.endNode);
	}

}



MapReduce程序的各种阶段:

Mapper:把输入映射为key value形式
Reducer:接受来自各个mapper的输出,它按照key/value对的key进行排序,并将相同key的值归并放在同一个reducer里。
Partition:重定向Mapper输出,默认的作法是对键的散列来觉得reducer,hadoop可以通过HashPartitioner类改变这种策略。
Combiner:本地reduce
Shuffle:当第一个map任务完成后,节点可能还要继续执行更多的map任务,但这时候也开始把map任务的中间输出交换到需要它们的reducer那里去,这个移动map输出到reducer的过程叫做shuffle

举例:WordCount程序:
import java.io.IOException;
import java.util.StringTokenizer;

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.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;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

  public static class TokenizerMapper 
       extends Mapper<Object, Text, Text, IntWritable>{
    
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
      
    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }
  
  public static class IntSumReducer 
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values, 
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);//Context接受reduce阶段的输出
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length != 2) {
      System.err.println("Usage: wordcount <in> <out>");
      System.exit(2);
    }
    Job job = new Job(conf, "word count");//根据配置来实例化一个Job,并给Job设置名字
   
    job.setJarByClass(WordCount.class);//Set the Jar by finding where a given class came from.
    job.setMapperClass(TokenizerMapper.class);//Set the Mapper for the job
    job.setCombinerClass(IntSumReducer.class);//Set the combiner class for the job.
    job.setReducerClass(IntSumReducer.class);//Set the Reducer for the job
    
    job.setOutputKeyClass(Text.class);//Set the key class for the job output data.
    job.setOutputValueClass(IntWritable.class);//Set the value class for job outputs.
    
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//设置输入路径
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//设置输出位置 (在运行之前,该目录不应该存在,否则会报错并拒绝运行该任务)
   
    System.exit(job.waitForCompletion(true) ? 0 : 1);//job.waitForCompletion Submit the job to the cluster and wait for it to finish.
  }
}



下面的图可以清晰得开到怎样对两个文件的单词进行统计的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AOP(Aspect-Oriented Programming,面向方面编程)是对OOP(Object-Oriented Programming,面向对象编程)的补充和完善。它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,即方面。AOP的核心思想是将与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,以减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。 AOP将软件系统分为两个部分:核心关注点和横切关注点。核心关注点是业务处理的主要流程,而横切关注点是与核心关注点关系不大的部分,如权限认证、日志、事务处理等。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。通过AOP,可以实现将应用程序中的商业逻辑与对其提供支持的通用服务进行分离的目标。 实现AOP的技术主要分为两大类:一是采用动态代理技术,利用截取消息的方式对消息进行装饰,以取代原有对象行为的执行;二是采用静态织入的方式,引入特定的语法创建“方面”,从而使得编译器可以在编译期间织入有关“方面”的代码。 总结来说,AOP的核心思想是将系统中的通用功能和业务逻辑分离,使系统更加模块化、可维护和可扩展。通过AOP,我们可以将一些横切关注点(如日志、权限控制等)与核心业务逻辑相分离,从而提高代码的复用性和可读性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值