通过cdh运行wordcount

WordCount    Map类

<span style="font-family: Arial, Helvetica, sans-serif;">	</span><pre name="code" class="java">package com.zhangfy.hadoop;

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 TestMap extends Mapper<Object,Text,Text,IntWritable>{
	//对于每个单词赋予出现频数1,因为单词是一个一个取出来的,所以每个数量都为1
	private final static IntWritable one = new IntWritable(1);
	//存储取出来的一行单词
	private Text word = new Text();
	public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
		//StringTokenizer 对输入单词进行切分
		StringTokenizer itr = new StringTokenizer(value.toString());
		while(itr.hasMoreTokens())
		{
		      word.set(itr.nextToken());
		      context.write(word, one);
		}
	}
}

 
 

Reducer类

<pre name="code" class="java">package com.zhangfy.hadoop;

import java.io.IOException;

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

public  class TestReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
	//存取对应单词总频数
	private IntWritable result = new IntWritable();
	
	public  void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException, InterruptedException{
		//计算频数
		int sum = 0;
		for(IntWritable value:values){
			sum+=value.get();
		}
		result.set(sum);
		//写入输出
		context.write(key, result);
	}
}

 

main类

<pre name="code" class="java">package com.zhangfy.hadoop;

import java.io.IOException;
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.output.FileOutputFormat;

public class Test{
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		// TODO Auto-generated method stub
		//初始化配置
		Configuration conf = new Configuration();
		Job job = new Job(conf,"word count");
		job.setJarByClass(WordCount.class);
		
		//设置map,combine,reduce处理
		job.setMapperClass(TestMap.class);
		job.setReducerClass(TestReducer.class);
		job.setCombinerClass(TestReducer.class);
		
		//设置输出格式处理类
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		//设置输入输出路径
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		job.waitForCompletion(true);
		System.out.println("任务名称: "+job.getJobName());
		System.out.println("任务成功: "+(job.isSuccessful()?"Yes":"No"));
		
	}
}

 

在cdh运行配置




 


 

 

	
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值