Hadoop示例程序之单词统计MapReduce

在eclipse下新建一个map/reduce Project


1,新建文件MyMap.java


import java.io.IOException;  
import java.util.StringTokenizer;  
 
import org.apache.hadoop.io.IntWritable;  
import org.apache.hadoop.io.Text;  
import org.apache.hadoop.mapreduce.Mapper;  
 
public class MyMap extends Mapper<Object, Text, Text, IntWritable> {  
    private final static IntWritable one = new IntWritable(1);  
 
    private Text word;  
 
    public void map(Object key, Text value, Context context)  
            throws IOException, InterruptedException {  
 
        String line = value.toString();  
        StringTokenizer tokenizer = new StringTokenizer(line);  
        while (tokenizer.hasMoreTokens()) {  
            word = new Text();  
            word.set(tokenizer.nextToken());  
            context.write(word, one);  
        }  
    }  
}  
 


2,新建文件MyReduce.java:


import java.io.IOException;  
 
import org.apache.hadoop.io.IntWritable;  
import org.apache.hadoop.io.Text;  
import org.apache.hadoop.mapreduce.Reducer;  
 
public class MyReduce extends  
        Reducer<Text, IntWritable, Text, IntWritable> {  
    public void reduce(Text key, Iterable<IntWritable> values, Context context)  
            throws IOException, InterruptedException {  
        int sum = 0;  
        for (IntWritable val : values) {  
            sum += val.get();  
        }  
        context.write(key, new IntWritable(sum));  
    }  
}  


3,新建一个文件MyDriver.java

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.lib.input.FileInputFormat;  
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
 
public class MyDriver {  
 
      
    public static void main(String[] args) throws Exception,InterruptedException {  
        Configuration conf=new Configuration();  
          
       Job job=new Job(conf,"Hello Hadoop World");  
         
       job.setJarByClass(MyDriver.class);           
        
       job.setMapOutputKeyClass(Text.class);      
       job.setMapOutputValueClass(IntWritable.class);  
         
       job.setOutputKeyClass(Text.class);  
       job.setOutputValueClass(IntWritable.class);  
         
       job.setMapperClass(MyMap.class);  
        job.setCombinerClass(MyReduce.class);  
        job.setReducerClass(MyReduce.class);  
          
        job.setInputFormatClass(TextInputFormat.class);            
        job.setOutputFormatClass(TextOutputFormat.class);          
          
        FileInputFormat.setInputPaths(job, new Path("./input/555.txt"));  
          
        FileOutputFormat.setOutputPath(job, new Path("./input/out.txt"));  

       job.waitForCompletion(true);  
       }  
 

好了,见证奇迹的时刻到了,先在工程目录下创建一个目录input,并在下面新建文件555.txt,

Hello World 555   hahaha

Hello World

保存,运行java应用程序

在input下多了个文件目录:out.txt,该目录下有个文件part-r-0000文件,

打开后文件的内容是:

555    1
Hello    2
World    2
hahaha    1

算是搞定了。。。

运行中碰到包错:

org.apache.hadoop.fs.ChecksumException: Checksum error:

这个好办 只要将工程文件下的CRC数据校验文件删除就可以了

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值