运行一个Hadoop Job所需要指定的属性


1、设置job的基础属性
Job job = new Job();
job.setJarByClass(***.class);
job.setJobName(“job name”);
job.setNumReduce(2);

2、设置Map与Reudce的类
job.setMappgerClass(*.class);
job.setReduceClass(*.class);

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()。


见以下示例:

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);
	}
}




posted @ 2015-02-02 21:33  eagleGeek  阅读(586)  评论(0编辑  收藏  举报