Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2468880
  • 博文数量: 392
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 4138
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-17 13:03
个人简介

范德萨发而为

文章分类

全部博文(392)

文章存档

2017年(5)

2016年(19)

2015年(34)

2014年(14)

2013年(47)

2012年(40)

2011年(51)

2010年(137)

2009年(45)

分类: 服务器与存储

2010-01-22 10:14:48

实验环境参看本目录下的环境安装文章。

实验步驟基本参照这里
,只是在一些细节的地方有点出入

先把代码贴出来

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 Mapper<LongWritable, Text, Text, IntWritable> {
                private final static IntWritable one = new IntWritable(1);
                private Text word = new Text();

                public void map(LongWritable key, Text value, OutputCollector<Text, 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 Reducer<Text, IntWritable, Text, IntWritable> {
                public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, 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
阅读(2669) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~