1.2.1 eclipse开发环境

配置hadoop-eclipse开发环境

由于hadoop-eclipse-1.2.1插件需要自行编译,所以为了图省事而从网上直接下载了这个jar包,所以如果有需要可以从点击并下载资源。下载这个jar包后,将它放置在eclipse/plugins目录下,并重启eclipse即可。

如果你需要自己编译该插件,请参考文献。

配置hadoop-1.2.1与eclipse链接信息

如果没有意外,在你的eclipse的右上角应该出现了一只蓝色的大象logo,请点击那只大象。之后,在正下方的区域将会多出一项Map/Reduce Locations的选项卡,点击该选项卡,并右键新建New Hadoop Location。

这时应该会弹出一个对话框,需要你填写这些内容:

Location name 指的是当前创建的链接名字,可以任意指定;Map/Reduce Master 指的是执行MR的主机地址,并且需要给定hdfs协议的通讯地址;DFS Master 指的是Distribution File System的主机地址,并且需要给定hdfs协议的通讯地址; User name 指定的是链接至Hadoop的用户名。

参考上一篇文章的设计,hadoop-1.2.1集群搭建,这里的配置信息将沿用上一篇文章的设定。

因此,我们的设置情况如下

参数名配置参数说明

Location namehadoop

MapReduce MasterHost: 192.168.145.100NameNode 的IP地址

MapReduce MasterPort: 8021MapReduce Port,参考自己配置的mapred-site.xml

DFS MasterPort: 8020DFS Port,参考自己配置的core-site.xml

User namehadoop

之后,切换到Advanced parameters,而你需要修改的有如下参数

参数名配置参数说明

fs.default.namehdfs://192.168.145.100:8020参考core-site.xml

hadoop.tmp.dir/home/hadoop/hadoopdata/tmp参考core-site.xml

mapred.job.trackerhdfs://192.168.145.100:8021参考mapred-site.xml

之后确认,这样便在eclipse左边出现了HDFS的文件结构。但是现在你只能查看,而不能添加修改文件。因此你还需要手工登录到HDFS上,并使用命令修改权限。

./bin/hadoop fs -chmod -R 777 /

在完成这些步骤后,,需要配置最后的开发环境了。

配置开发环境

如果是在Windows上模拟远程开发,那么你需要将hadoop-1.2.1.tar.gz解压一份,我们将解压后得到的hadoop-1.2.1放置在documents里

C:\Users\ISCAS\Documents\src\hadoop-1.2.1

之后,打开 eclipse -> Preferences -> Hadoop Map/Reduce,将解压后的路径添加在 hadoop installation directory 中,并点击apply使设置生效。

这个时候,我们可以试着编译一两个Hadoop程序, File -> Map/Reduce -> Map/Reduce Project 或者直接通过 Project Wizzard 新建一个Hadoop项目,并命名该项目为 Hadoop Test。

我们的第一个程序是 wordcount, 源代码可以从 ..\hadoop-1.2.1\src\examples\org\apache\hadoop\examples 中获得。

/** * Licensed 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 * * * * 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 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 TokenizerMapperextends 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 IntSumReducerextends 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); }}

这里面,为了方便,我们直接贴出该代码。准备好后,就可以直接点击 Run 命令,对代码进行编译。不过在编译前,会弹出一个小窗口,选择 Run on Hadoop,并确认。

等待一段时间,编译后并执行后,你会发现出现一段提示:

Usage: wordcount <in> <out>

WordCount例程,需要输入文件,并且需要指定输出的文件存放目录。因此,我们还需要为程序设定参数。方法是,在Run命令下,选择Run Configurations。

在 Arguments 选项卡中,Program arguments一栏里,指定输入和输出的参数。

我们给定的需要进行统计的文本存放在 /Data/words。

Mary had a little lambits fleece very white as snowand everywhere that Mary wentthe lamb was sure to go

所以设定的参数为:

hdfs://192.168.145.100:8020/Data/words hdfs://192.168.145.100:8020/out如果前世五百次眸回,才换来今生的擦肩而过。

1.2.1 eclipse开发环境

相关文章:

你感兴趣的文章:

标签云: