在centos7下正确安装伪分布hadoop2.7.2和配置eclipse(6)

2019-08-20 19:21

I 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{ 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 { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable 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(\); System.exit(2); } Job job = new Job(conf, \); 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); } } Java ?

通过 Eclipse 运行 MapReduce

在运行 MapReduce 程序前,还需要执行一项重要操作(也就是上面提到的通过复制配置文件解决参数设置问题):将 /usr/local/hadoop/etc/hadoop 中将有修改过的配置文件(如伪分布式需要 core-site.xml 和 hdfs-site.xml),以及 log4j.properties 复制到 WordCount 项目下的 src 文件夹(~/workspace/WordCount/src)中: 1. cp /usr/local/hadoop/etc/hadoop/core-site.xml ~/workspace/WordCount/src 2. cp /usr/local/hadoop/etc/hadoop/hdfs-site.xml ~/workspace/WordCount/src 3. cp /usr/local/hadoop/etc/hadoop/log4j.properties ~/workspace/WordCount/src Shell 命令 没有复制这些文件的话程序将无法正确运行,本教程最后再解释为什么需要复制这些文件。

复制完成后,务必右键点击 WordCount 选择 refresh 进行刷新(不会自动刷新,需要手动刷新),可以看到文件结构如下所示:

WordCount项目文件结构

点击工具栏中的 Run 图标,或者右键点击 Project Explorer 中的 WordCount.java,选择 Run As -> Run on Hadoop,就可以运行 MapReduce 程序了。不过由于没有指定参数,运行时会提示 “Usage: wordcount “,需要通过Eclipse设定一下运行参数。 右键点击刚创建的 WordCount.java,选择 Run As -> Run Configurations,在此处可以设置运行时的相关参数(如果 Java Application 下面没有 WordCount,那么需要先双击 Java Application)。切换到 “Arguments” 栏,在 Program arguments 处填写 “hdfs://localhost:9000/data/input hdfs://localhost:9000/data/output” 就可以了。

设置程序运行参数

或者也可以直接在代码中设置好输入参数。可将代码 main() 函数的 String[]

otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); 改为: 1. // String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); 2. String[] otherArgs=new String[]{\,\}; /* 直接设置输入参数 */ Java 设定参数后,再次运行程序,可以看到运行成功的提示,刷新 DFS Location 后也能看到输出的 output 文件夹。 WordCount 运行结果

至此,你就可以使用 Eclipse 方便的进行 MapReduce程序的开发了。

?

在 Eclipse 中运行 MapReduce 程

序会遇到的问题

在使用 Eclipse 运行 MapReduce 程序时,会读取 Hadoop-Eclipse-Plugin 的

Advanced parameters 作为 Hadoop 运行参数,如果我们未进行修改,则默认的参数其实就是单机(非分布式)参数,因此程序运行时是读取本地目录而不是 HDFS 目录,就会提示 Input 路径不存在。

Exception in thread \org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist: file:/home/hadoop/workspace/WordCountProject/input 所以我们要么修改插件参数,要么将配置文件复制到项目中的 src 目录来覆盖参数,才能让程序能够正确运行。

此外,log4j 用于记录程序的输出日记,需要 log4j.properties 这个配置文件,如果没有复制该文件到项目中,运行程序后在 Console 面板中会出现警告提示: log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 虽然不影响程序的正确运行的,但程序运行时无法看到任何提示消息(只能看到出错信息)。


在centos7下正确安装伪分布hadoop2.7.2和配置eclipse(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:阅读中人物性格、形象、品质的常用词语

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: