028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

MapReduce的入门-创新互联

1. MapReduce 的介绍:

   MapReduce 是一个分布式运算程序的编程框架,核心功能是将用户编写的业务逻辑代码和自带默认组件整合成一个完整的分布式运算程序,并发运行在一个 Hadoop 集群上。
  MapReduce大体上分三个部分:
  - MRAppMaster:MapReduce Application Master,分配任务,协调任务的运行
  - MapTask:阶段并发任,负责 mapper 阶段的任务处理 YARNChild
  - ReduceTask:阶段汇总任务,负责 reducer 阶段的任务处理 YARNChild

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名雅安服务器托管、营销软件、网站建设、福海网站维护、网站推广。

2.MapReduce编写代码的流程:

3.WordCount 案例:

public class MyWordCount {
    public static void main(String[] args) {
        // 指定 hdfs 相关的参数
        Configuration conf=new Configuration(true);
        conf.set("fs.defaultFS","hdfs://hadoop01:9000");
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        try {
            // 新建一个 job 任务
            Job job=Job.getInstance(conf);
            // 设置 jar 包所在路径
           job.setJarByClass(MyWordCount.class);
            // 指定 mapper 类和 reducer 类
            job.setMapperClass(Mapper.class);
            job.setReducerClass(MyReduce.class);

            // 指定 maptask 的输出类型,注意,如果maptask的输出类型与reducetask输出类型一样,mapTask可以不用设置
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
            // 指定 reducetask 的输出类型
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);

            // 指定该 mapreduce 程序数据的输入和输出路径
            Path input=new Path("/data/input");
            Path output =new Path("/data/output");
            //一定要保证output不存在
            if(output.getFileSystem(conf).exists(output)){
                output.getFileSystem(conf).delete(output,true);  //递归删除
            }
            FileInputFormat.addInputPath(job,input);
            FileOutputFormat.setOutputPath(job,output);

            // 最后提交任务
             boolean success = job.waitForCompletion(true);
             System.exit(success?0:-1);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private class MyMapper extends Mapper{
        Text mk =new Text();
        IntWritable mv=new IntWritable(1);

        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            // 计算任务代码:切割单词,输出每个单词计 1 的 key-value 对
             String[] words = value.toString().split("\\s+");
             for(String word:words){
                 mk.set(word);
                 context.write(mk,mv);
             }
        }
    }
    private class MyReduce extends Reducer {
        IntWritable mv=new IntWritable();
        @Override
        protected void reduce(Text key, Iterable values, Context context)
                throws IOException, InterruptedException {
            int sum=0;
            // 汇总计算代码:对每个 key 相同的一组 key-value 做汇总统计
            for(IntWritable value:values){
                sum+=value.get();
            }
            mv.set(sum);
            context.write(key,mv);
        }
    }
}

4. MapReduce 程序的核心运行机制:

1)MapReduce 程序的运行流程:

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:MapReduce的入门-创新互联
分享路径:http://www.tsicrk.com/article/cssjoj.html

其他资讯

让你的专属顾问为你服务

3.4176s