MapReduce分布式计算

    xiaoxiao2023-10-28  145

    MapReduce简介

    MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算,用于解决海量数据的计算问题。 MapReduce分成了两个部分: 1、映射(Mapping)对集合里的每个目标应用同一个操作。即,如果你想把表单里每个单元格乘以二,那么把这个函数单独地应用在每个单元格上的操作就属于mapping。 2、化简(Reducing)遍历集合中的元素来返回一个综合的结果。即,输出表单里一列数字的和这个任务属于reducing。

    每一个Map任务处理输入数据中的一部分,当Map任务完成后,它会生成一些中间文件,这些中间文件将会作为Reduce任务的输入数据。 Reduce任务的主要目标就是把前面若干个Map的输出汇总到一起并输出。 MapReduce的伟大之处就在于编程人员在不会分布式并行编程的情况下,将自己的程序运行在分布式系统上。

    Mapper负责“分”: 分解计算任务,规模大大缩小; “计算向数据靠近” ; 这些小任务可以并行计算。 Reducer负责“汇总” map阶段的结果

    MapReduce模型 MapReduce函数 1.Combiner函数,本地化的reducer 2.Partitioner函数,决定着Map节点的输出将被分区到哪个Reduce节点 把map task的输出结果有效地传送到reduce端:map输出之前,在内存里经过sort和combiner,再将所有的输出集合到partitioner进行划分到不同的reducer,在每个分区(partition)中,再进行内存中排序,再运行combiner,最后输出到HDFS。

    MapReduce的输入输出 <key,value>键值对 Map和Reduce任务 1.Map处理

    public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>

    2.Reduce处理

    public class WordCountReducer extends Reducer <Text, IntWritable, Text, IntWritable>

    3.Job配置

    public class WordCountDriver //关联使用的Mapper类 job.setMapperClass(WordCountMapper.class); //关联使用的Reducer类 job.setReducerClass(WordCountReducer.class); public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ //对数据进行打散 protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { //输入数据 hello world lovework String line = value.toString(); //对数据切分 String[] words=line.split(" "); //写出<hello, 1> for(String w:words) { //写出reducer端 context.write(new Text(w), new IntWritable(1)); }}} public class WordCountReducer extends Reducer <Text, IntWritable, Text, IntWritable>{ protected void reduce(Text Key, Iterable values, Context context) throws IOException, InterruptedException { //记录出现的次数 int sum=0; //累加求和输出 for(IntWritable v:values) { sum +=v.get(); } context.write(Key, new IntWritable(sum)); } } public class WordCountDriver { public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException,InterruptedException { // 设置root权限 System.setProperty(“HADOOP_USER_NAME”,“root”); //创建job任务 Configuration conf=new Configuration(); Job job=Job.getInstance(conf); //指定jar包位置 job.setJarByClass(WordCountDriver.class); //关联使用的Mapper类 job.setMapperClass(WordCountMapper.class); //关联使用的Reducer类 job.setReducerClass(WordCountReducer.class); //设置Mapper阶段输出的数据类型 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); //设置Reducer阶段输出的数据类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); //设置数据输入路径和文件名 FileInputFormat.setInputPaths(job, new Path(“c:\in\aa.txt”)); //设置数据输出路径 FileOutputFormat.setOutputPath(job, newPath(“c:\out”)); //提交任务 Boolean rs=job.waitForCompletion(true); //退出 System.exit(rs?0:1); } }

    从启动和资源调度来看MapReduce过程 客户端(Client) 编写mapreduce程序,配置作业,提交作业,这就是程序员完成的工作。 JobTracker JobTracker是一个后台服务进程,启动之后,会一直监听并接收来自各个TaskTracker发送的心跳信息,包括资源使用情况和任务运行情况等信息。 TaskTracker TaskTracker是JobTracker和Task之间的桥梁。TaskTracker与JobTracker和Task之间采用了RPC协议进行通信。 HDFS 保存作业的数据、配置信息等等,最后的结果也是保存在hdfs上面

    总结

    MapReduce是Hadoop最重要的组成模块之一,分为Map和Reduce两部分,利用框架在计算机集群上按需求运行多个程序实例来处理各个子任务,然后对结果进行输出。在实际的工作环境中,MapReduce也有广范的应用。本章的实验,就是熟悉掌握MapReduce的简单编程应用,为深入学习大数据打基础。

    最新回复(0)