hadoop如何执行自己编写的MapReduce程序

转载 2015年01月26日 09:58:52

比如我们现在写好了一个mapred程序如下:

package com.besttone.mapred;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class SingleWordCount {

	public static class SingleWordCountMapper extends
			Mapper<Object, Text, Text, IntWritable> {
		private final static IntWritable one = new IntWritable(1);
		private Text word = new Text();

		public void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {

			StringTokenizer itr = new StringTokenizer(value.toString());
			String keyword = context.getConfiguration().get("word");
			while (itr.hasMoreTokens()) {
				String nextkey = itr.nextToken();
				if (nextkey.trim().equals(keyword)) {
					word.set(nextkey);
					context.write(word, one);
				}
			}
		}

	}

	public static class SingleWordCountReducer extends
			Reducer<Text, IntWritable, Text, IntWritable> {

		private IntWritable result = new IntWritable();

		public void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			result.set(sum);
			context.write(key, result);
		}
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Configuration conf = new Configuration();

		String[] otherArgs = new GenericOptionsParser(conf, args)
				.getRemainingArgs();

		if (otherArgs.length != 3) {
			System.err.println("Usage: singlewordcount <in> <out> <word>");
			System.exit(2);
		}
		conf.set("word", otherArgs[2]);
		Job job = new Job(conf, "single word count");
		job.setJarByClass(SingleWordCount.class);
		job.setMapperClass(SingleWordCountMapper.class);
		job.setCombinerClass(SingleWordCountReducer.class);
		job.setReducerClass(SingleWordCountReducer.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

}


这个mapred程序是用来统计指定单词的数量。
然后我们可以把这个类打包成JAR,比如命名叫:myexample.jar。拷贝到远程HADOOP_HOME目录下,比如我们统计input目录下的"hello"这个单词的个数,执行bin/hadoop jar myexample.jar com.besttone.mapred.SingleWordCount hdfs://master:9000/user/hadoop/input/* hdfs://master:9000/user/hadoop/output hello 。


另外一只执行方式是写一个Driver程序:

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.besttone.mapred;

import org.apache.hadoop.util.ProgramDriver;

/**
 * A description of an example program based on its class and a 
 * human-readable description.
 */
public class MapRedDriver {
  
  public static void main(String argv[]){
    int exitCode = -1;
    ProgramDriver pgd = new ProgramDriver();
    try {
      pgd.addClass("singlewordcount", SingleWordCount.class, 
                   "A map/reduce program that counts the words in the input files.");
     
      pgd.driver(argv);
      
      // Success
      exitCode = 0;
    }
    catch(Throwable e){
      e.printStackTrace();
    }
    
    System.exit(exitCode);
  }
}
	

然后和上面那个类一起重新打包成JAR,双击jar文件打开,修改META-INF下的MANIFEST.MF文件如下:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 20.6-b01 (Sun Microsystems Inc.)
Main-Class: com/besttone/mapred/MapRedDriver

将Main-Class设置为Driver的全路径名,然后将jar包拷贝到hadoop_home目录下。这时候就可以不用写mapred的全路径名了,而是使用Driver里定义的别名:

bin/hadoop jar myexample.jar singlewordcount hdfs://master:9000/user/hadoop/input/* hdfs://master:9000/user/hadoop/output hello 。

 

执行过程中可能会遇到hadoop mapred执行目录文件权限问题
   错误信息如下:

   job Submission failed with exception 'java.io.IOException(The ownership/permissions on the staging directory /tmp/hadoop-hadoop-user1/mapred/staging/hadoop-user1/.staging is not as expected. It is owned by hadoop-user1 and permissions are rwxrwxrwx. The directory must be owned by the submitter hadoop-user1 or by hadoop-user1 and permissions must be rwx------)

   修改权限:

       bin/hadoop fs -chmod  -R 700 /home/hadoop/tmp



Hadoop实战之路——第四章 使用Eclipse编写Hadoop程序

4.1 使用VNC远程桌面连接Linux        下面以centOS6.4为例进行
  • xiangchengguan
  • xiangchengguan
  • 2014-05-29 10:34:17
  • 1566

第一个hadoop程序:WordCount

在windows8.1+eclipse编写hadoop程序,并尝试运行,步骤如下: 1.在Eclipse开发环境中创建JAVA工程 双击桌面上的Eclipse的快捷方式。首先选择菜单“Fi...
  • hexiaofen1996
  • hexiaofen1996
  • 2017-03-30 19:26:30
  • 195

编写MapReduce程序

MapReduce就是一系列键值变换 一个完整的MapReduce作业,涉及三个要素:Mapper、Reducer的Driver,可以将处理过程描述成 {K1,V1} -> {K2,List} ->{...
  • u012135300
  • u012135300
  • 2016-04-11 22:27:02
  • 1113

用Python写一个 Hadoop MapReduce 程序

写作缘由 尽管Hadoop的框架是用Java写的,但是基于Hadoop运行的程序并不一定要用Java来写,我们可以选择一些其他的编程语言比如Python或者C++。不过,Hadoop的文档以及H...
  • u010159842
  • u010159842
  • 2016-11-15 17:54:53
  • 4247

hadoop上的两种运行mapreduce程序的方法

之前学习了一段时间的hadoop的相关知识 ,学习理论基础的时候要同时实际操作才能对它更熟练,废话不多说来说说在hadoop上运行一个最简单的words count的程序 首先我先贴上这个程序的源...
  • YQlakers
  • YQlakers
  • 2017-04-13 19:22:18
  • 3254

打包运行自己的MapReduce程序

http://www.powerxing.com/hadoop-build-project-by-shell/
  • guotong1988
  • guotong1988
  • 2015-11-09 20:00:33
  • 1216

Hadoop学习笔记之如何运行一个MapReduce程序

Hadoop学习笔记之如何运行一个MapReduce程序        MapReduce可以分为两个阶段来处理,一个阶段为map,另一个阶段为reduce.每个阶段都有键值对的输入和输出参数,...
  • accptanggang
  • accptanggang
  • 2015-03-02 16:49:06
  • 872

hadoop上运行Java程序

第一种:原生态运行jar包 1,利用eclipse编写Map-Reduce方法,一般引入Hadoop-core-1.1.2.jar。注意这里eclipse里没有安装hadoop的插件,只是引入其匝包,...
  • wangyang1354
  • wangyang1354
  • 2015-04-20 20:47:41
  • 4811

MapReduce编程(入门篇)

一. MapReduce 编程模型 还是以一个经典的图片来说明问题. 1. 首先, 我们能确定我们有一份输入, 而且他的数据量会很大 2. 通过split之后, 他变成了若干...
  • zmx729618
  • zmx729618
  • 2016-10-09 14:41:42
  • 3474

mapreduce程序编写规范

1:用户编写的程序分成三个部分:Map,Reducer,Driver(用户提交mr程序的客户端) 2:map的输入数据是KV对的形式(kv的类型可以自定义) 3:map的输出数据是KV对的形式(k...
  • qq_30259339
  • qq_30259339
  • 2016-11-27 16:14:53
  • 454
收藏助手
不良信息举报
您举报文章:hadoop如何执行自己编写的MapReduce程序
举报原因:
原因补充:

(最多只允许输入30个字)