Eclipse中开发MapReduce程序步骤

开发环境上是否能运行MapReduce(使用自带的hadoop-examples.jar):

hadoop jar ./hadoop-examples.jar pi 10 10000

 

开发前提:

一般我们写的mapreduce主程序放在客户端机器上,执行任务时是在集群机器上,所以要将变量从主程序传递到我们自己写的map或者reduce函数中就不能使用全局变量,因为map和reduce函数的执行是在集群的内存中,而mapreduce主程序的执行是在客户端机器的内存中。可以使用以下方法解决:

1 首先全局变量不可以使用,因为全局变量是在运行mapreduce主程序的机器内存当中,在集群的内存中是无法调用的。

2 通过写入mapreduce文件方式,这样如果数据量比较大新增这么一个变量字段不是很合理。

3 通过数据库方式传输。

http://my.oschina.net/muou/blog/408543

1、 把hadoop-eclipse-plugin-2.6.0.jar拷贝到D:\eclipse-jee-luna-SR1-win32\eclipse\plugins目录下,重启一下Eclipse,然后可以看到DFS Locations

 2.打开Window-->Preferens,可以看到Hadoop Map/Reduc选项,然后点击,然后添加hadoop-2.6.0进来,如图所示:

 

3.配置Map/ReduceLocations

   1)点击Window-->Show View -->MapReduce Tools  点击Map/ReduceLocation

   2)点击Map/ReduceLocation选项卡,点击右边小象图标,打开Hadoop Location配置窗口: 输入Location Name,任意名称即可。配置Map/Reduce Master和DFS Mastrer,Host和Port配置成hdfs-site.xml与core-site.xml的设置一致即可

 

find / -name ‘hdfs-site.xml’ 

 

4.查看是否连接成功

5.新建MapReduce项目并运行

package org.apache.hadoop.examples;

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 WordCount {

public static class TokenizerMapper

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

while (itr.hasMoreTokens()) {

word.set(itr.nextToken());
context.write(word, one);
}
}
}

public static class IntSumReducer

extends Reducer<Text, IntWritable, Text, IntWritable> {

private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> values,
Context context)

throws IOException, InterruptedException {

int sum = 0;

for (IntWritable val : values) {

sum += val.get();

}

result.set(sum);

context.write(key, result);
}
}

public static void main(String[] args) throws Exception {

Configuration conf = new Configuration();

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

if (otherArgs.length != 2) {

System.err.println("Usage: wordcount <in> <out>");

System.exit(2);
}

Job job = new Job(conf, "word count");

job.setJarByClass(WordCount.class);

job.setMapperClass(TokenizerMapper.class);

job.setCombinerClass(IntSumReducer.class);

job.setReducerClass(IntSumReducer.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);

}

}

入参:

点击WordCount.java右击-->Run As-->Run on  Hadoop

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值