MapReduce運転モード

4498 ワード

MapReduce運転モード
1、ローカルモードORクラスタモード
//    local ,         
config.set("mapreduce.framework.name", "local");

//    yarn ,         
config.set("mapreduce.framework.name", "yarn");

2、データファイルの入出力パス
//        hdfs  
config.set("fs.defaultFS", "hdfs://node01:9000");

//            
config.set("fs.defaultFS", "file:///");

ヒント:クラスタモードでは、ファイルパスはhdfsパスでなければなりません.
3、ファイルの出力経路がhdfsの場合、AccessControlException:Permission deniedが現れる可能性があります.エラーメッセージは以下の通りです.
Caused by: org.apache.hadoop.ipc.RemoteException: org.apache.hadoop.security.AccessControlException: Permission denied: user=node01, access=WRITE, inode="":suh:supergroup:rwxr-xr-x 
ソリューション:
1、システム環境変数またはJVM変数にHADOOP_を追加するUSER_NAME、値はHADOOP上のLinuxを実行するユーザー名です.変更後、eclipseを再起動する必要があります(推奨シナリオ)
2、MapReduceのドライバに以下の設定を追加します.
// HADOOP_USER_NAME      hdfs     
conf.set("HADOOP_USER_NAME", "root");

WordCount完了コード
package com.theone.pureone.mymapreducer;
import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCountMR {

	/**
	 * LongWritable:    ,      
	 * Text:       
	 * Text:Mapper    Key   
	 * IntWritable:Mapper    Value   
	 * @author Pureone
	 *
	 */
	
	public static class MyMapper extends Mapper {
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			String[] split = value.toString().split("\\s+");
			for (String str : split) {
				value.set(str);
				context.write(value, new IntWritable(1));
			}
		}
	}

	/**
	 * Text: Mapper    Key   
	 * IntWritable: Mapper    Value   
	 * Text:Reducer   Key   
	 * IntWritable:Reducer   Value   
	 * @author Pureone
	 *
	 */
	public static class MyReducer extends Reducer {
		@Override
		protected void reduce(Text key, Iterable value,
				Reducer.Context context)
				throws IOException, InterruptedException {
			Iterator iterator = value.iterator();
			int count = 0;
			while (iterator.hasNext()) {
				IntWritable next = (IntWritable) iterator.next();
				count += next.get();
			}
			IntWritable sum = new IntWritable(count);
			context.write(key, sum);
		}
	}

	/**
	 *     
	 * 
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws ClassNotFoundException
	 */

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration config = new Configuration();
		//        hdfs  
		config.set("fs.defaultFS", "hdfs://node01:9000");
		System.setProperty("HADOOP_USER_NAME", "root");
		//          ,    yarn,      
		config.set("mapreduce.framework.name", "local");
		// config.set("yarn.resourcemanager.hostname", "node01");
		Job job = Job.getInstance();
		//      
		job.setJarByClass(WordCountMR.class);
		//   Mapper    Key-Value   
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		//   Reducer    Key-Value   
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		//   Mapper 
		job.setMapperClass(MyMapper.class);
		//   Reducer 
		job.setReducerClass(MyReducer.class);
		//    mapreduce            
		Path inputPath = new Path("F:\\input");
		Path outputPath = new Path("hdfs://node01:9000/first_path/output");
		FileSystem fileSystem = FileSystem.get(config);
		//           
		if (fileSystem.exists(outputPath)) {
			fileSystem.delete(outputPath, true);
		}
		//
		FileInputFormat.setInputPaths(job, inputPath);
		FileOutputFormat.setOutputPath(job, outputPath);
		//     
		boolean completion = job.waitForCompletion(true);
		System.exit(completion ? 0 : 1);
	}
}