Hadoop2.6.2的Eclipse插件的使用

时间:2023-03-10 02:26:09
Hadoop2.6.2的Eclipse插件的使用

欢迎转载,且请注明出处,在文章页面明显位置给出原文连接。

本文链接:http://www.cnblogs.com/zdfjf/p/5178197.html

首先给出eclipse插件的下载地址:http://download.****.net/download/zdfjf/9421244

  • 1.插件的安装

插件下载后,放在eclipse安装目录下的plugins文件夹下,然后重启eclipse,就会发现Project Explorer窗口里多出DFS Locations这一项,对应的是HDFS里存放的文件,现在里边还没有显示目录结构,不用着急,第二步配置之后,目录结构就会出现了。

Hadoop2.6.2的Eclipse插件的使用

我突然想起来博客园上有一篇文章对这部分介绍的很好,而且我感觉对这一部分,我不会写的比他好。所以我就不浪费时间了,直接参考虾皮工作室的,原文链接http://www.cnblogs.com/xia520pi/archive/2012/05/20/2510723.html,可以对这一部分配置完成,下面我们要说的是配置完成后,有一些问题导致运行程序不能成功。通过不断调试,我把我运行成功的代码和相应的配置贴出来。

  • 2.代码
 /**
* 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 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 {
System.setProperty("HADOOP_USER_NAME", "hadoop");
Configuration conf = new Configuration();
conf.set("mapreduce.framework.name", "yarn");
conf.set("yarn.resourcemanager.address", "192.168.0.1:8032");
conf.set("mapreduce.app-submission.cross-platform", "true");
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = new Job(conf, "word count1");
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);
}
}

这里第69行,因为我windows上用户名为frank,集群上用户名是hadoop ,所以这里增加配置文件,把HADOOP_USER_NAME设置为hadoop。第71和72行是因为配置文件没有起作用,如果不加这两行,会以本地方式运行,没有提交到集群上运行。第73行因为是跨平台的,windows->linux,所以加上这一句。

然后,最重要的一步来了,注意,注意,注意,重要的事说3遍。

插件本来会自动把项目打成jar包,上传运行。但是有问题,现在不会自动打包。所以,我们要把project打成jar包,然后build path ,配置为项目的外部依赖包,然后右键run as -> run on hadoop.就能运行成功了。

ps:这是我的一种方法,在配置的过程中,遇到的问题多种多样,造成问题的原因也不尽相同。So,多搜索,多思考,解决问题。