手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop及Mahout安装

[日期:2012-10-09] 来源:Linux社区  作者:yuanyu5237 [字体:大 中 小]

7,在eclipse中配置Hadoop,

将hadoop.0.2.0下contrib/eclipse-plugin目录下的插件拷贝出来,

解压(右键解压即可),

将hadoop内的文件hadoop-common-0.21.0..jar,hadoop-hdfs-0.21.0.jar,log4j-1.2.15.jar,hadoop-mapred-0.21.0.jar拷贝到lib目录,

在插件解压后的目录打包:jar cvf hadoop-0.21.0-eclipse-plugin ./* ;

打开jar包内(META-INF)的文件MANIFEST.MF打开,用加压后的内容覆盖这个文件里的内容,并修改其中(应该在文件末尾处)的Bundle-ClassPath: classes/,lib/hadoop-common-0.21.0..jar,lib/hadoop-hdfs-0.21.0.jar,lib/log4j-1.2.15.jar,lib/hadoop-mapred-0.21.0.jar

将修改后的问价保存,然后把新的插件jar包拷贝到eclipse/plugins目录下,重启eclipse,打开map reduce视窗,配置其中的参数和之前在hadoop.1的配置文件中一样。

8,在eclipse中运行wordcount,

  1. package org.frame.base.hbase.hadoop;  
  2.  
  3. import java.io.IOException;  
  4. import java.util.StringTokenizer;  
  5.  
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.IntWritable;  
  9. import org.apache.hadoop.io.Text;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Reducer;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.util.GenericOptionsParser;  
  16.  
  17. public class WordCount {  
  18.  
  19.   /**  
  20.    * TokenizerMapper 继续自 Mapper<Object, Text, Text, IntWritable>  
  21.    *  
  22.    * [一个文件就一个map,两个文件就会有两个map]  
  23.    * map[这里读入输入文件内容 以" \t\n\r\f" 进行分割,然后设置 word ==> one 的key/value对]  
  24.    *  
  25.    * @param Object  Input key Type:  
  26.    * @param Text    Input value Type:  
  27.    * @param Text    Output key Type:  
  28.    * @param IntWritable Output value Type:  
  29.    *  
  30.    * Writable的主要特点是它使得Hadoop框架知道对一个Writable类型的对象怎样进行serialize以及deserialize.  
  31.    * WritableComparable在Writable的基础上增加了compareT接口,使得Hadoop框架知道怎样对WritableComparable类型的对象进行排序。  
  32.    *  
  33.    * @author yangchunlong.tw  
  34.    *  
  35.    */  
  36.   public static class TokenizerMapper  
  37.        extends Mapper<Object, Text, Text, IntWritable>{  
  38.  
  39.     private final static IntWritable one = new IntWritable(1);  
  40.     private Text word = new Text();  
  41.     public void map(Object key, Text value, Context context  
  42.                     ) throws IOException, InterruptedException {  
  43.       StringTokenizer itr = new StringTokenizer(value.toString());  
  44.       while (itr.hasMoreTokens()) {  
  45.         word.set(itr.nextToken());  
  46.         context.write(word, one);  
  47.       }  
  48.     }  
  49.   }  
  50.  
  51.   /**  
  52.    * IntSumReducer 继承自 Reducer<Text,IntWritable,Text,IntWritable>  
  53.    *  
  54.    * [不管几个Map,都只有一个Reduce,这是一个汇总]  
  55.    * reduce[循环所有的map值,把word ==> one 的key/value对进行汇总]  
  56.    *  
  57.    * 这里的key为Mapper设置的word[每一个key/value都会有一次reduce]  
  58.    *  
  59.    * 当循环结束后,最后的确context就是最后的结果.  
  60.    *  
  61.    * @author yangchunlong.tw  
  62.    *  
  63.    */  
  64.   public static class IntSumReducer  
  65.        extends Reducer<Text,IntWritable,Text,IntWritable> {  
  66.     private IntWritable result = new IntWritable();  
  67.  
  68.     public void reduce(Text key, Iterable<IntWritable> values,  
  69.                        Context context  
  70.                        ) throws IOException, InterruptedException {  
  71.       int sum = 0;  
  72.       for (IntWritable val : values) {  
  73.         sum += val.get();  
  74.       }  
  75.       result.set(sum);  
  76.       context.write(key, result);  
  77.     }  
  78.   }  
  79.  
  80.   public static void main(String[] args) throws Exception {  
  81.     Configuration conf = new Configuration();  
  82.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  83.     /**  
  84.      * 这里必须有输入/输出  
  85.      */  
  86.     if (otherArgs.length != 2) {  
  87.       System.err.println("Usage: wordcount <in> <out>");  
  88.       System.exit(2);  
  89.     }  
  90.     Job job = new Job(conf, "word count");  
  91.     job.setJarByClass(WordCount.class);//主类  
  92.     job.setMapperClass(TokenizerMapper.class);//mapper  
  93.     job.setCombinerClass(IntSumReducer.class);//作业合成类  
  94.     job.setReducerClass(IntSumReducer.class);//reducer  
  95.     job.setOutputKeyClass(Text.class);//设置作业输出数据的关键类  
  96.     job.setOutputValueClass(IntWritable.class);//设置作业输出值类  
  97.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//文件输入  
  98.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//文件输出  
  99.     System.exit(job.waitForCompletion(true) ? 0 : 1);//等待完成退出.  
  100.   }  
  101. } 

新建mapreduc目录,设置运行参数hdfs://localhost:9000/user/name/test hdfs://localhost:9000/user/name/result,运行

linux
相关资讯       Hadoop  Mahout  Hadoop安装  Mahout安装 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款