myeclipse下搭建hadoop2.7.3开发环境

一  下载并编译  hadoop-eclipse-plugin-2.7.3.jar

二  将hadoop-eclipse-plugin-2.7.3.jar放到myeclipse的安装目录下的plugins目录下,并重启myeclipse

  在windows->preferences下可看见hadoop Map/Reduce界面,路径选择你WINDOWS下的hadoop解压后的路径。

三 选择Windows->show view->others下的MapReduce Locations

四  新建一个配置 配置如下

host为你的远程hadoop待连接的主机IP地址

Port:8020对应mapred-site.xml下的jobtracher地址 

Port:8020对应core-site.xml下的fs.default.name的端口

user name 填你windows的用户名; 

 

 

修改Advanced parameters下的参数

值对应 core-site.xml下的hadoop.tmp.dir参数

 

修改hdfs-site.xml下的dfs.permissions参数,允许连接,免权限检测

四 保存配置参数并重启myeclipse,可以看见如下的文件结构说明配置连接成功。

 

五 下载hadoop.ll和winutils.exe 到windows的hadoop/bin目录下

并将hadoop.dll添加到windows->system32目录下

五 环境测试

  新建项目:File-->New-->Other-->Map/Reduce Project ,项目名可以随便取

  它会自动添加依赖包,如下:

   

 新建如下文件:

编写实现代码,与官方例子为例

package com.duking.hadoop;

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(otherArgs.length);
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,选择run as - run configurations

右击wordcount-run as -run on hadoop

 

注意:HDFS的目录结构应如下:

protocols为输入待计算的数据。

 

查看运行结果

至此环境搭建成功!!!!!!!!!!

问题总结:环境搭建好后运行mapreduce程序发现output目录下为空,但把程序打包为jar到hadoop环境下运行是有数据输出的。

最后查资料解决方法如下:首先把

这个文件加入工程目录,注意解压的hadoop目录下有两个这个文件,不要加错了。

或者新建source folder 文件夹,将log4j.properties 放入

最后工程目录如下

然后运行程序发现报错了,错误提示为:Could not locate executable null

查阅资料后发现是没有添加HADOOP_HOME环境变量,添加即可。

如果不想重启电脑可以在代码下加如下代码

注意路径改为自己的windows  hadoop路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沙漏无语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值