MapReduce API

WordCount

WordCount
package a.b.c;

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.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

//继承Mapper类,重写Reduccer方法
class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
	@Override
	protected void map(LongWritable key, Text value,
			org.apache.hadoop.mapreduce.Mapper.Context context)
			throws IOException, InterruptedException {
		String lineString=value.toString();
		String [] wordStrings=lineString.split(",");
		for (String word:wordStrings){
			context.write(new Text(word), new IntWritable(1));
		}

	}
	
	
/*	protected void mapper(LongWritable key,Text value,Context context) throws IOException,InterruptedException {
		String lineString=value.toString();
		String [] wordStrings=lineString.split(",");
		for (String word:wordStrings){
			context.write(new Text(word), new IntWritable(1));
		}
		
	}*/
}

//继承Reducer类,重写reducer方法
class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,Context context)
			throws IOException, InterruptedException {
		int count=0;
		for (IntWritable value:values){
			count+=value.get();
		}
		context.write(key, new IntWritable(count));
	}
	
/*	protected void reduce(Text key, Iterable<IntWritable> values,
			Context context)
			throws IOException, InterruptedException {
		int count=0;
		for (IntWritable value:values){
			count+=value.get();
		}
		context.write(key, new IntWritable(count));

	}*/
}

public class WordCount {
	public static void main(String[] args) throws IOException ,ClassNotFoundException,InterruptedException{
		//设置程序名
		Configuration configuration=new Configuration();
		Job job=Job.getInstance(configuration,"wordCount");
		job.setJarByClass(WordCount.class);
		
		//mapper设置
		job.setMapperClass(WordCountMapper.class);
		System.out.println("before mapppp");
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		System.out.println("after mapppp");
		
		//reducer设置
		job.setReducerClass(WordCountReducer.class);	
		
		//输入输出路径
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		//返回执行成功结果
		boolean res=job.waitForCompletion(true);
		System.exit(res?0:1);
				
	
	}
	
	
	

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce是一种用于大规模数据处理的编程模型和算法。它将大规模数据集分成小的数据块,然后在集群中的多台计算机上并行处理这些数据块。MapReduce API是一种用于实现MapReduce算法的编程接口。以下是一个简单的MapReduce API的例子: ```java public class WordCount { public static class Map extends 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, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } public static class Reduce 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)); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf, "wordcount"); job.setJarByClass(WordCount.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } } ``` 这个例子是一个简单的WordCount程序,它将输入文件中的单词计数,并将结果写入输出文件。Map函数将输入文件中的每一行拆分成单词,并将每个单词映射到一个键值对,其中键是单词,值是1。Reduce函数将相同键的值相加,并将结果写入输出文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值