免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3463 | 回复: 0
打印 上一主题 下一主题

ubuntu上运行mapreduce的word count [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-22 10:14 |只看该作者 |倒序浏览

                                实验环境参看本目录下的环境安装文章。
实验步驟基本参照
这里
,只是在一些细节的地方有点出入
先把代码贴出来
               
               
                package org.myorg;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class WordCount {
        public static class Map extends MapReduceBase implements MapperLongWritable, Text, Text, IntWritable> {
                private final static IntWritable one = new IntWritable(1);
                private Text word = new Text();
                public void map(LongWritable key, Text value, OutputCollectorText, IntWritable> output, Reporter reporter) throws IOException {
                        String line = value.toString();
                        StringTokenizer tokenizer = new StringTokenizer(line);
                        while (tokenizer.hasMoreTokens()) {
                                word.set(tokenizer.nextToken());
                                output.collect(word, one);
                        }
                }
        }
        public static class Reduce extends MapReduceBase implements ReducerText, IntWritable, Text, IntWritable> {
                public void reduce(Text key, IteratorIntWritable> values, OutputCollectorText, IntWritable> output, Reporter reporter) throws IOException {
                        int sum = 0;
                        while (values.hasNext()) {
                                sum += values.next().get();
                        }
                        output.collect(key, new IntWritable(sum));
                }
        }
        public static void main(String[] args) throws Exception {
                JobConf conf = new JobConf(WordCount.class);
                conf.setJobName("wordcount");
                conf.setOutputKeyClass(Text.class);
                conf.setOutputValueClass(IntWritable.class);
                conf.setMapperClass(Map.class);
                conf.setCombinerClass(Reduce.class);
                conf.setReducerClass(Reduce.class);
                conf.setInputFormat(TextInputFormat.class);
                conf.setOutputFormat(TextOutputFormat.class);
                FileInputFormat.setInputPaths(conf, new Path(args[0]));
                FileOutputFormat.setOutputPath(conf, new Path(args[1]));
                JobClient.runJob(conf);
        }
}
把官网上的步驟贴出来,在有出入的地方进行注释
在进行一下操作之前,确定你的hadoop dfs文件系统已成功创建,并启动hadoop,具体步驟见hadoop的
Quick_start
,或者
这里
下面进入正题:
Usage
保证你的HADOOP_HOME指向安装主目录,HADOOP_VERSION正确,可以在~/.bash_rc中添加该环境变量。
Assuming HADOOP_HOME is the root of the installation and
        HADOOP_VERSION is is the Hadoop version installed, compile
        
Using WordCount.java and create a jar:
               
               
               
                 $ mkdir wordcount_classes
$ javac -classpath ${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-core.jar -d wordcount_classes WordCount.java
/*dfs的路径不是/usr/xxx/,用bin/hadoop fs -ls来确认用户路径*/
/*这里/usr/joe/wordcout.jar是需要创建的文件,我是直接创建在本地,而不是创建到dfs上(会报错)。我的想法是在本地创建了wordcount.jar之后,put到dfs上,但是这种方法在后面运行的时候出错*/
         
         
         
         
         
         
$ jar -cvf /usr/joe/wordcount.jar -C wordcount_classes/ .
创建输入文件:在本地建好你需要的文件之后,上传到dfs。使用 bin/hadoop fs -mkdir(-put)。
Assuming that:
/usr/joe/wordcount/input  - input directory in HDFS
/usr/joe/wordcount/output - output directory in HDFS
         
Sample text-files as input:
         
$ bin/hadoop dfs -ls /usr/joe/wordcount/input/
/usr/joe/wordcount/input/file01
/usr/joe/wordcount/input/file02
$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file01
Hello World Bye World
$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file02
Hello Hadoop Goodbye Hadoop
         
         
         
         
         
         
         
         
        
确认需要的输入文件都存在之后,运行之
Run the application:
/*我把'/usr/joe/wordcount.jar替换成本地文件wordcount.jar才运行成功',按前述的操作来看,/usr/joe/应该是dfs下joe用户的主目录,但是我用本地wordcount.jar运行同样成功,疑问???*/
$ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount /usr/joe/wordcount/input /usr/joe/wordcount/output
Output:
         
            $ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
         
Bye    1
Goodbye    1
Hadoop    2
Hello    2
World    2

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/99156/showart_2157177.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP