MapReduce实现简单的Wordcount

1App.java

package com.qst.wordcount;


import java.io.IOException;


import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


import com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider.Text;


public class App 
{
    

public static void main( String[] args ) throws Exception
    {
       Job job=Job.getInstance();
       
       job.setJarByClass(App.class);
       
       job.setJobName("wordcount");
       // 设置输入输出路径
       FileInputFormat.addInputPath(job, new Path(args[0]));
       FileOutputFormat.setOutputPath(job, new Path(args[1]));
       //设置输出k,v
       job.setOutputKeyClass(Text.class);
       job.setOutputValueClass(IntWritable.class);
       
       //指定map,reduce类
       job.setMapperClass(WCMapper.class);
       job.setReducerClass(WCReducer.class);
    
    
    }




}

2.WCMapper类

package com.qst.wordcount;


import java.io.IOException;


import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.Mapper;


public class WCMapper extends Mapper<Writable, Text, Text, IntWritable> {


long start = System.currentTimeMillis();
int count = 0;
@Override
protected void map(Writable key, Text value, Mapper<Writable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {

String line =value.toString();

String[] arr=line.split(" ");


for( String word:arr){
context.write(new Text(word), new IntWritable(1));
}

}
}



3WCReducer类

package com.qst.wordcount;


import java.io.IOException;


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


public class WCReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

int num=0;
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
 
for( IntWritable value : values){
num += value.get();

}
context.write(key, new IntWritable(num));
}






}

jar包自取:http://pan.baidu.com/s/1geSeyY3


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值