千家信息网

MapReduce如何实现驱动程序

发表于:2025-02-06 作者:千家信息网编辑
千家信息网最后更新 2025年02月06日,这篇文章给大家分享的是有关MapReduce如何实现驱动程序的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1、设置job的基础属性Job job = new Job();j
千家信息网最后更新 2025年02月06日MapReduce如何实现驱动程序

这篇文章给大家分享的是有关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 cls)
void setOutputFormatClass(Class 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);

附带一张图:

完整例子

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 ");
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如何实现驱动程序"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0