温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MapReduce如何实现驱动程序

发布时间:2021-12-10 13:51:02 来源:亿速云 阅读:110 作者:小新 栏目:云计算

这篇文章给大家分享的是有关MapReduce如何实现驱动程序的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

1、设置job的基础属性

Job job = new Job();  
job.setJarByClass(***.class);  //要执行的类
job.setJobName(“job name”);    //作业的名字
job.setNumReduce(2);           //reduce的数目

2、设置Map与Reudce的类

job.setMappgerClass(*.class);  //map类
job.setReduceClass(*.class);   //reduce类

3、设置Job的输入输出格式

void    setInputFormatClass(Class<? extends InputFormat> cls)  
void    setOutputFormatClass(Class<? extends OutputFormat> cls)

前者默认是TextInputFormat,后者是FileOutputFormat。

4、设置Job的输入输出路径

当输入输出是文件时,需要指定路径。

InputFormat:  
static void    addInputPath(JobConf conf, Path path)   
FileOutputFormat:  
static void    setOutputPath(Job job, Path outputDir)

当输入格式是其它类型时,则需要指定相应的属性,如Gora的DataSource。

5、设置map与reduce的输出键值类型
主要有以下4个类

void    setOutputKeyClass(Class<?> theClass)  
void    setOutputValueClass(Class<?> theClass)   
void    setMapOutputKeyClass(Class<?> theClass)  
void    setMapOutputValueClass(Class<?> theClass)

(1)前面2个方法设置整个job的输出,即reduce的输出。默认情况下,map的输出类型与reduce一致,若二者不一致,则需要通过后面2个方法来指定map的输出类型。
(2)关于输入类型的说明:reduce的输入类型由output的输出类型决定。map的输入类型由输入格式决定,如输入格式是FileInputFormat,则输入KV类型为LongWriterable与Text。

6、运行程序

job.waitForCompletion()

我们还可以设置combine类和partition类

job.setCombinerClass(Combine.class);
job.setPartitionerClass(MyPartition.class);

 附带一张图:

MapReduce如何实现驱动程序

完整例子

package org.jediael.hadoopdemo.maxtemperature;  
  
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.lib.input.FileInputFormat;  
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  
public class MaxTemperature {  
    public static void main(String[] args) throws Exception {  
        if (args.length != 2) {  
            System.err  
                    .println("Usage: MaxTemperature <input path> <output path>");  
            System.exit(-1);  
        }  
        //1、设置job的基础属性  
        Job job = new Job();  
        job.setJarByClass(MaxTemperature.class);  
        job.setJobName("Max temperature");  
  
        //2、设置Map与Reudce的类  
        job.setMapperClass(MaxTemperatureMapper.class);  
        job.setReducerClass(MaxTemperatureReducer.class);  
          
        //4、设置map与reduce的输出键值类型  
        job.setOutputKeyClass(Text.class);  
        job.setOutputValueClass(IntWritable.class);  
          
        //5、设置输入输出路径  
        FileInputFormat.addInputPath(job, new Path(args[0]));  
        FileOutputFormat.setOutputPath(job, new Path(args[1]));  
          
        //6、运行程序  
        System.exit(job.waitForCompletion(true) ? 0 : 1);  
    }  
}

感谢各位的阅读!关于“MapReduce如何实现驱动程序”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI