hadoop2.6.3学习第三节:win7+myeclipse2014配置开发环境,和运行WordCount

本节主要是在自己的开发机器上,搭建hadoop开发环境。

操作系统:win7 64位

开发工具:myeclipse2014

hadoop版本:2.6.3

1.首先,我们得先下载myeclipse的hadoop插件,这个网上很多有利用hadoop源码本地编译的教程,大家也可以自己试试,反正我是用现成的jar直接用的。简单方便。下载地址:http://download.csdn.net/detail/yuanfen99xia/9426342点击打开链接

2.将下载好的hadoop-eclipse-plugin-2.6.0.jar放到myeclipse安装目录的\dropins\plugins(注:这里不同版本myeclipse可能插件文件夹不一样)下。然后启动myeclipse

3.这时打开myeclipse工具栏的windows->preferences,此时可以看到左侧菜单多了Hadoop Map/Reduce,讲我们前面想下载的hadoop-2.6.3.tar.gz解压到本地,然后打开Hadoop Map/Reduce,选择我们刚才解压的目录


4.myeclipse工具栏的windows->show views->other中,选择Map/Reduce Locatcion


5.在视图栏右击,选择"new hadoo location",然后在窗口中填入参数。由于我本地没有配置host,所以原配置文件中的master换成了IP地址


1) Map/Reduce(V2)Master
对应mapred-site.xml配置中的mapreduce.jobtracker.http.address的参数,如下:
<property>
<name>mapreduce.jobtracker.http.address</name>
<value>master:50030</value>
</property>
2)DFS Master
对应core-site.xml配置中的:
<property>
<name>fs.defaultFS</name>
<value>hdfs://master:9000</value>
</property>
3) User name
如果hdfs-site.xml配置中的:
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
如上所示为false时,这个用户名随便设,否则就要改成在linux中搭建hadoop平台使用的linux帐号。此时Eclipse软件左侧会多出一个的"DFS Locations"

4) 临时目录
接着点击"Advanced parameters"从中找见"hadoop.tmp.dir",修改成为我们Hadoop集群中设置的地址,我们的Hadoop集群是"/opt/hadoop-2.6.3/tmp",这个参数在"core-site.xml"进行了配置。
6. 查看HDFS文件系统,并尝试建立文件夹和上传文件。

点击Eclipse软件左侧的"DFS Locations"下面的"myHadoop",就会展示出HDFS上的文件结构。右击文件夹,选择“create new directory",创建

"/user/input/wordCount"路径的文件夹,然后上传一个本地文件进去,用作测试,另外创建一个"/output"文件夹存放mapreduce的输出目录 

7.选择“file-》new-》map/reduce project”,点击next之后,输入一个项目名称,finsh就行。请注意,这里新建的项目之后,会自动把所有hadoop的安装包下所有jar都导入进来,这时候会有很多重复的。为了避免问题,建议把安装包的jar拿出来,然后集中存放,删除重复的,然后手动导入这里。

8.项目下创建类。WordCount.java.

package hdWordCount;

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.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;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount  {
	public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

		//每次调用map方法,会传入split中的一行数据key;该行数据所在文件中的位置下标,value;
		public void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			StringTokenizer itr = new StringTokenizer(value.toString());
			while (itr.hasMoreTokens()) {
				context.write(new Text(itr.nextToken()), new IntWritable(1));
			}
		}
	}

	public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
		public void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			context.write(key, new IntWritable(sum));
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("mapreduce.jobhistory.address", "master:10020");
		@SuppressWarnings("deprecation")
		Job job = new Job(conf);
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
	    if (otherArgs.length < 2) {
	      System.err.println("Usage: wordcount <in> [<in>...] <out>");
	      System.exit(2);
	    }
		job.setJarByClass(WordCount.class);
		job.setMapperClass(TokenizerMapper.class);
		job.setCombinerClass(IntSumReducer.class);
		job.setReducerClass(IntSumReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		 for (int i = 0; i < otherArgs.length - 1; ++i) {
		      FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
		    }
		    FileOutputFormat.setOutputPath(job,new Path(otherArgs[otherArgs.length - 1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}
写好之后,右击"run as ->run as configurations",加入代码需要的文件存储目录。一个输入一个输出目录,输出目录的最末节点应该不存在,运行时自动生成,如果存在,请先删除,否则会报错

8.此时还不能运行,本地运行会报错。

先下载windows运行的工具类,将winutils.exe放到hadoop放到目录的bin目录下,将hadoop.dll放到C:\Windows\System32目录下

下载地址:http://download.csdn.net/detail/yuanfen99xia/9426446

配置环境变量:用户变量不要带bin目录,系统变量具体到bin目录



9,返回代码右击运行“run as-》run on hadoop”,如果执行没错。output目录下会存在如图的输出结果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夏V风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值