MapReduce基础开发之一词汇统计和排序(wordcount)

统计/var/log/boot.log中含k的字符的数量,并对含k的字符按照数量排序。需分两个job完成,一个用来统计,一个用来排序。


一、统计


1、上传文件到hadoop:
   1)新建文件夹:hadoop fs -mkdir /tmp/fjs
   2)上传文件:hadoop fs -put /var/log/boot.log /tmp/fjs


2、编写wordcount代码并导出jar和上传到namenode
   1)挂载共享文件夹,上传jar包:mount -t cifs //ip/tmp /mnt -o username=xxx,password=xxx
   2)移动jar包到tmp目录下:cp -R /mnt/wordcount.jar /tmp
   3)jar包是root权限,更改给hadoop用户:chown -R hdfs:hdfs /tmp/wordcount.jar

   代码如下:

 

package com;

import java.io.IOException;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {
	
	public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{
		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 itr = new StringTokenizer(value.toString());
			while (itr.hasMoreTokens()) {
				String strVal=itr.nextToken();//获取字符
				//if(strVal.contains("k")){//如果字符包含k,则统计
					word.set(strVal);
					context.write(word, one);
				//}
			}
		}
	}
	
	public static class IntSumReducer 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 val : values) {
				sum += val.get();
			}
			result.set(sum);
			context.write(key, result);
		}
	}
	
	public static void main(String[] args) throws Exception {
	
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount <in> <out>");
			System.exit(2);
		}
		
		Job job = new Job(conf, "word count");
		job.setJarByClass(WordCount.class);
		job.setMapperClass(TokenizerMapper.class);
		job.setCombinerClass(IntSumReducer.class);
		job.setReducerClass(IntSumReducer.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

}



3、执行wordcount.jar并查看结果
   1)执行:yarn jar /tmp/wordcount.jar /tmp/fjs /tmp/fjs/out
   2)查看:hadoop fs -text /tmp/fjs/out/part-r-0000.bz2  


二、排序


1、编写wordsort代码并导出jar和上传namenode,对wordcount执行的结果进行排序;
   排序就是利用mapreduce本身的key排序功能,主要是互换key和value。
   代码如下:
package com;
import java.io.IOException;
import java.util.StringTokenizer;

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;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordSort {
	
	public static class SortIntValueMapper extends Mapper<LongWritable, Text, IntWritable, Text>{
		
		private final static IntWritable wordCount = new IntWritable(1);
		private Text word = new Text();
		
		public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			StringTokenizer tokenizer = new StringTokenizer(value.toString());
			while (tokenizer.hasMoreTokens()) {
				word.set(tokenizer.nextToken().trim());
				wordCount.set(Integer.valueOf(tokenizer.nextToken().trim()));
				context.write(wordCount, word);//<k,v>互换
			}
		}
	}
	
	public static class SortIntValueReduce extends  Reducer<IntWritable, Text, Text, IntWritable> {
		private Text result = new Text();
		
		public void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
			for (Text val : values) {
				result.set(val.toString());
				context.write(result, key);//<k,v>互换
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordsort <in> <out>");
			System.exit(2);
		}
		
		Job job = new Job(conf, "word sort");
		job.setJarByClass(WordSort.class);
		
		job.setMapperClass(SortIntValueMapper.class);
		job.setReducerClass(SortIntValueReduce.class);
		
		job.setOutputKeyClass(IntWritable.class);
		job.setOutputValueClass(Text.class);
		
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}



2、执行wordsort.jar并查看结果
   1)执行:yarn jar /tmp/wordsort.jar /tmp/fjs/out /tmp/fjs/out1
   2)查看:hadoop fs -text /tmp/fjs/out1/part-r-0001.bz2
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,这是一个比较经典的问题,实现MapReduceWordCount单词词汇统计可以分为以下几个步骤: 1. Map阶段:将输入的文本文件按照空格或其他分隔符进行分割,每个分割出来的单词作为key,值为1,即将每个单词映射为(key, value)键值对。 2. Shuffle阶段:将Map阶段输出的键值对按照key进行排序,并将相同key的value合并为一个数组。 3. Reduce阶段:对每个key的values数组进行遍历,将其中的值累加即可得到该单词的词频。 以下是MapReduceWordCount单词词汇统计的Java代码示例: ```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 = Job.getInstance(conf, "wordcount"); job.setJarByClass(WordCount.class); job.setMapperClass(Map.class); job.setCombinerClass(Reduce.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 以上就是MapReduceWordCount单词词汇统计的实现过程和Java代码示例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值