zoukankan      html  css  js  c++  java
  • hadoop按字母数量进行排序

    package demo;
    
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.StringTokenizer;
    
    import javax.security.auth.kerberos.KerberosTicket;
    import javax.ws.rs.core.NewCookie;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.examples.SecondarySort.IntPair.Comparator;
    import org.apache.hadoop.examples.Sort;
    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.io.WritableComparable;
    import org.apache.hadoop.mapred.KeyValueTextInputFormat;
    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 Ch
    {
      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> [<in>...] <out>");
          System.exit(2);
    }
        Job job = Job.getInstance(conf, "word");
        job.setJarByClass(Ch.class);
        job.setMapperClass(TokenizerMapper.class);
    	job.setOutputKeyClass(IntWritable.class);
    	job.setOutputValueClass(Text.class);
    
    for (int i = 0; i < otherArgs.length - 1; i++) {
    
          FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
    }
    
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[(otherArgs.length - 1)]));
    
        System.exit(job.waitForCompletion(true) ? 0 : 1);
      }
    
    
      public static class TokenizerMapper extends Mapper<Object, Text, Object, Text>
      {
        private static final IntWritable one = new IntWritable(1);
        HashMap<String,Integer> map = new HashMap<String,Integer>();
        private Text word = new Text();
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
        	String str1 = value.toString();
        	String str = str1.replaceAll(" ","");
            for (int i=0;i<str.length();i++){
                char c=str.charAt(i);
                String key1=String.valueOf(c);
                if(map.containsKey(key1)){
                    Integer value1=map.get(key1);
                    map.put(key1, value1+1);
                }
                else{
                    if(c>='A'&&c<='Z'||c>='a'&&c<='z'){
                        map.put(key1, 1);
                    }
                }
            }
            for (String key2 : map.keySet()) {
                Integer value2 = map.get(key2);  
                context.write(new IntWritable(value2), new Text(key2));
              
            }
        }
      }  
      }
    
  • 相关阅读:
    自动发送邮件功能
    工作中常用的Android系统ADB命令收集
    商城系统必须知道的【订单、优惠金额、退货、实际营收】解释
    商城系统项目必须知道的专业数据指标
    接口加密思路
    Selenium使用Chrome模拟手机浏览器方法解析
    PHP上传图片基本代码示例
    iframe子页面获取父页面的点击事件
    javascript实现网页倒计时效果
    Crontab常用命令总结
  • 原文地址:https://www.cnblogs.com/szj666/p/12654714.html
Copyright © 2011-2022 走看看