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);
    	}
    
    }
    

      

     

  • 相关阅读:
    Apache-一个IP多个主机域名
    Apache-配置详解
    Apache-配置、测试和调试
    Linux-Memcache和Redis常用命令
    Linux-Linux下安装redis报错"undefined reference to__sync_add_and_fetch_4"解决办法
    C-从源文件到可执行文件的详细编译链接过程
    JavaScript-jQuery报TypeError $(...) is null错误(jQuery失效)解决办法
    MSSQL-SQL SERVER一些使用中的技巧
    unity 在Game视图中显示Gizmos
    unity Transform.TransformPoint
  • 原文地址:https://www.cnblogs.com/Jomini/p/11462513.html
Copyright © 2011-2022 走看看