MapReduce: WordCount的Eclipse实现

WordCountMapper.java

package cds.hadoop.wordcount;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils;

/**
 * Mapper Task
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
   

    @Override
    /**
     * key: line number;
     * value: a line of input
     */
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
            throws IOException, InterruptedException {
        // get the line
        String line = value.toString();

        // split a line onto words
        String[] words = StringUtils.split(line, ' ');

        // output <word, 1>
        for (String word : words) {
            context.write(new Text(word), new LongWritable(1L));

        }

    }

}

WordCountReducer.java

package cds.hadoop.wordcount;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

/**
 * Reducer Task
 * */
public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
   

    @Override
    // input is <key, <v1,v2,...,vn>>
    protected void reduce(Text key, Iterable<LongWritable> values,
            Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        // count the number that every word appears
        long count = 0;
        for (LongWritable value : values) {
            count += value.get();
        }

        // output is <word, count>, it's what we want
        context.write(new Text(key), new LongWritable(count));

    }

}

WordCountRunner.java

package cds.hadoop.wordcount;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * describe a job:
 * which class is mapper?
 * which class is reducer?
 * where is the input file?
 * where to output?
 * 
 * Then submit this job to Hadoop
 * */
public class WordCountRunner {
   

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job wcjob = Job.getInstance(conf);

        wcjob.setJarByClass(WordCountRunner.class);

        // set Mapper
        wcjob.setMapperClass(WordCountMapper.class);
        wcjob.setMapOutputKeyClass(Text.class);
        wcjob.setMapOutputValueClass(LongWritable.class);

        // set Reducer
        wcjob.setReducerClass(WordCountReducer.class);
        wcjob.setOutputKeyClass(Text.class);
        wcjob.setOutputValueClass(LongWritable.class);

        // where is the input file?
        // FileInputFormat is a component of Job, but it has a default realization,
        // so we don't write a class of FileInputFormat in this demo
        FileInputFormat.setInputPaths(wcjob, "hdfs://master:9000/wc/srcData");

        // where to output?
        FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://master:9000/wc/output"));

        boolean ret = wcjob.waitForCompletion(true);
    }

}

输出与结果:

2016-04-04 08:28:39,173 WARN  [main] util.NativeCodeLoader (NativeCodeLoader.java:<clinit>(62)) - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
2016-04-04 08:28:40,150 INFO  [main] Configuration.deprecation (Configuration.java:warnOnceIfDeprecated(1173)) - session.id is deprecated. Instead, use dfs.metrics.session-id
2016-04-04 08:28:40,151 INFO  [main] jvm.JvmMetrics (JvmMetrics.java:init(76)) - Initializing JVM Metrics with processName=JobTracker, sessionId=
2016-04-04 08:28:40,707 WARN  [main] mapreduce.JobResourceUploader (JobResourceUploader.java:uploadFiles(64)) - Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
2016-04-04 08:28:40,757 WARN  [main] mapreduce.JobResourceUploader (JobResourceUploader.java:uploadFiles(171)) - No job jar file set.  User classes may not be found. See Job or Job#setJar(String).
2016-04-04 08:28:41,003<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值