zoukankan      html  css  js  c++  java
  • WordCount-JAVA版

    WordCountMapper

    import java.io.IOException;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;
    
    public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    	Text k = new Text();
    	IntWritable v = new IntWritable(1);
    
    	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    
    		String line = value.toString();
    
    		String[] words = line.split(" ");
    
    		for (String word : words) {
    			k.set(word);
    			context.write(k, v);
    		}
    	}
    }
    

     WordCountMapper

    import java.io.IOException;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;
    
    public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    
    	Text k = new Text();
    	IntWritable v = new IntWritable(1);
    
    	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    
    		String line = value.toString();
    
    		String[] words = line.split(" ");
    
    		for (String word : words) {
    			k.set(word);
    			context.write(k, v);
    		}
    
    	}
    }
    

      WordCountReducer

    import java.io.IOException;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Reducer;
    
    public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
    	
    	int sum;
    	IntWritable v = new IntWritable();
    
    	@Override
    	protected void reduce(Text key, Iterable<IntWritable> value, Context context)
    			throws IOException, InterruptedException {
    		// 1 累加求和
    		sum = 0;
    		for (IntWritable count : value) {
    			sum += count.get();
    		}
    		// 2 输出
    		v.set(sum);
    		context.write(key, v);
    	}
    
    }
    

      

     

  • 相关阅读:
    1019.安全技能树
    1020.Burp Suite扩展之Java-Deserialization-Scanner
    1018.渗透利器
    1016.XXE漏洞攻防学习
    1017.前端黑在线工具
    1015.WebGoat SQL注入之 Order by注入解题思路
    1014.WebGoat SQL盲注 解题思路
    2019春节防坑指南之抢票陷阱(文末有彩蛋)
    【年度大戏】勒索”嘿客“无间道之战
    470余万条疑似12306用户数据遭贩卖 嫌疑人被刑拘
  • 原文地址:https://www.cnblogs.com/Jomini/p/11462513.html
Copyright © 2011-2022 走看看